Windows10 将 YOLOX模型转换为OpenVINO需要的IR文件

Posted ʚVVcatɞ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 模型:

parser = argparse.ArgumentParser("YOLOX onnx deploy")
    parser.add_argument(
        "--output-name", type=str, default="yolox.onnx", help="output name of models"
    )
    parser.add_argument(
        "--input", default="images", type=str, help="input node name of onnx model"
    )
    parser.add_argument(
        "--output", default="output", type=str, help="output node name of onnx model"
    )
    parser.add_argument(
        "-o", "--opset", default=11, type=int, help="onnx opset version"
    )
    parser.add_argument("--no-onnxsim", action="store_true", help="use onnxsim or not")
    parser.add_argument(
        "-f",
        "--exp_file",
        default=None,
        type=str,
        help="expriment description file",
    )
    parser.add_argument("-expn", "--experiment-name", type=str, default=None)
    parser.add_argument("-n", "--name", type=str, default=None, help="model name")
    parser.add_argument("-c", "--ckpt", default=None, type=str, help="ckpt path")
    parser.add_argument(
        "opts",
        help="Modify config options using the command-line",
        default=None,
        nargs=argparse.REMAINDER,
    )

由以上为 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 安装

[深度学习][原创]yolox快速训练自己数据集方法

Windows10下yolov8 tensorrt模型加速部署实战

YOLOX改进之模型轻量化(Lite)

我用YOLOX露了一手,记录一下模型部署优化及训练的实现全过程

时隔一年,终于还是对 YOLOX 之 Openvino 出手啦