为什么“sess.graph.get_all_collection_keys()”返回一个空集合?

时间:2017-09-20 09:04:32

标签: python tensorflow jupyter-notebook feature-extraction

抱歉,我在“stackoverflow”中搜索了所有答案,但没有获得令人满意的结果。 我正在学习一个初始v3功能抽象: codes on this site

当我插入一行:“print sess.graph.get_all_collection_keys()”到代码分段。打印结果为[]。但是使用:“pool3 = sess.graph.get_tensor_by_name('pool_3:0')”有正确的结果,为什么?

代码为:

import tensorflow as tf
import numpy as np

IMG_PATH = '/tmp/feature abstraction/panda.jpg'
MODEL_PATH = '/tmp/feature abstraction/classify_image_graph_def.pb'


inception_v3 = tf.gfile.FastGFile(MODEL_PATH, 'rb')
graph_def =tf.GraphDef()
graph_def.ParseFromString(inception_v3.read())
tf.import_graph_def(graph_def, name='')
layers_name=graph_def.ListFields()



with tf.Session() as sess:


#******

    print(sess.graph.get_all_collection_keys())
#*****



    pool3 = sess.graph.get_tensor_by_name('pool_3:0')
    #print sess.graph.get_all_collection_keys()
    print tf.get_default_graph().get_all_collection_keys()

    image_data = tf.gfile.FastGFile(IMG_PATH, 'rb').read()

    features = sess.run(pool3, {'DecodeJpeg/contents:0': image_data})

#******
    print(sess.graph.get_all_collection_keys())
#*******


    print features.shape
    print(np.squeeze(features))

输出为:

[]

[]

[]

(1, 1, 1, 2048)

[ 0.21214311  0.04288583  0.14220749 ...,  0.09034956  0.0148661
  0.13966754]

1 个答案:

答案 0 :(得分:0)

您应该尝试在MetaGraphDef表示中导出/导入图形(请参阅GraphDef和MetaGraphDef上的文档:https://www.tensorflow.org/versions/r1.3/programmers_guide/graphs)。 MetaGraphDef包含有关图表的更多信息(例如图表集的内容)。