赞
踩
Ubuntu 18.04.5 LTS
py3.8-rknn2-1.4.0
迅为itop-3568开发板
采用yolov5训练并将pt转换为onnx,再将onnx采用py3.8-rknn2-1.4.0推理转换为rknn,rknn模型能正常转换,并且推理显示正常。但将rknn文件放到开发板,使用rknn_toolkit_lite2进行推理时,得到的推理图片出现大量锚框变花屏,如下。
经过排查发现是前面为了解决rknn置信度大于1,图像出现乱框问题在将pt导出为onnx文件时,对yolo.py文件做了修改,引入了sigmoid函数。
models/yolo.py
def forward(self, x): z = [] # inference output for i in range(self.nl): if os.getenv('RKNN_model_hack', '0') != '0': x[i] = torch.sigmoid(self.m[i](x[i])) # conv return x # def forward(self, x): # z = [] # inference output # for i in range(self.nl): # x[i] = self.m[i](x[i]) # conv # bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) # x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() # # if not self.training: # inference # if self.grid[i].shape[2:4] != x[i].shape[2:4] or self.onnx_dynamic: # self.grid[i] = self._make_grid(nx, ny).to(x[i].device) # # y = x[i].sigmoid() # if self.inplace: # y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy # y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh # else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953 # xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy # wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i].view(1, self.na, 1, 1, 2) # wh # y = torch.cat((xy, wh, y[..., 4:]), -1) # z.append(y.view(bs, -1, self.no)) # # return x if self.training else (torch.cat(z, 1), x)
而板子上跑的test_rknn_lite.py后期对数据处理函数与虚拟机上推理导出rknn函数存在差异,而这差异就是对sigmoid函数的处理不同。
修改过后推理结果正常。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。