export_inference_graph.py 4.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

r"""Tool to export an object detection model for inference.

Prepares an object detection tensorflow graph for inference using model
19
20
21
configuration and an optional trained checkpoint. Outputs inference
graph, associated checkpoint files, a frozen inference graph and a
SavedModel (https://tensorflow.github.io/serving/serving_basic.html).
22

23
The inference graph contains one of three input nodes depending on the user
24
25
specified option.
  * `image_tensor`: Accepts a uint8 4-D tensor of shape [1, None, None, 3]
26
27
  * `encoded_image_string_tensor`: Accepts a scalar string tensor of encoded PNG
    or JPEG image.
28
29
30
  * `tf_example`: Accepts a serialized TFExample proto. The batch size in this
    case is always 1.

31
32
and the following output nodes returned by the model.postprocess(..):
  * `num_detections`: Outputs float32 tensors of the form [batch]
33
      that specifies the number of valid boxes per image in the batch.
34
  * `detection_boxes`: Outputs float32 tensors of the form
35
      [batch, num_boxes, 4] containing detected boxes.
36
  * `detection_scores`: Outputs float32 tensors of the form
37
38
39
      [batch, num_boxes] containing class scores for the detections.
  * `detection_classes`: Outputs float32 tensors of the form
      [batch, num_boxes] containing classes for the detections.
40
41
42
43
  * `detection_masks`: Outputs float32 tensors of the form
      [batch, num_boxes, mask_height, mask_width] containing predicted instance
      masks for each box if its present in the dictionary of postprocessed
      tensors returned by the model.
44

45
46
47
48
Notes:
 * Currently `batch` is always 1, but we will support `batch` > 1 in the future.
 * This tool uses `use_moving_averages` from eval_config to decide which
   weights to freeze.
49
50
51
52
53
54

Example Usage:
--------------
python export_inference_graph \
    --input_type image_tensor \
    --pipeline_config_path path/to/ssd_inception_v2.config \
55
56
57
58
59
60
61
62
63
64
65
66
    --trained_checkpoint_prefix path/to/model.ckpt \
    --output_directory path/to/exported_model_directory

The expected output would be in the directory
path/to/exported_model_directory (which is created if it does not exist)
with contents:
 - graph.pbtxt
 - model.ckpt.data-00000-of-00001
 - model.ckpt.info
 - model.ckpt.meta
 - frozen_inference_graph.pb
 + saved_model (a directory)
67
68
69
70
71
72
73
74
75
76
"""
import tensorflow as tf
from google.protobuf import text_format
from object_detection import exporter
from object_detection.protos import pipeline_pb2

slim = tf.contrib.slim
flags = tf.app.flags

flags.DEFINE_string('input_type', 'image_tensor', 'Type of input node. Can be '
77
78
                    'one of [`image_tensor`, `encoded_image_string_tensor`, '
                    '`tf_example`]')
79
flags.DEFINE_string('pipeline_config_path', None,
80
81
                    'Path to a pipeline_pb2.TrainEvalPipelineConfig config '
                    'file.')
82
83
84
85
flags.DEFINE_string('trained_checkpoint_prefix', None,
                    'Path to trained checkpoint, typically of the form '
                    'path/to/model.ckpt')
flags.DEFINE_string('output_directory', None, 'Path to write outputs.')
86

87
88
89
tf.app.flags.MarkFlagAsRequired('pipeline_config_path')
tf.app.flags.MarkFlagAsRequired('trained_checkpoint_prefix')
tf.app.flags.MarkFlagAsRequired('output_directory')
90
91
92
93
94
95
96
FLAGS = flags.FLAGS


def main(_):
  pipeline_config = pipeline_pb2.TrainEvalPipelineConfig()
  with tf.gfile.GFile(FLAGS.pipeline_config_path, 'r') as f:
    text_format.Merge(f.read(), pipeline_config)
97
98
99
  exporter.export_inference_graph(
      FLAGS.input_type, pipeline_config, FLAGS.trained_checkpoint_prefix,
      FLAGS.output_directory)
100
101
102
103


if __name__ == '__main__':
  tf.app.run()