Tensorflow: delete nodes from graph

前端 未结 2 676
一整个雨季
一整个雨季 2021-01-28 04:27

I\'m trying to delete some nodes from graph and save it in .pb

Only needed nodes can be added to new mod_graph_def graph, but the problem that graph still h

2条回答
  •  抹茶落季
    2021-01-28 05:00

    def delete_ops_from_graph():
        with open(input_model_filepath, 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
    
        # Delete nodes
        nodes = []
        for node in graph_def.node:
            if 'Neg' in node.name:
                print('Drop', node.name)
            else:
                nodes.append(node)
    
        mod_graph_def = tf.GraphDef()
        mod_graph_def.node.extend(nodes)
    
        # Delete references to deleted nodes
        for node in mod_graph_def.node:
            inp_names = []
            for inp in node.input:
                if 'Neg' in inp:
                    pass
                else:
                    inp_names.append(inp)
    
            del node.input[:]
            node.input.extend(inp_names)
    
        with open(output_model_filepath, 'wb') as f:
            f.write(mod_graph_def.SerializeToString())
    

提交回复
热议问题