为什么 "sess.graph.get_all_collection_keys()" return 是一个空集合?

why "sess.graph.get_all_collection_keys()" return a empty collection?

抱歉,我已经搜索了"Whosebug"中的所有答案,但没有得到满意的结果。 我正在学习一个 inception 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]

您应该尝试 export/import 您的图形以 MetaGraphDef 表示(参见有关 GraphDef 和 MetaGraphDef 的文档:https://www.tensorflow.org/versions/r1.3/programmers_guide/graphs)。 MetaGraphDef 包含有关您的图形的更多信息(例如图形集合的内容)。