本文旨在記錄如何將YOLO系列模型轉換IR數據格式,所有的YOLO模型最初實現框架是Darkent,而Darknet包含有兩個文件:
- .cfg 模型配置文件
- .weights 模型權重文件
Intel OpenVINO中並沒有直接封裝從darknet轉換IR的腳本,需要先將Darknet轉換Tensorflow,然後再有TensorFlow轉換至IR。
1. 轉換YOLOv3模型到Tensorflow*
這裏我們基於https://github.com/mystic123/tensorflow-yolo-v3 (commit ed60b90)將Darknet轉換至TensorFlow,步驟如下:
git clone https://github.com/mystic123/tensorflow-yolo-v3
cd tensorflow-yolo-v3
git checkout ed60b90
準備coco.names和yolov3.weights(yolov3 模型) or yolov3-tiny.weights(yolov3-tiny 模型)
運行轉換腳本:
# yolov3
python3 convert_weights_pb.py --class_names coco.names --data_format NHWC --weights_file yolov3.weights
# yolov3-tiny
python3 convert_weights_pb.py --class_names coco.names --data_format NHWC --weights_file yolov3-tiny.weights --tiny
需要注意,這裏默認輸入圖像的大小爲416*416, 若需要更換需指定輸入圖像大小(假設指定爲448)
# yolov3
python3 convert_weights_pb.py --class_names coco.names --data_format NHWC --weights_file yolov3.weights --size 448
2. 轉換YOLOv3 tensorflow模型到IR數據格式
轉換過程中使用的配置文件yolo_v3.json或yolo_v3_tiny.json(取決於模型),配置文件位於/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,
"masks":[[6, 7, 8], [3, 4, 5], [0, 1, 2]],
"entry_points": ["detector/yolo-v3/Reshape", "detector/yolo-v3/Reshape_4", "detector/yolo-v3/Reshape_8"]
}
}
]
id 和 match_kind 是參數,此處不需要進行修改;
custom_attributes 主要存儲了YOLOv3某些特殊的屬性:
classses, coords, num, masks, 這些按照Darknet的.cfg配置過來就好;
anchors: 是一個非必要參數,在模型推理時不使用,主要在解析Region 層的輸出;
entry_points 是一個節點名list,此處不需要修改;
生成YOLOv3 Tensorflow 模型的IR:
cd /opt/intel/openvino/deployment_tools/model_optimizer
python3 mo_tf.py
--input_model /opt/MODEL/frozen_darknet_yolov3_model.pb
--tensorflow_use_custom_operations_config extensions/front/tf/yolo_v3.json
--batch 1
生成YOLOv3-tiny Tensorflow 模型的IR:
cd /opt/intel/openvino/deployment_tools/model_optimizer
python3 mo_tf.py
--input_model /opt/MODEL/frozen_darknet_yolov3_model.pb
--tensorflow_use_custom_operations_config extensions/front/tf/yolo_v3_tiny.json
--batch 1
完,