如何从我的模型创建classify_image_graph_def.pb?

时间:2016-09-11 15:20:25

标签: tensorflow

我使用this repo将我的caffe模型转换为tensorflow。我以2个文件结束:一个是python类,第二个是带有模型权重的npy文件。

但是我想生成一个文件,格式与此one相同(此文件名为classify_image_graph_def.pb,可用于在任何测试图像上转发网络)。

我对此格式感兴趣,因为这是脚本quantize_graph.py所需的格式。

1 个答案:

答案 0 :(得分:0)

正确的方法是将图中的所有变量转换为常量。这可以使用freeze_graph tool来完成。 但由于此工具不是运行时张量流库的一部分,因此发现使用以下行更方便(无需构建freeze_graph):

sess = tf.InteractiveSession()

### create some graph here ###
##############################

graph_def = sess.graph.as_graph_def()
output_node_names = "output0,output1" # put the names of the output nodes here

# freeze all parameters and save
output_graph_def = graph_util.convert_variables_to_constants(
        sess, graph_def, output_node_names.split(","))
with tf.gfile.GFile(output_graph_file, "wb") as f:
    f.write(output_graph_def.SerializeToString())