如何为Tensorflow修改冻结的pb文件中的张量值?

时间:2019-01-29 13:16:47

标签: python-3.x python-2.7 tensorflow protocol-buffers

现在我有一个冻结的tensorflow pb文件。然后,我修改了从指定图层名称conv1/weights获得的张量的一些值:

 import tensorflow as tf 
 from tensorflow.core.framework import graph_pb2
 from tensorflow.python.framework import tensor_util
 FLAGS = tf.app.flags.FLAGS
 tf.app.flags.DEFINE_string('input_frozen_graph_path',
                           '/tmp/model.pb', '')
 graph = tf.GraphDef()
 with tf.gfile.Open(FLAGS.input_frozen_graph_path, 'rb') as f:
     data = f.read()
     graph.ParseFromString(data)

 nodes = {node.name: index for index, node in enumerate(graph.node)}
 conv1_weights = tensor_util.MakeNdarray(graph.node[nodes["conv1/weights"]].attr['value'].tensor)
 #modify some values in conv1_weights

但是如何将修改后的conv1_weights值写回到graph.node的名称为conv1/weights的节点中? 任何人都可以给一些建议? 谢谢

0 个答案:

没有答案