Intel OpenVINO 模型轉換 — TensorFlow, Darknet YOLO, ONNX

李謦伊
謦伊的閱讀筆記
13 min readDec 3, 2020

在 Intel OpenVINO 介紹與安裝教學文章中有說到進行 Inference Engine 前會將訓練好的模型轉換為 IR ( xml 與 bin檔案),OpenVINO 有提供 mo.py 來轉換模型,其路徑位在 C:\Program Files (x86)\IntelSWTools\openvino_2020.3.194\deployment_tools\model_optimizer,支援的深度學習框架有 Caffe, TensorFlow, MXNet, ONNX, Kaldi

本篇將要來介紹如何將 TensorFlow, Darknet YOLOv3 及 ONNX 轉換為 IR,我使用的作業系統為 WIindows10, OpenVINO 2020.3.194 版本

TensorFlow to IR

🔹 轉換 TensorFlow 模型的步驟為:

  • 配置模型優化器
  • 凍結模型
  • 轉換模型以產生優化過的模型 (IR)

🔹 官方模型轉換: https://docs.openvinotoolkit.org/2020.3/_docs_MO_DG_prepare_model_convert_model_Convert_Model_From_TensorFlow.html

首先配置優化器,記得一定要使用系統管理員開啟 cmd,不然會有權限問題

$ cd C:\Program Files (x86)\IntelSWTools\openvino_2020.3.194\deployment_tools\model_optimizer\install_prerequisites$ install_prerequisites_tf.bat

📌 Supported Non-Frozen Topologies with Links to the Associated Slim Model Classification Download Files

官方介紹了三種 TensorFlow 模型,首先示範將使用 tf_slim 訓練的圖像分類未凍結模型 inception_resnet_v2 做轉換

# Git clone TensorFlow 模型
$ mkdir tf_models
$ git clone https://github.com/tensorflow/models.git tf_models
# 進入 slim 資料夾後刪除 build 檔案,安裝 tf_slim
$ cd tf_models\research\slim
$ python setup.py install
# 若無輸出錯誤訊息,則代表下載成功
$ python -c “import tf_slim as slim; eval = slim.evaluation.evaluate_once”
# Export inference_graph,會輸出 inception_resnet_v2_inference_graph.pb 及 label.txt$ python tf_models/research/slim/export_inference_graph.py --model_name inception_resnet_v2 --output_file inception_resnet_v2_inference_graph.pb

查看 inference_graph 的輸入跟輸出

$ cd C:\Program Files (x86)\IntelSWTools\openvino\deployment_tools\model_optimizer$ python mo\utils\summarize_graph.py --input_model C:\Users\joyle\Desktop\chingi\openvino\inception_resnet_v2_inference_graph.pb

下載 TensorFlow-Slim 未凍結模型 Inception ResNet v2

然後使用以下指令轉換模型,其中 --input_model 是輸出的 inference_graph, --input_checkpoint 是下載的未凍結模型, --mean_value, --scale 則查看官網上設定的值

$ python mo_tf.py --input_model C:\Users\joyle\Desktop\chingi\openvino\inception_resnet_v2_inference_graph.pb --input_checkpoint C:\Users\joyle\Desktop\chingi\openvino\inception_resnet_v2_2016_08_30.ckpt -b 1 --mean_value [127.5,127.5,127.5] --scale 127.5

📌 Supported Frozen Topologies from TensorFlow Object Detection Models Zoo

這邊示範 SSD MobileNet V1 COCO*

$ python mo_tf.py --input_model=C:\Users\joyle\Desktop\chingi\openvino\ssd_mobilenet_v1_coco_2018_01_28\frozen_inference_graph.pb --transformations_config extensions/front/tf/ssd_v2_support.json  --tensorflow_object_detection_api_pipeline_config C:\Users\joyle\Desktop\chingi\openvino\ssd_mobilenet_v1_coco_2018_01_28\pipeline.config --reverse_input_channels

📌 Supported Frozen Quantized Topologies

這邊示範 Mobilenet V1 0.25 128

$ python mo_tf.py --input_model C:\Users\joyle\Desktop\chingi\openvino\mobilenet_v1_0.25_192_quant\mobilenet_v1_0.25_192_quant_frozen.pb --input_shape [1,192,192,3]

Darknet YOLO to IR

🔹 轉換 YOLO 模型的步驟為:

  • 配置模型優化器
  • 先轉換為 TensorFlow 模型
  • 再將其轉換為優化過的模型 (IR)

首先配置優化器,記得一定要使用系統管理員開啟 cmd,不然會有權限問題。若以配置過 TensorFlow 優化器,則不必執行以下指令

$ cd C:\Program Files (x86)\IntelSWTools\openvino_2020.3.194\deployment_tools\model_optimizer\install_prerequisites$ install_prerequisites_tf.bat

🔹 這邊示範轉換 YOLOv3 模型,若要轉換 YOLOv1、YOLOv2 則使用https://github.com/thtrieu/darkflow#dependencies

🔹 若要轉換 YOLOv4 需要使用 OpenVINO 2020R4 版本才有支援,轉換流程使用 https://github.com/TNTWEN/OpenVINO-YOLOV4

🔹可以使用官方模型轉換的方法: https://docs.openvinotoolkit.org/2020.3/_docs_MO_DG_prepare_model_convert_model_tf_specific_Convert_YOLO_From_Tensorflow.html

  • 或是使用另一個 github 進行轉換
$ git clone https://github.com/PINTO0309/OpenVINO-YoloV3$ cd OpenVINO-YoloV3
  • 將 Darknet YOLOv3 轉換為 TensorFlow,可參考 script.txt,圖像大小預設為 416
#  yolov3.weights
$ python convert_weights_pb.py --class_names coco.names --weights_file weights/yolov3.weights --data_format NHWC --tiny --output_graph pbmodels/frozen_yolo_v3.pb
# yolov3-tiny.weights
$ python convert_weights_pb.py --class_names coco.names --weights_file weights/yolov3-tiny.weights --data_format NHWC --tiny --output_graph pbmodels/frozen_tiny_yolo_v3.pb

若要更改圖像大小,於 convert_weights_pb.py 更改

轉換完後會出現 frozen_tiny_yolo_v3.pb

  • 將剛轉換過的 pb 檔轉為 IR

❗ 一定要使用系統管理員開啟 cmd,不然會有權限問題

--input_model 為轉換過的 pb 檔路徑, yolov3.json 或 yolov3-tiny.json 的路徑位於 C:\Program Files (x86)\IntelSWTools\openvino_2020.3.194\deployment_tools\model_optimizer\extensions\front\tf 資料夾中,--data_type 為轉換模型的型態,預設為 FP32,若要使用 NCS2 須設定為 FP16

$ cd C:\Program Files (x86)\IntelSWTools\openvino_2020.3.194\deployment_tools\model_optimizer$ python mo_tf.py --input_model \path\to\OpenVINO-YoloV3\pbmodels\frozen_tiny_yolo_v3.pb --tensorflow_use_custom_operations_config extensions\front\tf\yolo_v3_tiny.json --data_type FP16 --batch 1

執行完畢後,會在 C:\Program Files (x86)\IntelSWTools\openvino_2020.3.194\deployment_tools\model_optimizer 資料夾中看到 IR 模型

ONNX to IR

🔹 轉換 ONNX 模型的步驟為:

  • 配置模型優化器
  • 轉換模型以產生優化過的模型 (IR)

🔹 官方模型轉換: https://docs.openvinotoolkit.org/2020.3/_docs_MO_DG_prepare_model_convert_model_Convert_Model_From_ONNX.html

🔹 若其他深度學習框架的模型要轉換為 ONNX 可參考官方 Tutorials: https://github.com/onnx/tutorials

首先配置優化器,記得一定要使用系統管理員開啟 cmd,不然會有權限問題

$ cd C:\Program Files (x86)\IntelSWTools\openvino_2020.3.194\deployment_tools\model_optimizer\install_prerequisites$ install_prerequisites_onnx.bat

📌 Supported Public ONNX Topologies

這邊示範 bvlc_alexnet,下載模型後執行以下指令,會在資料夾中輸出 IR 模型

$ cd ..$ python mo_onnx.py --input_model C:\Users\joyle\Downloads\bvlc_alexnet\model.onnx

📌 Supported Pytorch* Models via ONNX Conversion

在 R4 版本後,OpenVINO 支援 Pytorch 模型,不過要先將其轉換為 ONNX,再轉為 IR 檔

PyTorch to ONNX 官方: https://pytorch.org/docs/stable/onnx.html

執行以下程式碼: 下載 Pytorch pretrained model 並轉換為 ONNX,這邊示範 VGG16

執行完畢會得到 vgg16.onnx,接著再執行以下指令將其轉換為 IR

$ python mo_onnx.py --input_model <model_dir>\vgg16.onnx

OpenVINO 文章:

Intel OpenVINO介紹與安裝教學

Intel OpenVINO with NCS2

Intel OpenVINO — synchronous, asynchronous

Intel OpenVINO 模型轉換 — TensorFlow, Darknet YOLO, ONNX

--

--