|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +from __future__ import division |
| 20 | +from __future__ import print_function |
| 21 | +import os |
| 22 | + |
| 23 | +import tensorflow as tf |
| 24 | + |
| 25 | +import horovod.tensorflow as hvd |
| 26 | +from model import resnet |
| 27 | + |
| 28 | +tf.app.flags.DEFINE_string( |
| 29 | + 'model_name', 'resnet50', 'The name of the architecture to save. The default name was being ' |
| 30 | + 'used to train the model') |
| 31 | + |
| 32 | +tf.app.flags.DEFINE_integer( |
| 33 | + 'image_size', 224, |
| 34 | + 'The image size to use, otherwise use the model default_image_size.') |
| 35 | + |
| 36 | +tf.app.flags.DEFINE_integer( |
| 37 | + 'num_classes', 1001, |
| 38 | + 'The number of classes to predict.') |
| 39 | + |
| 40 | +tf.app.flags.DEFINE_integer( |
| 41 | + 'batch_size', None, |
| 42 | + 'Batch size for the exported model. Defaulted to "None" so batch size can ' |
| 43 | + 'be specified at model runtime.') |
| 44 | + |
| 45 | + |
| 46 | +tf.app.flags.DEFINE_string('input_format', 'NCHW', |
| 47 | + 'The dataformat used by the layers in the model') |
| 48 | + |
| 49 | +tf.app.flags.DEFINE_string('compute_format', 'NCHW', |
| 50 | + 'The dataformat used by the layers in the model') |
| 51 | + |
| 52 | +tf.app.flags.DEFINE_string('checkpoint', '', |
| 53 | + 'The trained model checkpoint.') |
| 54 | + |
| 55 | +tf.app.flags.DEFINE_string( |
| 56 | + 'output_file', '', 'Where to save the resulting file to.') |
| 57 | + |
| 58 | +tf.app.flags.DEFINE_bool( |
| 59 | + 'quantize', False, 'whether to use quantized graph or not.') |
| 60 | + |
| 61 | +tf.app.flags.DEFINE_bool( |
| 62 | + 'symmetric', False, 'Using symmetric quantization or not.') |
| 63 | + |
| 64 | + |
| 65 | +tf.app.flags.DEFINE_bool( |
| 66 | + 'use_qdq', False, 'Use quantize and dequantize op instead of fake quant op') |
| 67 | + |
| 68 | +tf.app.flags.DEFINE_bool( |
| 69 | + 'use_final_conv', False, 'whether to use quantized graph or not.') |
| 70 | + |
| 71 | +tf.app.flags.DEFINE_bool('write_text_graphdef', False, |
| 72 | + 'Whether to write a text version of graphdef.') |
| 73 | + |
| 74 | +FLAGS = tf.app.flags.FLAGS |
| 75 | + |
| 76 | + |
| 77 | +def main(_): |
| 78 | + |
| 79 | + # Initialize Horovod (TODO: Remove dependency of horovod for freezing graphs) |
| 80 | + hvd.init() |
| 81 | + |
| 82 | + if not FLAGS.output_file: |
| 83 | + raise ValueError('You must supply the path to save to with --output_file') |
| 84 | + |
| 85 | + tf.logging.set_verbosity(tf.logging.INFO) |
| 86 | + with tf.Graph().as_default() as graph: |
| 87 | + if FLAGS.input_format=='NCHW': |
| 88 | + input_shape = [FLAGS.batch_size, 3, FLAGS.image_size, FLAGS.image_size] |
| 89 | + else: |
| 90 | + input_shape = [FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size, 3] |
| 91 | + input_images = tf.placeholder(name='input', dtype=tf.float32, shape=input_shape) |
| 92 | + |
| 93 | + resnet50_config = resnet.model_architectures[FLAGS.model_name] |
| 94 | + network = resnet.ResnetModel(FLAGS.model_name, |
| 95 | + FLAGS.num_classes, |
| 96 | + resnet50_config['layers'], |
| 97 | + resnet50_config['widths'], |
| 98 | + resnet50_config['expansions'], |
| 99 | + FLAGS.compute_format, |
| 100 | + FLAGS.input_format) |
| 101 | + probs, logits = network.build_model( |
| 102 | + input_images, |
| 103 | + training=False, |
| 104 | + reuse=False, |
| 105 | + use_final_conv=FLAGS.use_final_conv) |
| 106 | + |
| 107 | + if FLAGS.quantize: |
| 108 | + tf.contrib.quantize.experimental_create_eval_graph(symmetric=FLAGS.symmetric, |
| 109 | + use_qdq=FLAGS.use_qdq) |
| 110 | + |
| 111 | + # Define the saver and restore the checkpoint |
| 112 | + saver = tf.train.Saver() |
| 113 | + with tf.Session() as sess: |
| 114 | + if FLAGS.checkpoint: |
| 115 | + saver.restore(sess, FLAGS.checkpoint) |
| 116 | + else: |
| 117 | + sess.run(tf.global_variables_initializer()) |
| 118 | + graph_def = graph.as_graph_def() |
| 119 | + frozen_graph_def = tf.graph_util.convert_variables_to_constants(sess, graph_def, [probs.op.name]) |
| 120 | + |
| 121 | + # Write out the frozen graph |
| 122 | + tf.io.write_graph( |
| 123 | + frozen_graph_def, |
| 124 | + os.path.dirname(FLAGS.output_file), |
| 125 | + os.path.basename(FLAGS.output_file), |
| 126 | + as_text=FLAGS.write_text_graphdef) |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == '__main__': |
| 130 | + tf.app.run() |
0 commit comments