0

I am working with the ssd_mobilenet_v2_coco_2018_03_29 pretrained Tensorflow model. I want to change the input to fixed size, and save it under the saved_model.pb (I am using Neuron Compiler which require this format).

Here is how I change the input Tensor to fixed size:

graph = tf.Graph()
with graph.as_default():
    fixed_image_tensor = tf.placeholder(tf.uint8, shape=(None, 300, 300, 3), name='image_tensor')
    graph_def = tf.GraphDef()
    with tf.io.gfile.GFile(frozen_pb_file, 'rb') as f:
        serialized_graph = f.read()
        graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(graph_def, name='', input_map={"image_tensor:0": fixed_image_tensor})

And now I save the modified graph to saved_model.pb format by using tf.saved_model.simple_save:

image_tensor = graph.get_tensor_by_name('image_tensor:0')
boxes_tensor = graph.get_tensor_by_name('detection_boxes:0')
scores_tensor = graph.get_tensor_by_name('detection_scores:0')
classes_tensor = graph.get_tensor_by_name('detection_classes:0')
num_detections_tensor = graph.get_tensor_by_name('num_detections:0')

sess = tf.Session(graph=graph)

tf.saved_model.simple_save(
    session=sess,
    export_dir='model/',
    inputs={image_tensor.name: image_tensor},
    outputs={
        boxes_tensor.name: boxes_tensor,
        scores_tensor.name: scores_tensor,
        classes_tensor.name: classes_tensor,
        num_detections_tensor.name: num_detections_tensor
    }
)

The code create the following directory (variables is empty):

|-model/
|---variables/
|---saved_model.pb

The saved_model.pb is only 370 bytes, and must contains no actual information. I also try tf.saved_model.Builder like this and this, but still got the exact same result.

I can still use the sess for inference as usual with no problems. What did I do wrong? Are there any other approaches? I am using Tensorflow 1.15.0.

1 Answers1

0

A bit rearranged code, TF1.13, got 67MBytes *.pb file. Reloaded generated saved_file, input has yours dimensions and all listed outputs:

import tensorflow as tf

frozen_pb_file = "./ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb"

graph = tf.Graph()
    with graph.as_default():
    fixed_image_tensor = tf.placeholder(tf.uint8, shape=(None, 300, 300, 3), name='image_tensor')
    graph_def = tf.GraphDef()
    with tf.io.gfile.GFile(frozen_pb_file, 'rb') as f:
        serialized_graph = f.read()
        graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(graph_def, name='', input_map={"image_tensor:0": fixed_image_tensor})
    
    image_tensor = graph.get_tensor_by_name('image_tensor:0')
    boxes_tensor = graph.get_tensor_by_name('detection_boxes:0')
    scores_tensor = graph.get_tensor_by_name('detection_scores:0')
    classes_tensor = graph.get_tensor_by_name('detection_classes:0')
    num_detections_tensor = graph.get_tensor_by_name('num_detections:0')

    sess = tf.Session(graph=graph)

    file_writer = tf.summary.FileWriter(logdir='log', graph=graph)

    tf.saved_model.simple_save(
        session=sess,
        export_dir='model/',
        inputs={image_tensor.name: fixed_image_tensor},
        outputs={
            boxes_tensor.name: boxes_tensor,
            scores_tensor.name: scores_tensor,
            classes_tensor.name: classes_tensor,
            num_detections_tensor.name: num_detections_tensor
        }
    )
Alex K.
  • 842
  • 7
  • 17
  • I still got the 370 bytes pb file, and the 140MB log file from FileWriter. I tested with 2 machines, and with both TF1.15, and TF1.13. If you can get the actual pb file, then there must be something wrong with my environment setup. Can you give me the details of your machine, and the environment? – Tung Nguyen Sep 03 '20 at 03:33
  • I have env created with Anaconda, Python3.7, TF1.13. Have you looked your logs with tensorboard? Does created graph meets your expectations? – Alex K. Sep 03 '20 at 06:10