Converting a TensorFlow* Model

A summary of the steps for optimizing and deploying a model that was trained with the TensorFlow* framework:

  1. Configure the Model Optimizer for TensorFlow* (TensorFlow was used to train your model).
  2. Freeze the TensorFlow model if your model is not already frozen or skip this step and use the instruction to a convert a non-frozen model.
  3. Convert a TensorFlow* model to produce an optimized Intermediate Representation (IR) of the model based on the trained network topology, weights, and biases values.
  4. Test the model in the Intermediate Representation format using the Inference Engine in the target environment via provided Inference Engine validation application or sample applications.
  5. Integrate the Inference Engine in your application to deploy the model in the target environment.

Supported Topologies

Supported Unfrozen Topologies with links to the associated Slim Model Classification download files.

Detailed information on how to convert models from the TensorFlow*-Slim Image Classification Model Library is available in the Converting TensorFlow*-Slim Image Classification Model Library Models chapter. The table below contains list of supported TensorFlow*-Slim Image Classification Model Library models and required mean/scale values. The mean values are specified as if the input image is read in BGR channels order layout like Inference Engine classification sample does.

Model Name Slim Model Checkpoint File --mean_values --scale
Inception v1 inception_v1_2016_08_28.tar.gz [127.5,127.5,127.5] 127.5
Inception v2 inception_v1_2016_08_28.tar.gz [127.5,127.5,127.5] 127.5
Inception v3 inception_v3_2016_08_28.tar.gz [127.5,127.5,127.5] 127.5
Inception V4 inception_v4_2016_09_09.tar.gz [127.5,127.5,127.5] 127.5
Inception ResNet v2 inception_resnet_v2_2016_08_30.tar.gz [127.5,127.5,127.5] 127.5
MobileNet v1 128 mobilenet_v1_0.25_128.tgz [127.5,127.5,127.5] 127.5
MobileNet v1 160 mobilenet_v1_0.5_160.tgz [127.5,127.5,127.5] 127.5
MobileNet v1 224 mobilenet_v1_1.0_224.tgz [127.5,127.5,127.5] 127.5
NasNet Large nasnet-a_large_04_10_2017.tar.gz [127.5,127.5,127.5] 127.5
NasNet Mobile nasnet-a_mobile_04_10_2017.tar.gz [127.5,127.5,127.5] 127.5
ResidualNet-50 v1 resnet_v1_50_2016_08_28.tar.gz [103.94,116.78,123.68] 1
ResidualNet-50 v2 resnet_v2_50_2017_04_14.tar.gz [103.94,116.78,123.68] 1
ResidualNet-101 v1 resnet_v1_101_2016_08_28.tar.gz [103.94,116.78,123.68] 1
ResidualNet-101 v2 resnet_v2_101_2017_04_14.tar.gz [103.94,116.78,123.68] 1
ResidualNet-152 v1 resnet_v1_152_2016_08_28.tar.gz [103.94,116.78,123.68] 1
ResidualNet-152 v2 resnet_v2_152_2017_04_14.tar.gz [103.94,116.78,123.68] 1
VGG-16 vgg_16_2016_08_28.tar.gz [103.94,116.78,123.68] 1
VGG-19 vgg_19_2016_08_28.tar.gz [103.94,116.78,123.68] 1

Supported Frozen Topologies from TensorFlow Object Detection models zoo

Detailed information on how to convert models from the Object Detection Models Zoo is available in the Converting TensorFlow Object Detection API Models chapter. The table below contains models from the Object Detection Models zoo that are supported.

Model Name TensorFlow Object Detection API Models (Frozen)
SSD MobileNet V1 COCO* ssd_mobilenet_v1_coco_2018_01_28.tar.gz
SSD MobileNet V1 0.75 Depth COCO ssd_mobilenet_v1_0.75_depth_300x300_coco14_sync_2018_07_03.tar.gz
SSD MobileNet V1 PPN COCO ssd_mobilenet_v1_ppn_shared_box_predictor_300x300_coco14_sync_2018_07_03.tar.gz
SSD MobileNet V1 FPN COCO ssd_mobilenet_v1_fpn_shared_box_predictor_640x640_coco14_sync_2018_07_03.tar.gz
SSD ResNet50 FPN COCO ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync_2018_07_03.tar.gz
SSD MobileNet V2 COCO ssd_mobilenet_v2_coco_2018_03_29.tar.gz
SSD Lite MobileNet V2 COCO ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz
SSD Inception V2 COCO ssd_inception_v2_coco_2018_01_28.tar.gz
RFCN ResNet 101 COCO rfcn_resnet101_coco_2018_01_28.tar.gz
Faster R-CNN Inception V2 COCO faster_rcnn_inception_v2_coco_2018_01_28.tar.gz
Faster R-CNN ResNet 50 COCO faster_rcnn_resnet50_coco_2018_01_28.tar.gz
Faster R-CNN ResNet 50 Low Proposals COCO faster_rcnn_resnet50_lowproposals_coco_2018_01_28.tar.gz
Faster R-CNN ResNet 101 COCO faster_rcnn_resnet101_coco_2018_01_28.tar.gz
Faster R-CNN ResNet 101 Low Proposals COCO faster_rcnn_resnet101_lowproposals_coco_2018_01_28.tar.gz
Faster R-CNN Inception ResNet V2 COCO faster_rcnn_inception_resnet_v2_atrous_coco_2018_01_28.tar.gz
Faster R-CNN Inception ResNet V2 Low Proposals COCO faster_rcnn_inception_resnet_v2_atrous_lowproposals_coco_2018_01_28.tar.gz
Faster R-CNN NasNet COCO faster_rcnn_nas_coco_2018_01_28.tar.gz
Faster R-CNN NasNet Low Proposals COCO faster_rcnn_nas_lowproposals_coco_2018_01_28.tar.gz
Mask R-CNN Inception ResNet V2 COCO mask_rcnn_inception_resnet_v2_atrous_coco_2018_01_28.tar.gz
Mask R-CNN Inception V2 COCO mask_rcnn_inception_v2_coco_2018_01_28.tar.gz
Mask R-CNN ResNet 101 COCO mask_rcnn_resnet101_atrous_coco_2018_01_28.tar.gz
Mask R-CNN ResNet 50 COCO mask_rcnn_resnet50_atrous_coco_2018_01_28.tar.gz
Faster R-CNN ResNet 101 Kitti* faster_rcnn_resnet101_kitti_2018_01_28.tar.gz
Faster R-CNN Inception ResNet V2 Open Images* faster_rcnn_inception_resnet_v2_atrous_oid_2018_01_28.tar.gz
Faster R-CNN Inception ResNet V2 Low Proposals Open Images* faster_rcnn_inception_resnet_v2_atrous_lowproposals_oid_2018_01_28.tar.gz
Faster R-CNN ResNet 101 AVA v2.1* faster_rcnn_resnet101_ava_v2.1_2018_04_30.tar.gz

NOTE: Faster and Mask R-CNNs are supported on CPU and GPU only with batch size 1.

Other supported topologies

Model Name Repository
ResNext Repo
DenseNet Repo
NCF Repo
lm_1b Repo
DeepSpeech Repo
A3C Repo
Unet Repo

Loading Non-Frozen Models to the Model Optimizer

There are three ways to store non-frozen TensorFlow models and load them to the Model Optimizer:

  1. Checkpoint:

    In this case, a model consists of two files:

    • inference_graph.pb or inference_graph.pbtxt
    • checkpoint_file.ckpt

    If you do not have an inference graph file, refer to Freezing Custom Models in Python.

    To convert such TensorFlow model:

    1. Go to the <INSTALL_DIR>/deployment_tools/model_optimizer directory
    2. Run the script with the path to the checkpoint file to convert a model:
    • If input model is in .pb format:
      python3 --input_model <INFERENCE_GRAPH>.pb --input_checkpoint <INPUT_CHECKPOINT>
    • If input model is in .pbtxt format:
      python3 --input_model <INFERENCE_GRAPH>.pbtxt --input_checkpoint <INPUT_CHECKPOINT> --input_model_is_text
  2. MetaGraph:

    In this case, a model consists of three or four files stored in the same directory:

    • model_name.meta
    • model_name.index
    • (digit part may vary)
    • checkpoint (optional)

    To convert such TensorFlow model:

    1. Go to the <INSTALL_DIR>/deployment_tools/model_optimizer directory
    2. Run the script with a path to the MetaGraph .meta file to convert a model:
      python3 --input_meta_graph <INPUT_META_GRAPH>.meta
  3. SavedModel:

    In this case, a model consists of a special directory:

    To convert such TensorFlow model:

    1. Go to the <INSTALL_DIR>/deployment_tools/model_optimizer directory
    2. Run the script with a path to the SavedModel directory to convert a model:
      python3 --saved_model_dir <SAVED_MODEL_DIRECTORY>

Freezing Custom Models in Python*

When a network is defined in Python code you have to create an inference graph file. Usually graphs are built in a form that allows model training. That means that all trainable parameters are represented as variables in the graph. To be able to use such graph with Model Optimizer such graph should be frozen. The graph is frozen and dumped to a file with the following code:

import tensorflow as tf
from tensorflow.python.framework import graph_io
frozen = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, ["name_of_the_output_node"])
graph_io.write_graph(frozen, './', 'inference_graph.pb', as_text=False)


Command Line Parameters

To convert a TensorFlow model:

  1. Go to the <INSTALL_DIR>/deployment_tools/model_optimizer directory
  2. Use the script to simply convert a model with the path to the input model .pb file:
    python3 --input_model <INPUT_MODEL>.pb

Two groups of parameters are available to convert your model:

NOTE: The Model Optimizer does not revert input channels from RGB to BGR by default, as it did in the 2017 R3 Beta release. Manually specify the command-line parameter to perform this reversion: --reverse_input_channels.

Using TensorFlow*-Specific Conversion Parameters

The following list provides the TensorFlow*-specific parameters.

TensorFlow*-specific parameters:
TensorFlow*: treat the input model file as a text
protobuf format. If not specified, the Model Optimizer
treats it as a binary file by default.
--input_checkpoint INPUT_CHECKPOINT
TensorFlow*: variables file to load.
--input_meta_graph INPUT_META_GRAPH
Tensorflow*: a file with a meta-graph of the model
before freezing
--saved_model_dir SAVED_MODEL_DIR
TensorFlow*: directory representing non frozen model
--saved_model_tags SAVED_MODEL_TAGS
Group of tag(s) of the MetaGraphDef to load, in string
format, separated by ','. For tag-set contains
multiple tags, all tags must be passed in.
TensorFlow*: automatically offload unsupported
operations to TensorFlow*
--tensorflow_subgraph_patterns TENSORFLOW_SUBGRAPH_PATTERNS
TensorFlow*: a list of comma separated patterns that
will be applied to TensorFlow* node names to infer a
part of the graph using TensorFlow*.
--tensorflow_operation_patterns TENSORFLOW_OPERATION_PATTERNS
TensorFlow*: a list of comma separated patterns that
will be applied to TensorFlow* node type (ops) to
infer these operations using TensorFlow*.
--tensorflow_custom_operations_config_update TENSORFLOW_CUSTOM_OPERATIONS_CONFIG_UPDATE
TensorFlow*: update the configuration file with node
name patterns with input/output nodes information.
--tensorflow_use_custom_operations_config TENSORFLOW_USE_CUSTOM_OPERATIONS_CONFIG
TensorFlow*: use the configuration file with custom
operation description.
--tensorflow_object_detection_api_pipeline_config TENSORFLOW_OBJECT_DETECTION_API_PIPELINE_CONFIG
TensorFlow*: path to the pipeline configuration file
used to generate model created with help of Object
Detection API.
--tensorboard_logdir TENSORBOARD_LOGDIR
TensorFlow*: dump the input graph to a given directory
that should be used with TensorBoard.
--tensorflow_custom_layer_libraries TENSORFLOW_CUSTOM_LAYER_LIBRARIES
TensorFlow*: comma separated list of shared libraries
with TensorFlow* custom operations implementation.
Disables default translation from NHWC to NCHW

NOTE: Models produces with TensorFlow* usually have not fully defined shapes (contain -1 in some dimensions). It is necessary to pass explicit shape for the input using command line parameter --input_shape or -b to override just batch dimension. If the shape is fully defined, then there is no need to specify either -b or --input_shape options.

Command-Line Interface (CLI) Examples Using TensorFlow*-Specific Parameters

Custom Layer Definition

Internally, when you run the Model Optimizer, it loads the model, goes through the topology, and tries to find each layer type in a list of known layers. Custom layers are layers that are not included in the list of known layers. If your topology contains any layers that are not in this list of known layers, the Model Optimizer classifies them as custom.

See Custom Layers in the Model Optimizer for information about:

Supported TensorFlow* Layers

Refer to Supported Framework Layers for the list of supported standard layers.

Frequently Asked Questions (FAQ)

The Model Optimizer provides explanatory messages if it is unable to run to completion due to issues like typographical errors, incorrectly used options, or other issues. The message describes the potential cause of the problem and gives a link to the Model Optimizer FAQ. The FAQ has instructions on how to resolve most issues. The FAQ also includes links to relevant sections in the Model Optimizer Developer Guide to help you understand what went wrong.


In this document, you learned: