【边缘设备】yolov5训练与rknn模型导出并在RK3588部署~4.导出模型(亲测有效)

保姆级教程,看这一篇就够用了。
在翻阅了网络上很多资料后,发现很多版本的信息不匹配。
花了一周的时间配置环境,以及环境验证,然后写了这篇长文。
有过程,有代码,有经验,欢迎大家批评指正。

一、环境准备
【边缘设备】yolov5训练与rknn模型导出并在RK3588部署~1.环境准备(亲测有效)

二、环境验证
【边缘设备】yolov5训练与rknn模型导出并在RK3588部署~2.环境验证(亲测有效)

三、yolov5 训练自己的模型
【边缘设备】yolov5训练与rknn模型导出并在RK3588部署~3.YOLOv5训练自己的模型(亲测有效)

四、导出模型
【边缘设备】yolov5训练与rknn模型导出并在RK3588部署~4.导出模型(亲测有效)

五、部署模型
【边缘设备】yolov5训练与rknn模型导出并在RK3588部署~5.模型部署(亲测有效)

四、导出模型

前提条件: 已经创建了虚拟环境 rknn
前提条件: 已经创建了虚拟环境 rknn
前提条件: 已经创建了虚拟环境 rknn

问题及解决方案
解决 AttributeError: module ‘numpy‘ has no attribute ‘int‘ 训练yolo时的问题 原因:np.int在NumPy 1.20中已弃用,在NumPy 1.24中已删除。
一步真实解决RuntimeError: result type Float can‘t be cast to the desired output type long int

在训练之前确定 ./models/yolo.py 文件中的 class Detect(nn.Module):line:55forward 代码是否注释了。

此时应修改 forward 代码为:

    def forward(self, x):
        z = []  # inference output
        for i in range(self.nl):
            x[i] = self.m[i](x[i])  # conv
        return x # 此处如果是训练, 请注释
1. 修改参数
  1. 修改 config_onekey.sh 相关参数
  2. 将测试用的图片放到 export 文件夹下,修改 proj_dataset.txt 的内容为自己的图片列表
2. 开始一键导出

前提条件: 已经创建了虚拟环境 pytorch
前提条件: 已经创建了虚拟环境 pytorch
前提条件: 已经创建了虚拟环境 pytorch

bash onekey_bash/export_rknn.sh

终端显示如下:

(base) jxxx@desk:~/workspace/algo/yolov5_rknn$ bash onekey_bash/export_rknn.sh 
export: weights=runs/train/drp/weights/best.pt, img_size=[320], batch_size=1, device=cpu, include=['torchscript', 'onnx'], half=False, inplace=False, train=False, optimize=True, dynamic=False, simplify=True, opset=12
YOLOv5 🚀 c5360f6 torch 2.0.0+cu117 CPU

Fusing layers... 
Model Summary: 224 layers, 7053910 parameters, 0 gradients, 16.3 GFLOPs

PyTorch: starting from runs/train/drp/weights/best.pt (14.3 MB)

TorchScript: starting export with torch 2.0.0+cu117...
TorchScript: export success, saved as runs/train/drp/weights/best.torchscript.pt (28.3 MB)

ONNX: starting export with onnx 1.13.1...
...
...
I rknn buiding done
done
--> Export rknn model
done
--> Init runtime environment
W init_runtime: Target is None, use simulator!
done
(0.3333333333333333, 0.3333333333333333) 0.0 70.0
--> Running model
Analysing : 100%|███████████████████████████████████████████████| 161/161 [00:00<00:00, 5866.87it/s]
Preparing : 100%|█████████████████████████████████████████████████| 161/161 [00:04<00:00, 39.76it/s]
W inference: The dims of input(ndarray) shape (320, 320, 3) is wrong, expect dims is 4! Try expand dims to (1, 320, 320, 3)!
W inference: The dims of input(ndarray) shape (320, 320, 3) is wrong, expect dims is 4! Try expand dims to (1, 320, 320, 3)!
W inference: The dims of input(ndarray) shape (320, 320, 3) is wrong, expect dims is 4! Try expand dims to (1, 320, 320, 3)!
W inference: The dims of input(ndarray) shape (320, 320, 3) is wrong, expect dims is 4! Try expand dims to (1, 320, 320, 3)!
done
class: person, score: 0.8913032412528992
box coordinate topleft: (159.54501259326935, 145.2426918745041), bottomright: (177.8633736371994, 186.3882895708084)
class: person, score: 0.8837643265724182
box coordinate topleft: (82.14642906188965, 183.73637676239014), bottomright: (110.66749000549316, 226.49699115753174)
class: person, score: 0.8643947243690491
box coordinate topleft: (146.98097324371338, 140.67060202360153), bottomright: (164.14344692230225, 166.86144143342972)
class: person, score: 0.8264284729957581
box coordinate topleft: (110.99540531635284, 146.92019426822662), bottomright: (130.9836596250534, 177.2033714056015)
class: person, score: 0.7860860824584961
box coordinate topleft: (146.88146114349365, 127.81136840581894), bottomright: (163.5996789932251, 148.1453576683998)
class: person, score: 0.7520475387573242
box coordinate topleft: (170.8560609817505, 123.72485554218292), bottomright: (185.6856870651245, 143.04388225078583)
class: person, score: 0.6209931373596191
box coordinate topleft: (108.76676034927368, 133.68206477165222), bottomright: (119.2998595237732, 156.40799260139465)

成功后,可以看到 export 文件下多了 rknn 模型。

在这里插入图片描述

参考(以下为编撰本文的引用参考)

  1. yolov5训练pt模型并转换为rknn模型,部署在RK3588开发板上——从训练到部署全过程
  2. RK3588模型推理总结
  3. NPU使用
  4. 一步一步教大家使用RK3588的NPU之在电脑端交叉编译yolov5运行在ARM平台上-哔哩哔哩
  5. 转换yolov5s.onnx为rknn模型RK3588主板实时推理和电脑端模拟推理-哔哩哔哩
  6. 摆脱电脑,直接在RK3588平台上推理,帮用户部署 RKNN 模加速 AI 应用的落地-哔哩哔哩
  7. 仓库下载 参考 rknn-toolkit 教程, 需要切换到 commit ID c5360f6e7009eb4d05f14d1cc9dae0963e949213, 可以直接在 官网github下载, 然后终端执行 git checkout c5360f6e7
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄金旺铺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值