3

I have a frozen inference graph stored in a .pb file, which was obtained from a trained Tensorflow model by the freeze_graph function.

Suppose, for simplicity, that I would like to change some of the sigmoid activations in the model to tanh activations (and let's not discuss whether this is a good idea).

How can this be done with access only to the frozen graph in the .pb file, and without the possibility to retrain the model?

I am aware of the Graph Editor library in tf.contrib, which should be able to do this kind of job, but I wasn't able to figure out a simple way to do this in the documentation.

Himanshu sharma
  • 7,487
  • 4
  • 42
  • 75
Daniel P
  • 199
  • 2
  • 15

4 Answers4

2

The solution is to use import_graph_def:

import tensorflow as tf
sess = tf.Session()

def load_graph(frozen_graph_filename):
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name='')
    return graph
graph_model = load_graph("frozen_inference_graph.pb")
graph_model_def = graph_model.as_graph_def()

graph_new = tf.Graph()
graph_new.as_default()
my_new_tensor = # whatever
tf.import_graph_def(graph_model_def, name='', input_map={"tensor_to_replace": my_new_tensor})
#do somthing with your new graph

Here I wrote a post about it

ixeption
  • 1,972
  • 1
  • 13
  • 19
1

Can you try this:

graph = load_graph(filename)
graph_def = graph.as_graph_def()
# if ReLu op is at node 161
graph_def.node[161].op="tanh"
tf.train.write_graph(graph_def, path2savfrozn, "altered_frozen.pb", False)

Please let know the if it works.

B Singh
  • 93
  • 1
  • 10
0

The *.pb file contains a SavedModel protocol buffer. You should be able to load it using a SavedModel loader. You can also inpsect it with the SavedModel CLI. The full documentation on SavedModels is here.

MatthewScarpino
  • 5,672
  • 5
  • 33
  • 47
  • My main question is how to replace e.g. the `sigmoid` activations with `tanh` activations in the existing graph. As for loading the model, I am using the `tf.import_graph_def` function, which works fine. – Daniel P Nov 19 '17 at 18:39
0

Something along these lines should work:

graph_def = tf.GraphDef()
with open('frozen_inference.pb', 'rb') as f:
    graph_def.ParseFromString(f.read())

with tf.Graph().as_default() as graph:
    importer.import_graph_def(graph_def, name='')

new_model = tf.GraphDef()

with tf.Session(graph=graph) as sess:

    for n in sess.graph_def.node:

        if n.op == 'Sigmoid':
            nn = new_model.node.add()
            nn.op = 'Tanh'
            nn.name = n.name
            for i in n.input:
                nn.input.extend([i])

        else:
            nn = new_model.node.add()
            nn.CopyFrom(n)
schil
  • 322
  • 2
  • 10