• YOLOV5移植RV1126说明


    YOLOV5移植RV1126说明

    说明

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

    操作过程简述

    1. 在YOLOV5中训练出 xxx.pt 模型
    2. 将pt转为onnx模型
    3. 将onnx转为rknn模型
    4. 部署rknn模型

    环境特别说明

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

    YOLOV5 环境 (python 3.8)

    1. 下载YOLOV5的代码: https://github.com/airockchip/yolov5 f022f01b2070dfd415c4eb8f33da2677afe8bfa2
    2. 修改 requirements.txt, 要打开 onnx coremltools
    # export --------------------------------------
    coremltools==4.0
    onnx>=1.8.0
    # scikit-learn==0.19.2  # for coreml quantization
    
    • 1
    • 2
    • 3
    • 4
    1. 安装requirements.txt (采用清华源,比较快)
    pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
    
    • 1
    1. 训练出 pt 文件
    # 以训练 coco128 为例
    python3.8 train.py --img 640 --batch 16 --epochs 5 --device cpu --data ./data/coco128.yaml --cfg ./models/yolov5s.yaml
    
    • 1
    • 2

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

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

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

    rknn-toolkit 环境 (python 3.6)

    1. 环境安装
    https://github.com/rockchip-linux/rknn-toolkit
    
    • 1
    1. onnx 转为 rknn
      可参考 toolkit/example/onnx/yolov5/test.py 写一个转成rknn的即可

    2. 在RV1126 的部署(python版)
      toolkit 是支持python 或者C API的, 这里部署 python 版的.
      注意:由于 rv1126 版本是仅支持 RKNNLite ,因此需要将toolkit 的 example 改为 RKNNLite

    问题

    1. _pickle.UnpicklingError: STACK_GLOBAL requires str

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

    2. 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)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. 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)
    
    • 1
  • 相关阅读:
    二叉树的最近公共祖先
    Yolov8-pose关键点检测:模型轻量化创新 | OREPA结合c2f,节省70%的显存!训练速度提高2倍! | CVPR2022
    WPS转PDF怎么转换?接下来分享这三个方法和操作步骤给你
    debian/ubuntu/linux如何快速安装vscode
    网络原理 - 详解
    国内最受欢迎的电商API接口调用京东商品详情数据
    word怎么公式求平均值
    会计制度设计名词解释
    5.24 基础题目
    ubuntu 22.04版本修改服务器名、ip,dns信息的操作方法
  • 原文地址:https://blog.csdn.net/todo9351/article/details/125557607