Tensorflow 2: Add operations to input and ouput of frozen graph

Viewed 54

When I load a frozen graph from disc, it already contains a placeholder for the input (input_placeholder_name="main_input:0")

    graph = tf.Graph()
    graph.as_default()
    sess = tf.compat.v1.Session()

    with tf.io.gfile.GFile(model_filepath, "rb") as f:
        graph_def = tf.compat.v1.GraphDef()
        loaded = graph_def.ParseFromString(f.read())

However, I'd like to add my own input normalization to the graph:

img = tf.compat.v1.placeholder(tf.float32, shape=[1, 384, 256, 3], name="img")


normalized_input = tf.divide(img_bgr, 255.0)
normalized_input_b = tf.subtract(normalized_input[:, :, :, 0], tf.constant(0.406))
normalized_input_g = tf.subtract(normalized_input[:, :, :, 1], tf.constant(0.456))
normalized_input_r = tf.subtract(normalized_input[:, :, :, 2], tf.constant(0.485))

normalized_input_b = tf.expand_dims(normalized_input_b, axis=-1)
normalized_input_g = tf.expand_dims(normalized_input_g, axis=-1)
normalized_input_r = tf.expand_dims(normalized_input_r, axis=-1)

normalized_input_b = tf.divide(normalized_input_b, tf.constant(0.225))
normalized_input_g = tf.divide(normalized_input_g, tf.constant(0.224))
normalized_input_r = tf.divide(normalized_input_r, tf.constant(0.229))

input_data = tf.concat((normalized_input_r,
                        normalized_input_g,
                        normalized_input_b),
                       axis=3)

I try to combine the above described input normalization with the existing graph using import_graph_def:

tf.import_graph_def(graph_def, {input_paceholder_name: input_data})

When I try to run the new graph on some example input, I get an error of there being two placeholders to feed

sess.run(output_tensor, feed_dict={img: data}) 

I had something similar working for TF1 already (there is also a stackoverflow answer for that case), however I'm forced to use TF2 now and I'm really confused why it is not working by simply adding tf.compat.v1 and disable_eager_execution everywhere.

0 Answers
Related