【问题标题】:Tensorflow: modifying node operation using a frozen graph (graph edit)Tensorflow:使用冻结图修改节点操作(图编辑)
【发布时间】:2020-01-31 23:27:30
【问题描述】:

我想使用冻结图修改/更改操作。 例如,如下,将ops从"upsample2D,其大小调整方法为ResizeNearestNeighbor"修改为'upsample2D,其调整大小方法为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')

其实上面的代码是不行的;我认为这是由于 nodedef 中的不可散列类型;另外,解码错误 google.protobuf.message.DecodeError: Error parsing message when importing modified graph

我认为以下方法可能有效,但对此有何帮助?

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

或者,

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

谢谢

【问题讨论】:

标签: tensorflow graph edit operation


【解决方案1】:

你的主要代码块有点奇怪:

  • 您创建了graph_replace,但从不使用它。
  • 您在node.name 上匹配。名字几乎可以是任何东西。您可能应该匹配node.op,这是操作的“类型”。这些名称是固定的。
  • 您更改了node.op ="ResizeBilinear" 中的节点类型。这听起来不对。它类似于 C 中的 char a; boom = (uint64) a。您不能只更改某物的“类型”。

一般来说,手动修改 GraphDef 是个坏主意。它不是公共接口的一部分,可以随时更改。

使用graph_editor 可能是最好的方法。您可以使用图形Transformer 并覆盖transform_op_handler。有关使用 Transformer 的基本示例,请参阅此 test。您可以将您的处理程序基于默认的one,它只是按原样复制节点。如果有帮助,这里是使用此转换器的place

【讨论】:

    【解决方案2】:

    我刚刚遇到了类似的问题并找到了解决方案。我不知道是否可以仅重命名操作,所以我想您需要完全交换节点。

    要解决这个问题,您需要像这样定义一个新的操作:

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

    然后使用

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

    Here是更详细的解释。

    【讨论】:

      猜你喜欢
      • 2018-09-14
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多