Windows10 将 YOLOX模型转换为OpenVINO需要的IR文件
環境
- Windows:10
- Anaconda:2.0.4
- Python 3.7.10
- torch:1.7.0
- torchvision:0.8.0
- YOLOX:0.1.0
- OpenVINO 工具包 2020.2
OpenVINO安裝:https://blog.csdn.net/qq_44989881/article/details/119296663
下載 輕型模型:YOLOX-Nano
https://github.com/Megvii-BaseDetection/storage/releases/download/0.0.1/yolox_nano.pth
cd C:\Users\vvcat\Desktop\YOLOX-0.1.0
通過 -n 轉換標準 YOLOX 模型:
由以上為 export_onnx.py 文件提供的可選參數。
python tools/export_onnx.py --output-name yolox_nano.onnx --opset 10 -f exps/default/nano.py -c ./yolox_nano.pth- –output-name:輸出的模型名稱。
- -c:訓練好的模型
- -o:opset 版本,默認 11。但是,如果需要將 onnx 模型進一步轉換為OpenVINO,則輸入 opset 版本指定為 10。
(yolox) C:\Users\vvcat\Desktop\YOLOX-0.1.0>python tools/export_onnx.py --output-name yolox_nano.onnx --opset 10 -f exps/default/nano.py -c ./yolox_nano.pth
2021-09-05 20:09:49.403 | INFO | main:main:55 - args value: Namespace(ckpt=’./yolox_nano.pth’, exp_file=‘exps/default/nano.py’, experiment_name=None, input=‘images’, name=None, no_onnxsim=False, opset=10, opts=[], output=‘output’, output_name=‘yolox_nano.onnx’)
2021-09-05 20:09:49.623 | INFO | main:main:79 - loading checkpoint done.
H:\AnacondaNavigator\Anaconda\envs\yolox\lib\site-packages\torch\onnx\symbolic_helper.py:267: UserWarning: You are trying to export the model with onnx:Resize for ONNX opset version 10. This operator might cause results to not match the expected results by PyTorch.
ONNX’s Upsample/Resize operator did not match Pytorch’s Interpolation until opset 11. Attributes to determine how to transform the input were added in onnx:Resize in opset 11 to support Pytorch’s behavior (like coordinate_transformation_mode and nearest_mode).
We recommend using opset 11 and above for models using this operator.
“” + str(export_onnx_opset_version) + ". "
2021-09-05 20:09:56.399 | INFO | _main:main:89 - generated onnx model named yolox_nano.onnx
2021-09-05 20:09:57.052 | INFO | _main_:main:101 - generated simplified onnx model named yolox_nano.onnx
生成了 yolox_nano.onnx 文件
將 ONNX 轉換為 OpenVINO需要的IR文件
cd C:\Program Files (x86)\Intel\openvino_2021.2.185\bin setupvars.bat(yolox) C:\Program Files (x86)\Intel\openvino_2021.2.185\bin>setupvars.bat
Python 3.7.10
[setupvars.bat] OpenVINO environment initialized
轉換工具的安裝要求
cd C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer\install_prerequisites install_prerequisites_onnx.bat(yolox) C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer\install_prerequisites>install_prerequisites_onnx.bat
Python 3.7.10
ECHO 處于關閉狀態。
Requirement already satisfied: onnx>=1.1.2 in h:\anacondanavigator\anaconda\envs\yolox\lib\site-packages (from -r …\requirements_onnx.txt (line 1)) (1.8.1)
Requirement already satisfied: networkx>=1.11 in h:\anacondanavigator\anaconda\envs\yolox\lib\site-packages (from -r …\requirements_onnx.txt (line 2)) (2.6.2)
Requirement already satisfied: numpy>=1.14.0 in h:\anacondanavigator\anaconda\envs\yolox\lib\site-packages (from -r …\requirements_onnx.txt (line 3)) (1.21.2)
Collecting test-generator==0.1.1
Using cached test_generator-0.1.1-py2.py3-none-any.whl (5.5 kB)
Collecting defusedxml>=0.5.0
Using cached defusedxml-0.7.1-py2.py3-none-any.whl (25 kB)
Requirement already satisfied: six in h:\anacondanavigator\anaconda\envs\yolox\lib\site-packages (from test-generator==0.1.1->-r …\requirements_onnx.txt (line 4)) (1.16.0)
Requirement already satisfied: typing-extensions>=3.6.2.1 in h:\anacondanavigator\anaconda\envs\yolox\lib\site-packages (from onnx>=1.1.2->-r …\requirements_onnx.txt (line 1)) (3.10.0.0)
Requirement already satisfied: protobuf in h:\anacondanavigator\anaconda\envs\yolox\lib\site-packages (from onnx>=1.1.2->-r …\requirements_onnx.txt (line 1)) (3.17.3)
Installing collected packages: test-generator, defusedxml
Successfully installed defusedxml-0.7.1 test-generator-0.1.1
*****************************************************************************************
Warning: please expect that Model Optimizer conversion might be slow.
You can boost conversion speed by installing protobuf-*.egg located in the
“model-optimizer\install_prerequisites” folder or building protobuf library from sources.
For more information please refer to Model Optimizer FAQ, question #80.
然后轉換模型
cd C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer python mo.py --input_model C:\Users\vvcat\Desktop\YOLOX-0.1.0\yolox_nano.onnx --input_shape [1,3,416,416] --data_type FP16 --output_dir Converted_output(yolox) C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer>python mo.py --input_model C:\Users\vvcat\Desktop\YOLOX-0.1.0\yolox_nano.onnx --input_shape [1,3,416,416] --data_type FP16 --output_dir Converted_output
Model Optimizer arguments:
Common parameters:
- Path to the Input Model: C:\Users\vvcat\Desktop\YOLOX-0.1.0\yolox_nano.onnx
- Path for generated IR: C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer\Converted_output
- IR output name: yolox_nano
- Log level: ERROR
- Batch: Not specified, inherited from the model
- Input layers: Not specified, inherited from the model
- Output layers: Not specified, inherited from the model
- Input shapes: [1,3,416,416]
- Mean values: Not specified
- Scale values: Not specified
- Scale factor: Not specified
- Precision of IR: FP16
- Enable fusing: True
- Enable grouped convolutions fusing: True
- Move mean values to preprocess section: None
- Reverse input channels: False
ONNX specific parameters:
Model Optimizer version: 2021.2.0-1877-176bdf51370-releases/2021/2
[ SUCCESS ] Generated IR version 10 model.
[ SUCCESS ] XML file: C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer\Converted_output\yolox_nano.xml
[ SUCCESS ] BIN file: C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer\Converted_output\yolox_nano.bin
[ SUCCESS ] Total execution time: 33.50 seconds.
It’s been a while, check for a new version of Intel? Distribution of OpenVINO? toolkit here https://software.intel.com/content/www/us/en/develop/tools/openvino-toolkit/choose-download.html?cid=other&source=Prod&campid=ww_2021_bu_IOTG&content=upg_pro&medium=organic_uid_agjj or on the GitHub*
轉換成功后得到 xxx.bin,xxx.mapping,xxx.xml等三個文件。
我在我的YOLOX項目中創建了一個xml 文件夾, 然后我將xxx.bin,xxx.xml等兩個文件,放在里面。
OpenVINO測試轉換后的效果:
cd C:\Users\vvcat\Desktop\YOLOX-0.1.0格式如下:
python openvino_inference.py -m < XML_MODEL_PATH > -i < IMAGE_PATH > -o < OUTPUT_DIR > -s < SCORE_THR > -d < DEVICE >例如:
python ./demo/OpenVINO/python/openvino_inference.py -m ./xml/yolox_nano.xml -i ./assets/dog.jpg -o ./YOLOX_outputs -s 0.4 -d CPU注:openvino_inference.py中的 OpenVINO 采用的是同步推理。
(yolox) C:\Users\vvcat\Desktop\YOLOX-0.1.0>python ./demo/OpenVINO/python/openvino_inference.py -m ./xml/yolox_nano.xml -i ./assets/dog.jpg -o ./YOLOX_outputs -s 0.4 -d CPU
H:\AnacondaNavigator\Anaconda\envs\yolox\lib\site-packages\thop\utils.py:1: DeprecationWarning: Using or importing the ABCs from ‘collections’ instead of from ‘collections.abc’ is deprecated since Python 3.3,and in 3.9 it will stop working
from collections import Iterable
[ INFO ] Creating Inference Engine
[ INFO ] Reading the network: ./xml/yolox_nano.xml
[ INFO ] Configuring input and output blobs
[ INFO ] Loading the model to the plugin
[ INFO ] Starting inference in synchronous mode
效果如下:
出現問題一:在轉換模型的時候遇到以下問題
[ ERROR ] Failed to create directory C:\Program Files (x86)\Intel\openvino_2021.2.185\deployment_tools\model_optimizer\Converted_output. Permission denied!
For more information please refer to Model Optimizer FAQ, question #22. (https://docs.openvinotoolkit.org/latest/openvino_docs_MO_DG_prepare_model_Model_Optimizer_FAQ.html?question=22#question-22)
如果遇到 Windows 的權限問題,請用管理員的身份運行 Anaconda Prompt (Anaconda)
出現問題二:numpy 版本太低,導致無法使用 OpenVINO 運行,如果安裝大于 1.16.6 小于 1.20.1版本,也會出現 OpenVINO 版本過低類似的錯誤。
(yolox) C:\Users\vvcat\Desktop\YOLOX-0.1.0>pip install numpy==1.15.1
Collecting numpy==1.15.1
Downloading numpy-1.15.1-cp37-none-win_amd64.whl (13.5 MB)
|████████████████████████████████| 13.5 MB 92 kB/s
Installing collected packages: numpy
ERROR: 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.
scipy 1.7.1 requires numpy<1.23.0,>=1.16.5, but you have numpy 1.15.1 which is incompatible.
scikit-image 0.18.3 requires numpy>=1.16.5, but you have numpy 1.15.1 which is incompatible.
onnxruntime 1.8.0 requires numpy>=1.16.6, but you have numpy 1.15.1 which is incompatible.
onnx 1.8.1 requires numpy>=1.16.6, but you have numpy 1.15.1 which is incompatible.
matplotlib 3.4.3 requires numpy>=1.16, but you have numpy 1.15.1 which is incompatible.
建議安裝 numpy==1.20.1 版本以上,但是使用GPU作為設備運行 OpenVINO又會出現以下錯誤。
pip install numpy==1.20.3(yolox) C:\Users\vvcat\Desktop\YOLOX-0.1.0>python ./demo/OpenVINO/python/openvino_inference.py -m ./xml/yolox_nano.xml -i ./assets/dog.jpg -o ./YOLOX_outputs -s 0.4 -d GPU
H:\AnacondaNavigator\Anaconda\envs\yolox\lib\site-packages\thop\utils.py:1: DeprecationWarning: Using or importing the ABCs from ‘collections’ instead of from ‘collections.abc’ is deprecated since Python 3.3,and in 3.9 it will stop working
from collections import Iterable
[ INFO ] Creating Inference Engine
[ INFO ] Reading the network: ./xml/yolox_nano.xml
[ INFO ] Configuring input and output blobs
[ INFO ] Loading the model to the plugin
[ INFO ] Starting inference in synchronous mode
C:\Users\vvcat\Desktop\YOLOX-0.1.0\yolox\utils\demo_utils.py:94: RuntimeWarning: overflow encountered in exp
outputs[…, 2:4] = np.exp(outputs[…, 2:4]) * expanded_strides
Traceback (most recent call last):
File “./demo/OpenVINO/python/openvino_inference.py”, line 158, in
sys.exit(main())
File “./demo/OpenVINO/python/openvino_inference.py”, line 143, in main
boxes_xyxy /= ratio
TypeError: ufunc ‘true_divide’ output (typecode ‘d’) could not be coerced to provided output parameter (typecode ‘h’) according to the casting rule ‘‘same_kind’’
這個BUG暫時未找到解決的辦法,還在解決中,今天先到這吧,如果找到解決的辦法,會填上這個坑。
總結
以上是生活随笔為你收集整理的Windows10 将 YOLOX模型转换为OpenVINO需要的IR文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows10 yolox 安装
- 下一篇: Raspberry Pi 4B 部署 Y