This tutorial explains how to convert real-time object detection YOLOv1*, YOLOv2*, and YOLOv3* public models to the Intermediate Representation (IR). All YOLO* models are originally implemented in the DarkNet* framework and consist of two files:
.cfg
file with model configurations.weights
file with model weightsDepending on a YOLO model version, the Model Optimizer converts it differently:
On GitHub*, you can find several public versions of TensorFlow YOLOv3 model implementation. This tutorial explains how to convert YOLOv3 model from the https://github.com/mystic123/tensorflow-yolo-v3 repository (commit ed60b90) to IR , but the process is similar for other versions of TensorFlow YOLOv3 model.
Originally, YOLOv3 model includes feature extractor called Darknet-53
with three branches at the end that make detections at three different scales. These branches must end with the YOLO Region
layer.
Region
layer was first introduced in the DarkNet framework. Other frameworks, including TensorFlow, do not have the Region
implemented as a single layer, so every author of public YOLOv3 model creates it using simple layers. This badly affects performance. For this reason, the main idea of YOLOv3 model conversion to IR is to cut off these custom Region
-like parts of the model and complete the model with the Region
layers where required.
To dump TensorFlow model out of https://github.com/mystic123/tensorflow-yolo-v3 GitHub repository (commit ed60b90), follow the instructions below:
If you have YOLOv3 weights trained for an input image with the size different from 416 (320, 608 or your own), please provide the --size
key with the size of your image specified while running the converter. For example, run the following command for an image with size 608:
To solve the problems explained in the YOLOv3 architecture overview section, use the yolo_v3.json
or yolo_v3_tiny.json
(depending on a model) configuration file with custom operations located in the <OPENVINO_INSTALL_DIR>/deployment_tools/model_optimizer/extensions/front/tf
repository.
It consists of several attributes:
where:
id
and match_kind
are parameters that you cannot change.custom_attributes
is a parameter that stores all the YOLOv3 specific attributes:classes
, coords
, num
, and masks
are attributes that you should copy from the configuration file file that was used for model training. If you used DarkNet officially shared weights, you can use yolov3.cfg
or yolov3-tiny.cfg
configuration file from https://github.com/pjreddie/darknet/tree/master/cfg. Replace the default values in custom_attributes
with the parameters that follow the [yolo]
titles in the configuration file.anchors
is an optional parameter that is not used while inference of the model, but it used in a demo to parse Region
layer outputentry_points
is a node name list to cut off the model and append the Region layer with custom attributes specified above.To generate the IR of the YOLOv3 TensorFlow model, run:
To generate the IR of the YOLOv3-tiny TensorFlow model, run:
where:
--batch
defines shape of model input. In the example, --batch
is equal to 1, but you can also specify other integers larger than 1.--tensorflow_use_custom_operations_config
adds missing Region
layers to the model. In the IR, the Region
layer is named as RegionYolo
.OpenVINO™ toolkit provides a demo that uses YOLOv3 model. For more information, refer to Object Detection YOLO* V3 Demo, Async API Performance Showcase.
Before converting Choose a YOLOv1 or YOLOv2 model version that best suits your task. Download model configuration file and corresponding weight file:
cfg
directory, links to weight files are given in the README.md
file. The files from this repository are adatped for conversion to TensorFlow using DarkFlow.cfg
directory of the repository, links to weight files are given on the YOLOv1 and YOLOv2 websites.To convert DarkNet YOLOv1 and YOLOv2 models to IR, follow the next stepsg:
You need DarkFlow to convert YOLOv1 and YOLOv2 models to TensorFlow. To install DarkFlow:
README.md
file in the DarkFlow repository.To convert YOLOv1 or YOLOv2 model to TensorFlow, go to the root directory of the cloned DarkFlow repository and run the following command:
If the model was successfully converted, you can find the <model_name>.meta
and <model_name>.pb
files in built_graph
subdirectory of the cloned DarkFlow repository.
File <model_name>.pb
is a TensorFlow representation of the YOLO model.
Converted TensorFlow YOLO model is missing Region
layer and its parameters. Original YOLO Region
layer parameters are stored in the configuration <path_to_model>/<model_name>.cfg
file under the [region]
title.
To recreate the original model structure, use the corresponding yolo .json
configuration file with custom operations and Region
layer parameters when converting the model to the IR. This file is located in the <OPENVINO_INSTALL_DIR>/deployment_tools/model_optimizer/extensions/front/tf
directory.
If chosen model has specific values of this parameters, create another configuration file with custom operations and use it for conversion.
To generate the IR of the YOLOv1 model, provide TensorFlow YOLOv1 or YOLOv2 model to the Model Optimizer with the following parameters:
where:
--batch
defines shape of model input. In the example, --batch
is equal to 1, but you can also specify other integers larger than 1.--tensorflow_use_custom_operations_config
adds missing Region
layers to the model. In the IR, the Region
layer is named as RegionYolo
. For other applicable parameters, refer to Convert Model from TensorFlow.