首页 > 技术知识 > 正文

说明

实现在yolov5训练出模型,然后在rv1126设备上运行。

操作过程简述

在YOLOV5中训练出 xxx.pt 模型

将pt转为onnx模型

将onnx转为rknn模型

部署rknn模型

环境特别说明

YOLOV5的运行环境是python3.8的,而rv1126的开发环境toolkit目前只支持到python 3.6的(linux),因此,需要两个python的环境

环境的管理采用conda YOLOV5 环境 (python 3.8) 下载YOLOV5的代码: https://github.com/airockchip/yolov5 f022f01b2070dfd415c4eb8f33da2677afe8bfa2

修改 requirements.txt, 要打开 onnx coremltools

# export ————————————– coremltools==4.0 onnx>=1.8.0 # scikit-learn==0.19.2 # for coreml quantization

安装requirements.txt (采用清华源,比较快)

pip3 install -r requirements.txt -i <https://pypi.tuna.tsinghua.edu.cn/simple>

训练出 pt 文件

# 以训练 coco128 为例 python3.8 train.py –img 640 –batch 16 –epochs 5 –device cpu –data ./data/coco128.yaml –cfg ./models/yolov5s.yaml

注:这里训练默认会以 yolov5s.pt 作模版训练的,如果是全新训练的话,需要改一下 —weights ‘’, 训练完成后,可先执行如下操作验证一下

python3 detect.py –source $1 –weight yolov5s.pt –conf-thres 0.3 –device cpu 转换为 onnx 文件 python3.8 models/export.py –weights yolov5s.pt –img 640 –batch 1

接下来就转到 rknn-toolkit的环境

rknn-toolkit 环境 (python 3.6)

环境安装

<https://github.com/rockchip-linux/rknn-toolkit>

onnx 转为 rknn

可参考 toolkit/example/onnx/yolov5/test.py 写一个转成rknn的即可

在RV1126 的部署(python版)

toolkit 是支持python 或者C API的, 这里部署 python 版的.

注意:由于 rv1126 版本是仅支持 RKNNLite ,因此需要将toolkit 的 example 改为 RKNNLite 问题

_pickle.UnpicklingError: STACK_GLOBAL requires str

在使用YOLOv5进行训练时,可能会出现_pickle.UnpicklingError: STACK_GLOBAL requires str 这样的错误,解决办法是,将数据集中的labels.cache文件删掉

RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place

https://blog.csdn.net/weixin_44612221/article/details/115359866

找到在File “models\yolo.py”的line 145

将代码修改为: def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency # cf = torch.bincount[torch.tensor(np.concatenate(dataset.labels, 0](:, 0)).long(), minlength=nc) + 1. m = self.model[-1] # Detect() module for mi, s in zip(m.m, m.stride): # from b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) with torch.no_grad(): b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) b[:, 5:] += math.log(0.6 / (m.nc – 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor

https://blog.csdn.net/weixin_43401024/article/details/124428432

1.点击报错行该路径,进入编辑页, upsampling.py 修改forward函数 return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners)

猜你喜欢