Toybrick
标题: 转换并部署yolov5模型(demo)(详细图文见附件) [打印本页]
作者: safe 时间: 2023-3-7 10:00
标题: 转换并部署yolov5模型(demo)(详细图文见附件)
本帖最后由 safe 于 2023-3-7 10:05 编辑
1. 下载yolov5s.pt
2. 打开文件夹下models->yolo.py
将:
def forward(self, x):
z = [] # inference output
for i in range(self.nl):
x = self.m(x) # conv
bs, _, ny, nx = x.shape # x(bs,255,20,20) to x(bs,3,20,20,85)
x = x.view(bs, self.na,self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
if not self.training: # inference
if self.grid.shape[2:4] !=x.shape[2:4] or self.onnx_dynamic:
self.grid,self.anchor_grid = self._make_grid(nx, ny, i)
y = x.sigmoid()
if self.inplace:
y[..., 0:2] = (y[..., 0:2]* 2. - 0.5 + self.grid) * self.stride # xy
y[..., 2:4] = (y[..., 2:4]* 2) ** 2 * self.anchor_grid # wh
xy = (y[..., 0:2] * 2. -0.5 + self.grid) * self.stride #xy
wh = (y[..., 2:4] * 2) ** 2* self.anchor_grid # 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)
修改为:(仅仅是转换模型才需要修改,训练时不需要修改)
def forward(self, x):
z = [] # inference output
for i in range(self.nl):
x = self.m(x) # conv
return x
3. 返回上一层打开export.py将opset_version=opset修改为opset_version=12 :
将last.pt移动至export.py同一文件夹下:(last.pt是训练好的最终模型,这里没有训练直接用yolov5s.pt代替)
4. 在项目的命令行输入以下命令:
python export.py --weights 自己的模型.pt --img 640 --batch 1 --includeonnx
(这里用python export.py --weightyolov5s.pt --img 640 --batch 1 --include onnx)
得到转换的模型yolov5s.onnx
*接下来进入Ubuntu22.04中进行正式的rknn模型的转换(转换工具rknn-toolkit2只能在Linux系统上运行)
6. 在Ubuntu使用anaconda创建python3.8虚拟环境,创建环境并命名为rknn2_3588
8. 安装rknn-toolkit2工具包:
首先进入packages中:
打开终端输入source activate rknn2_3588激活环境:
输入命令pipinstall rknn_toolkit2-1.4.0_22dcfef4-cp38-cp38-linux_x86_64.whl:
提示安装成功
然后检查是否安装成功,先输入python进入python环境,然后输入from rknn.api import RKNN:
没有报错,所以说明rknn2工具包安装成功
9. onnx转换为rknn:
(1)首先将前面转换得到的自己的模型.onnx复制到./examples/onnx/yolov5文件夹中:(这里用yolov5s.onnx,理由同上)
(2)打开该文件夹下的test.py,修改如下图中选中的地方,应该把:
ONNX_MODEL = 'yolov5s.onnx'
RKNN_MODEL = 'yolov5s.rknn'
IMG_PATH = './bus.jpg'
DATASET = './dataset.txt'
......
CLASSES = ("person", "bicycle", "car","motorbike ", "aeroplane ", "bus ","train", "truck ", "boat", "trafficlight",
"fire hydrant", "stop sign ","parking meter", "bench", "bird","cat", "dog ", "horse ", "sheep","cow", "elephant",
"bear", "zebra ","giraffe", "backpack", "umbrella","handbag", "tie", "suitcase","frisbee", "skis", "snowboard", "sportsball", "kite",
"baseball bat", "baseball glove","skateboard", "surfboard", "tennis racket","bottle", "wine glass", "cup", "fork","knife ",
"spoon", "bowl", "banana","apple", "sandwich", "orange","broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair","sofa",
"pottedplant", "bed","diningtable", "toilet ", "tvmonitor","laptop ", "mouse ", "remote ", "keyboard", "cell phone", "microwave ",
"oven ", "toaster","sink", "refrigerator ", "book","clock", "vase", "scissors ", "teddy bear", "hair drier", "toothbrush ")
...
rknn.config(mean_values=[[0, 0, 0]], std_values=[[255,255, 255]])
修改为:
ONNX_MODEL = '自己模型的名字.onnx'
RKNN_MODEL = '自己模型的名字.rknn'
IMG_PATH = '待预测的图片'
DATASET = './dataset.txt'
......
CLASSES = (自定义类别)
...
rknn.config(mean_values=[[0, 0, 0]], std_values=[[255,255, 255]],target_platform='rk3588')(rk3588是搭载平台的名字)
(这里不做修改,理由同上)
(3)然后打开终端输入命令sourceactivate rknn2_3588,然后输入python test.py:
得到自己的模型.rknn(这里是yolov5s.rknn)
10. 3588平台部署
(2)进入rknn_yolov5_demo文件夹修改include文件中的头文件postprocess.h:
#define OBJ_CLASS_NUM 80
修改为:
#define OBJ_CLASS_NUM 自定义类别个数
(3)修改model目录下的coco_80_labels_list.txt文件,更改为自定义类别
(4)将转换后的rknn文件放在model/RK3588目录下;
编译运行bash ./build-linux_RK3588.sh生成install目录;
在model目录下放入需要推理的图片(这里用demo中的bus.jpg);
在终端中输入:
./rknn_yolov5_demo ./model/RK3588/yolov5s.rknn./model/bus.jpg
得到结果out.jpg
[attach]2616[/attach]
作者: tjmies 时间: 2023-3-31 16:16
请教一下 对应的box_priors.txt文件如何生成
欢迎光临 Toybrick (https://t.rock-chips.com/) |
Powered by Discuz! X3.3 |