转换流程
Darknet model -> TensorFlow model
Darknet yolo模型包含两个文件:.cfg、.weights。其中cfg保存有模型的结构,weights保存有模型的参数。
TensorFlow yolo模型格式为.pb,其中包含所有推理所需的模型结构与参数。
tensorflow-yolo-v3中包含有转换代码,但是readme上显示仅支持到TensorFlow 1.11.0,估计2.0 +不行,况且OpenVINO也不支持2.0 +。
由于本机已经安装TensorFlow 2.1.0,故需要使用虚拟环境
创建虚拟环境
>>> git clone https://github.com/mystic123/tensorflow-yolo-v3.git
>>> cd tensorflow-yolo-v3
>>> mkdir env
>>> python3 -m venv env
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/microfat/Git/tensorflow-yolo-v3/env/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
如果出现如上错误则:
>>> export LC_ALL="en_US.UTF-8"
>>> export LC_CTYPE="en_US.UTF-8"
>>> sudo dpkg-reconfigure locales
>>> Generating locales (this might take a while)...
en_AG.UTF-8... done
en_AU.UTF-8... done
en_BW.UTF-8... done
en_CA.UTF-8... done
en_DK.UTF-8... done
en_GB.UTF-8... done
en_HK.UTF-8... done
en_IE.UTF-8... done
en_IL.UTF-8... done
en_IN.UTF-8... done
en_NG.UTF-8... done
en_NZ.UTF-8... done
en_PH.UTF-8... done
en_SG.UTF-8... done
en_US.UTF-8... done
en_ZA.UTF-8... done
en_ZM.UTF-8... done
en_ZW.UTF-8... done
zh_CN.UTF-8... done
zh_SG.UTF-8... done
Generation complete.
>>> sudo apt install python3-venv
>>> python3 -m venv env
# 激活虚拟环境
>>> source env/bin/activate
>>> python3 convert_weights_pb.py --class_names coco.names --data_format NHWC --weights_file ./weights/yolov3.weights
...
1186 ops written to frozen_darknet_yolov3_model.pb.
# 退出虚拟环境
>>> deactivate
参考:https://stackoverflow.com/a/39539571/7151777
TensorFlow model -> IR model
YOLO v3中使用Darknet-53作为特征提取器,而Darknet-53的最后使用三个分支分别在三个尺度上进行检测,这三个分支的最后一层必须使用YOLO Region层。
YOLO Region 层最先在Darknet框架中提出,但包括TensorFlow在内的其他框架并不支持Region作为单独一层,因而每个公开的YOLO v3模型使用更简单的层来替换它,这导致性能损失很大。因而,IR模型的思路就是用原始的Region层把YOLO v3中自定义的Region层完全替代,以恢复原有性能。
修改配置文件
>>> sudo vim /opt/intel/openvino/deployment_tools/model_optimizer/extensions/front/tf/yolo_v3.json
[
{
"id": "TFYOLOV3",
"match_kind": "general",
"custom_attributes": {
"classes": 80,
"anchors": [10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326],
"coords": 4,
"num": 9,
"mask":[6, 7, 8],
"entry_points": ["detector/yolo-v3/Reshape", "detector/yolo-v3/Reshape_4", "detector/yolo-v3/Reshape_8"]
}
}
]
- 不可修改参数
- id、match_kind
- 可修改参数
- classes、num、masks与要转的yolo模型配套的yolov3.cfg文件中的[yolo]标签下的参数一致,例如我的是:https://github.com/AlexeyAB/darknet/blob/8dc4833e7f51b0a75dad47a00cc0c6c711279868/cfg/yolov3.cfg#L693
- anchors 是可以修改的参数,但这里不修改
- entry_points是插入上面那些参数定义的Region层的位置,这里也不修改
需要特别注意的是,yolo_v3.json源文件中是
"masks":[[6, 7, 8], [3, 4, 5], [0, 1, 2]],
而如果列表中只有一个元素[6, 7, 8],则需要将masks改为mask,且最外面一层列表也不需要了
"mask":[6, 7, 8],
否则转换时会报错
[ ERROR ] Cannot infer shapes or values for node "detector/yolo-v3/Conv_22/BiasAdd/YoloRegion".
[ ERROR ] object of type 'int' has no len()
[ ERROR ]
[ ERROR ] It can happen due to bug in custom shape infer function <function RegionYoloOp.regionyolo_infer at 0x7f220a491d90>.
[ ERROR ] Or because the node inputs have incorrect values/shapes.
[ ERROR ] Or because input shapes are incorrect (embedded to the model or passed via --input_shape).
[ ERROR ] Run Model Optimizer with --log_level=DEBUG for more information.
[ ANALYSIS INFO ] Your model looks like YOLOv3 Model.
To generate the IR, provide TensorFlow YOLOv3 Model to the Model Optimizer with the following parameters:
--input_model <path_to_model>/yolo_v3.pb
--batch 1
--tensorflow_use_custom_operations_config <OPENVINO_INSTALL_DIR>/deployment_tools/model_optimizer/extensions/front/tf/yolo_v3.json
Detailed information about conversion of this model can be fount at
https://docs.openvinotoolkit.org/latest/_docs_MO_DG_prepare_model_convert_model_tf_specific_Convert_YOLO_From_Tensorflow.html
[ ERROR ] Exception occurred during running replacer "REPLACEMENT_ID" (<class 'extensions.middle.PartialInfer.PartialInfer'>): Stopped shape/value propagation at "detector/yolo-v3/Conv_22/BiasAdd/YoloRegion" node.
For more information please refer to Model Optimizer FAQ (https://docs.openvinotoolkit.org/latest/_docs_MO_DG_prepare_model_Model_Optimizer_FAQ.html), question #38.
转换
>>> cd /opt/intel/openvino/deployment_tools/model_optimizer
# 指定上文中转换成的.pb文件路径,指定上文yolo_v3.json文件路径
>>> sudo python3 mo_tf.py \
... --input_model /home/Git/tensorflow-yolo-v3/frozen_darknet_yolov3_model.pb \
... --tensorflow_use_custom_operations_config /opt/intel/openvino/deployment_tools/model_optimizer/extensions/front/tf/yolo_v3.json \
... --batch 1
[ WARNING ] Use of deprecated cli option --tensorflow_use_custom_operations_config detected. Option use in the following releases will be fatal. Please use --transformations_config cli option instead
Model Optimizer arguments:
Common parameters:
- Path to the Input Model: /home/microfat/Git/tensorflow-yolo-v3/frozen_darknet_yolov3_model.pb
- Path for generated IR: /opt/intel/openvino_2020.1.023/deployment_tools/model_optimizer/.
- IR output name: frozen_darknet_yolov3_model
- Log level: ERROR
- Batch: 1
- Input layers: Not specified, inherited from the model
- Output layers: Not specified, inherited from the model
- Input shapes: Not specified, inherited from the model
- Mean values: Not specified
- Scale values: Not specified
- Scale factor: Not specified
- Precision of IR: FP32
- Enable fusing: True
- Enable grouped convolutions fusing: True
- Move mean values to preprocess section: False
- Reverse input channels: False
TensorFlow specific parameters:
- Input model in text protobuf format: False
- Path to model dump for TensorBoard: None
- List of shared libraries with TensorFlow custom layers implementation: None
- Update the configuration file with input/output node names: None
- Use configuration file used to generate the model with Object Detection API: None
- Operations to offload: None
- Patterns to offload: None
- Use the config file: /opt/intel/openvino/deployment_tools/model_optimizer/extensions/front/tf/yolo_v3.json
Model Optimizer version: 2020.1.0-61-gd349c3ba4a
[ SUCCESS ] Generated IR version 10 model.
[ SUCCESS ] XML file: /opt/intel/openvino_2020.1.023/deployment_tools/model_optimizer/./frozen_darknet_yolov3_model.xml
[ SUCCESS ] BIN file: /opt/intel/openvino_2020.1.023/deployment_tools/model_optimizer/./frozen_darknet_yolov3_model.bin
[ SUCCESS ] Total execution time: 19.34 seconds.
[ SUCCESS ] Memory consumed: 1742 MB.
完成上面操作后会在该路径下生成三个文件
- frozen_darknet_yolov3_model.xml
- frozen_darknet_yolov3_model.bin
- frozen_darknet_yolov3_model.mapping
测试
这里使用opencv提供的Open Model Zoo中的示例代码:
https://github.com/opencv/open_model_zoo/blob/master/demos/python_demos/object_detection_demo_yolov3_async/object_detection_demo_yolov3_async.py
可以克隆整个仓库,也可以只保存这个文件
设置OpenVINO环境:
>>> source /opt/intel/openvino/bin/setupvars.sh
[setupvars.sh] OpenVINO environment initialized
这会将opencv切换为OpenVINO版并引入openvino的Python包
运行代码
>>> python3 object_detection_demo_yolov3_async.py \
... -m /opt/intel/openvino/deployment_tools/model_optimizer/frozen_darknet_yolov3_model.xml \
... -i /home/microfat/Git/raspberry_pi_object_detection/airbus.mp4 \
... -d CPU
其中:
- -d参数指定CPU、GPU、MYRIAD等设备,其中MYRIAD即Movidius\NCS 2
- -m参数指定上文转换的IR模型路径
- -i参数指定输入视频路径
除了上述几个参数外,还有很多参数,具体可以通过-h参数查看帮助或者看官方说明中的介绍:https://docs.openvinotoolkit.org/latest/_demos_python_demos_object_detection_demo_yolov3_async_README.html
运行结果: