onnx转换为rknn置信度大于1,图像出现乱框问题解决

前言

环境介绍:

1.编译环境

Ubuntu 18.04.5 LTS

2.RKNN版本

py3.8-rknn2-1.4.0

3.单板

迅为itop-3568开发板


一、现象

采用yolov5训练并将pt转换为onnx,再将onnx采用py3.8-rknn2-1.4.0推理转换为rknn出现置信度大于1,并且图像乱框问题。

类似下面这样

二、解决

经过网上一顿查找发现是在将pt文件转化为onnx时对models/yolo.py的修改有问题。网上大部分的修改都是下面这种

models/yolo.py

python 复制代码
def forward(self, x):
    z = []  # inference output
    for i in range(self.nl):
        x[i] = 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.dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
    #                 self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)

    #             if isinstance(self, Segment):  # (boxes + masks)
    #                 xy, wh, conf, mask = x[i].split((2, 2, self.nc + 1, self.no - self.nc - 5), 4)
    #                 xy = (xy.sigmoid() * 2 + self.grid[i]) * self.stride[i]  # xy
    #                 wh = (wh.sigmoid() * 2) ** 2 * self.anchor_grid[i]  # wh
    #                 y = torch.cat((xy, wh, conf.sigmoid(), mask), 4)
    #             else:  # Detect (boxes only)
    #                 xy, wh, conf = x[i].sigmoid().split((2, 2, self.nc + 1), 4)
    #                 xy = (xy * 2 + self.grid[i]) * self.stride[i]  # xy
    #                 wh = (wh * 2) ** 2 * self.anchor_grid[i]  # wh
    #                 y = torch.cat((xy, wh, conf), 4)
    #             z.append(y.view(bs, self.na * nx * ny, self.no))
                

    #     return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x)

这是导致问题的根源,至于为什么现在我还没办法回答。正确的应该按如下方式修改

models/yolo.py

python 复制代码
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)

export.py文件的run函数

python 复制代码
# shape = tuple((y[0] if isinstance(y, tuple) else y).shape)  # model output shape
shape = tuple(y[0].shape)  # model output shape

export.py文件的开头加上

python 复制代码
#onn转换添加内容
import os
os.environ['RKNN_model_hack'] = 'npu_2'
#

修改之后按照如下命令导出onnx

其中./runs/train/exp3/weights/best.pt换成自己训练的pt文件

bash 复制代码
python export.py --weights ./runs/train/exp3/weights/best.pt --img 640 --batch 1 --include onnx --opset 12

参考这位大佬的文件

相关推荐
音沐mu1 个月前
【基于YOLOv5的反光衣检测预警系统】可检测图片、视频、摄像头,支持GPU加速检测以及语音播报预警
人工智能·yolo·目标检测·yolov5·反光衣检测
zh路西法2 个月前
基于opencv-C++dnn模块推理的yolov5 onnx模型
c++·图像处理·pytorch·opencv·yolo·dnn·yolov5
不做签到员2 个月前
RKLLM部署
python·大模型·rk3588·rknn·rkllm
jcfszxc2 个月前
【Rockchip系列】RGA imcopy 性能分析:不同缓冲区拷贝的对比(含实验代码)
开发语言·c++·rknn·rockchip·rknn-toolkit2
jcfszxc2 个月前
【RKNN系列】常用函数:使用RGA加速画框
c++·rknn·rockchip·rknn-toolkit2
从懒虫到爬虫2 个月前
yolov5明厨亮灶检测系统,厨师帽-口罩检测,带pyqt界面-可检测图片和视频,支持中文标签,检测接口已封装好并优化,代码可读性强!
yolov5·厨房·口罩厨师帽
jcfszxc2 个月前
RKNN 系列文章导航
rknn·rknn-toolkit2
从懒虫到爬虫3 个月前
yolov5 +gui界面+单目测距 实现对图片视频摄像头的测距
yolov5·单目测距·qq767172261
弗兰随风小欢4 个月前
【目标检测实验系列】EMA高效注意力机制,融合多尺度特征,助力YOLOv5检测模型涨点(文内附源码)
人工智能·深度学习·yolo·目标检测·计算机视觉·yolov5·注意力机制
不做签到员4 个月前
Bubbliiiing 的 Retinaface rknn python推理分析
开发语言·python·rk3588·rknn·后处理·retinaface