Replacing a node in a frozen Tensorflow model

2019-08-07 11:32发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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)