2

I would like to modify/change operations using a frozen graph. For example, as below, modify ops from "upsample2D whose resizing method is ResizeNearestNeighbor" to 'upsample2D whose resizing method is ResizeBilinear"

with tf.device('/gpu:0'):
    with tf.gfile.GFile(filename, 'rb') as file:
        serialized_graph = file.read()
        graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(graph_def, name='')
        graph_replace = tf.contrib.graph_editor.graph_replace
        nodes = graph_def.node
        for node in nodes:
            if "ResizeNearestNeighbor" in node.name :
                print ("===========> ", node.name)
                node.op ="ResizeBilinear"
                # also need to change node name                   
        nodes = graph_def.node
        for node in nodes:
            print (node.name)
        tf.train.write_graph(graph_def, "./", name='modified.pb')

Actually, the above code is not working; I think it's due to unhashable type in nodedef; also, decoding error google.protobuf.message.DecodeError: Error parsing message when importing modified graph

I think the following methods may work, but any help for this?

graph_replace = tf.contrib.graph_editor.graph_replace
graph_replace(node, {node.xx: new_node.xx })

or,

tf.import_graph_def(graph_def, input_map={node: a new node})

Thanks

joshsuihn
  • 770
  • 1
  • 10
  • 25
  • Seemingly, tf.train.write_graph(graph_def, "./", name='modified.pb') is working, but the pb fails to be parsed and the file size is much larger than original size. – joshsuihn Jun 30 '18 at 22:52
  • Take a look at this one: https://stackoverflow.com/questions/47379766/replacing-a-node-in-a-frozen-tensorflow-model/53077609#53077609 – schil Nov 01 '18 at 22:21

2 Answers2

1

Your main code block is somewhat strange:

  • You create graph_replace but never use it.
  • You match on node.name. Names can be pretty much anything. You should probably match on node.op, which is the "type" of the operation. These names are fixed.
  • You change the the nodes type in node.op ="ResizeBilinear". This does not sound right. It is analogous to char a; boom = (uint64) a in C. You can't just change the "type" of something.

In general, modifying the GraphDef by hand is a bad idea. It is not part of the public interface and can change at any time.

Using graph_editor is probably the best approach. You can use the graph Transformer and override the transform_op_handler. See this test for basic example of using Transformer. You can base your handler on the default one that just copies the node as is. In case it helps, here is the place where this transformer is used.

iga
  • 3,571
  • 1
  • 12
  • 22
0

I have just faced a similar problem and found a solution. I don't know if it's possible just to rename the operation, so I guess you need to completly exchange the node.

To solve that, you need to define a new Operation like this:

output_tensor= tf.image.resize_images(input_tensor, [300, 300], method=tf.image.ResizeMethod.BILINEAR)

and then use

tf.import_graph_def(graph_model_def, name='', input_map={"existing_input_tensor": input_tensor}, return_elements=['data/inputs:0'])

Here is a more detailed explanation.

ixeption
  • 1,972
  • 1
  • 13
  • 19