Toybrick

标题: .torchscript.pt转rknn报错 [打印本页]

作者: tiger_k    时间: 2022-3-24 11:43
标题: .torchscript.pt转rknn报错
运行环境:rknn-toolkit-1.7.1 docker、ubuntu18.4虚拟机、demo /examples/pytorch/yolov5 、yolov5V6.0+yolov5s.pt(官方模型V6.0版本)操作流程:
1.修改 yolov5/models/yolo.py的后处理部分,替换class Detect(nn.Module) 类的子函数forward为
  1. def forward(self, x):
  2.     z = []  # inference output
  3.     for i in range(self.nl):
  4.        x[i] = self.m[i](x[i])  # conv
  5.    
  6.     return x
复制代码
2.从官方yolov5s.pt导出模型yolov5s.torchscript.pt
python export.py --weights yolov5s.pt --img 640 --batch 1 --include torchscript

3.复制yolov5s.torchscript.pt到docker:/examples/pytorch/yolov5 目录下
4.模型转换和推理
python3 test.py
转换报错
  1. --> Config model
  2. done
  3. --> Loading model
  4. yolov5s.torchscript.pt ********************
  5. E Catch exception when loading pytorch model: yolov5s.torchscript.pt!
  6. E Traceback (most recent call last):
  7. E   File "rknn/api/rknn_base.py", line 376, in rknn.api.rknn_base.RKNNBase.load_pytorch
  8. E   File "rknn/base/RKNNlib/RK_nn.py", line 157, in rknn.base.RKNNlib.RK_nn.RKnn.load_pytorch
  9. E   File "rknn/base/RKNNlib/app/importer/import_pytorch.py", line 129, in rknn.base.RKNNlib.app.importer.import_pytorch.ImportPytorch.run
  10. E   File "rknn/base/RKNNlib/converter/convert_pytorch_new.py", line 3971, in rknn.base.RKNNlib.converter.convert_pytorch_new.convert_pytorch.load
  11. E   File "/usr/local/lib/python3.6/dist-packages/torch/jit/__init__.py", line 275, in load
  12. E     cpp_module = torch._C.import_ir_module(cu, f, map_location, _extra_files)
  13. E RuntimeError:
  14. E aten::_convolution(Tensor input, Tensor weight, Tensor? bias, int[] stride, int[] padding, int[] dilation, bool transposed, int[] output_padding, int groups, bool benchmark, bool deterministic, bool cudnn_enabled) -> (Tensor):
  15. E Expected at most 12 arguments but found 13 positional arguments.
复制代码
我对demo的操作和理解有哪里不对的吗 烦指点




作者: tiger_k    时间: 2022-3-24 11:45
我的ubuntu虚拟机中导出运行yolov5的环境是
torch                         1.9.0
Python 3.6.13 :: Anaconda, Inc.
作者: tiger_k    时间: 2022-3-24 11:50
完整的错误信息是
  1. root@e0ded6759c5b:/examples/pytorch/yolov5# python test.py
  2. --> Config model
  3. done
  4. --> Loading model
  5. yolov5s.torchscript.pt ********************
  6. E Catch exception when loading pytorch model: yolov5s.torchscript.pt!
  7. E Traceback (most recent call last):
  8. E   File "rknn/api/rknn_base.py", line 376, in rknn.api.rknn_base.RKNNBase.load_pytorch
  9. E   File "rknn/base/RKNNlib/RK_nn.py", line 157, in rknn.base.RKNNlib.RK_nn.RKnn.load_pytorch
  10. E   File "rknn/base/RKNNlib/app/importer/import_pytorch.py", line 129, in rknn.base.RKNNlib.app.importer.import_pytorch.ImportPytorch.run
  11. E   File "rknn/base/RKNNlib/converter/convert_pytorch_new.py", line 3971, in rknn.base.RKNNlib.converter.convert_pytorch_new.convert_pytorch.load
  12. E   File "/usr/local/lib/python3.6/dist-packages/torch/jit/__init__.py", line 275, in load
  13. E     cpp_module = torch._C.import_ir_module(cu, f, map_location, _extra_files)
  14. E RuntimeError:
  15. E aten::_convolution(Tensor input, Tensor weight, Tensor? bias, int[] stride, int[] padding, int[] dilation, bool transposed, int[] output_padding, int groups, bool benchmark, bool deterministic, bool cudnn_enabled) -> (Tensor):
  16. E Expected at most 12 arguments but found 13 positional arguments.
  17. E :
  18. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/nn/modules/conv.py(440): _conv_forward
  19. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/nn/modules/conv.py(443): forward
  20. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/nn/modules/module.py(1039): _slow_forward
  21. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/nn/modules/module.py(1051): _call_impl
  22. E /home/bertha/Desktop/1/yolov5-6.0/models/common.py(48): forward_fuse
  23. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/nn/modules/module.py(1039): _slow_forward
  24. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/nn/modules/module.py(1051): _call_impl
  25. E /home/bertha/Desktop/1/yolov5-6.0/models/yolo.py(156): _forward_once
  26. E /home/bertha/Desktop/1/yolov5-6.0/models/yolo.py(133): forward
  27. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/nn/modules/module.py(1039): _slow_forward
  28. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/nn/modules/module.py(1051): _call_impl
  29. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/jit/_trace.py(959): trace_module
  30. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/jit/_trace.py(744): trace
  31. E export.py(56): export_torchscript
  32. E export.py(304): run
  33. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/autograd/grad_mode.py(28): decorate_context
  34. E export.py(359): main
  35. E export.py(364): <module>
  36. E Serialized   File "code/__torch__/torch/nn/modules/conv.py", line 11
  37. E     x: Tensor) -> Tensor:
  38. E     _0 = self.bias
  39. E     x0 = torch._convolution(x, self.weight, _0, [2, 2], [2, 2], [1, 1], False, [0, 0], 1, False, False, True, True)
  40. E          ~~~~~~~~~~~~~~~~~~ <--- HERE
  41. E     return x0
  42. E Please feedback the detailed log file <log_feedback_to_the_rknn_toolkit_dev_team.log> to the RKNN Toolkit development team.
  43. E You can also check github issues: https://github.com/rockchip-linux/rknn-toolkit/issues
  44. Load yolov5 failed!
复制代码

有一个理解不了的地方是 为什么docker中运行程序 报错信息中会出现主机中的conda环境,
  1. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/nn/modules/module.py(1039): _slow_forward
  2. E /home/bertha/anaconda3/envs/rknny5v5/lib/python3.6/site-packages/torch/nn/modules/module.py(1051): _call_impl
复制代码

甚至会报出关于yolov5的代码部分
  1. E /home/bertha/Desktop/1/yolov5-6.0/models/yolo.py(133): forward
复制代码

我理解docker应该是一个独立的环境呀 是我哪里操作有问题吗  
求帮助 谢谢
作者: tiger_k    时间: 2022-3-24 11:52
还有一个情况是docker中的demo test.py 有一行会报错,被我屏蔽了
  1. import os
  2. import urllib
  3. import traceback
  4. import time
  5. import sys
  6. import numpy as np
  7. import cv2
  8. #from torch.autograd.grad_mode import F
  9. from rknn.api import RKNN
  10. ....
复制代码

作者: ybqaq    时间: 2022-4-1 00:29
我也有类似的错误,同问啊
作者: wokafu    时间: 2022-4-11 17:52
yolov5的版本和docker里面的torch版本要一致,我们这边都是1.7.0就没有问题。一个1.7.0,一个1.6.0就会出现这样的问题,亲测




欢迎光临 Toybrick (https://t.rock-chips.com/) Powered by Discuz! X3.3