瑞芯微1808模型转换(onnx →\torknn)环境配置

阅读本解决方案前,请读者确保已经根据官方的相关教程【rknn_model_zoo/common/rknn_converter at v1.5.0 · airockchip/rknn_model_zoo (github.com)】完成其他配置文件的修改,以便和您的模型参数相符合,才能转换成功

一、基本配置

  1. 创建虚拟环境

    conda create -n rknn python=3.8 -y
  2. 安装pytorch,该命令读者可在pytorch的历史版本【Previous PyTorch Versions | PyTorch】配置命令中找到

    conda install pytorch==1.10.0 torchvision==0.11.0 torchaudio==0.10.0 cpuonly -c pytorch
  3. 修改requirements文件,将tensorflow-gpu1.14.0修改为tensorflow-gpu2.2.0

  4. 安装对应包

    pip install -r requirements-gpu.txt

    pip install -r requirements-cpu.txt
  5. 定位到rknn_toolkit-1.7.5-cp38-cp38-linux_x86_64.whl所在目录下,执行

    pip install rknn_toolkit-1.7.5-cp38-cp38-linux_x86_64.whl

二、解决方案

  1. 报错1

    Traceback (most recent call last):File "rknn_convert.py", line 14, in from phase import convert_phase, validate_phaseFile "/17106/Pengcaiping/rknn_model_zoo/common/rknn_converter/phase.py", line 17, in from image_utils.img_preprocesser_tools import Image_preprocessorFile "/17106/Pengcaiping/rknn_model_zoo/common/image_utils/img_preprocesser_tools.py", line 1, in import cv2File "/root/anaconda3/envs/rknn/lib/python3.8/site-packages/cv2/__init__.py", line 181, in bootstrap()File "/root/anaconda3/envs/rknn/lib/python3.8/site-packages/cv2/__init__.py", line 153, in bootstrapnative_module = importlib.import_module("cv2")File "/root/anaconda3/envs/rknn/lib/python3.8/importlib/__init__.py", line 127, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)ImportError: libGL.so.1: cannot open shared object file: No such file or directory

    解决方案:

    • 安装opencv-python-headless库
    pip install opencv-python-headless

    如果安装过后还是没能解决问题,可能是openGL库确实,先给他装上,下面的命令在非root用户下执行,如果是root用户,则去掉前缀sudo

    sudo apt-get updatesudo apt-get install libgl1-mesa-glx
  2. 报错2

    Traceback (most recent call last):File "rknn_convert.py", line 14, in from phase import convert_phase, validate_phaseFile "/17106/Pengcaiping/rknn_model_zoo/common/rknn_converter/phase.py", line 17, in from image_utils.img_preprocesser_tools import Image_preprocessorFile "/17106/Pengcaiping/rknn_model_zoo/common/image_utils/img_preprocesser_tools.py", line 1, in import cv2File "/root/anaconda3/envs/rknn/lib/python3.8/site-packages/cv2/__init__.py", line 181, in bootstrap()File "/root/anaconda3/envs/rknn/lib/python3.8/site-packages/cv2/__init__.py", line 175, in bootstrapif __load_extra_py_code_for_module("cv2", submodule, DEBUG):File "/root/anaconda3/envs/rknn/lib/python3.8/site-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_modulepy_module = importlib.import_module(module_name)File "/root/anaconda3/envs/rknn/lib/python3.8/importlib/__init__.py", line 127, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)File "/root/anaconda3/envs/rknn/lib/python3.8/site-packages/cv2/mat_wrapper/__init__.py", line 39, in cv._registerMatType(Mat)AttributeError: partially initialized module 'cv2' has no attribute '_registerMatType' (most likely due to a circular import)

    解决方案

    卸载该库

    pip uninstall opencv-python

    重新安装该库

    pip install opencv-python

    如果还是不行,请再次卸载,重新安装其扩展版本

    pip install opencv-contrib-python
  3. 报错3

    Collecting opencv-pythonUsing cached opencv_python-4.9.0.80-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (20 kB)Requirement already satisfied: numpy>=1.17.0 in /root/anaconda3/envs/rknn/lib/python3.8/site-packages (from opencv-python) (1.19.5)WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/d9/64/7fdfb9386511cd6805451e012c537073a79a958a58795c4e602e538c388c/opencv_python-4.9.0.80-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whlWARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/d9/64/7fdfb9386511cd6805451e012c537073a79a958a58795c4e602e538c388c/opencv_python-4.9.0.80-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whlDownloading opencv_python-4.9.0.80-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (62.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.2/62.2 MB 17.8 MB/s eta 0:00:00Installing collected packages: opencv-pythonERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.rknn-toolkit 1.7.5 requires opencv-python==4.5.1.48, but you have opencv-python 4.9.0.80 which is incompatible.Successfully installed opencv-python-4.9.0.80WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

    根据上述报错信息,是我们重新安装opencv版本与rknn-toolkit工具包不兼容,此时再次卸载opencv-python包,安装新的包,此时我们指定rknn-toolkit工具包要求的版本。

    pip install opencv-python==4.5.1.48

    具体安装哪个版本,以你的控制台报错信息提示为主。

三、祝贺

如果最后执行命令后出现了如下信息行

---> Export RKNN modelConvert Done! outpath: ./model_cvt/RK1808_3399pro/best_RK1808_3399pro_u8.rknn

那么要恭喜,此时模型转换成功。

四、注意事项

  1. 时过境迁,当读者你看到此解决方案时,可能距离作者收笔此文相去已久,若未能解决你的问题,作者深感抱歉。当然,以上大部分问题我想,读者可以举一反三,完成符合技术标准的配置。