Tensorflow会话-ValueError:GraphDef不能大于2GB

时间:2018-09-28 21:42:48

标签: python tensorflow transfer-learning

我正在尝试遍历数据集批次,并在预训练的模型上进行推断。我创建了一个会话并按如下方式加载了模型:

import numpy as np

sess = tf.Session()

saver = tf.train.import_meta_graph('model_resnet/imagenet.ckpt.meta')
saver.restore(sess, "model_resnet/imagenet.ckpt")

# To view the graph in tensorboard:
summary_writer = tf.summary.FileWriter("/tmp/tensorflow_logdir", graph=tf.get_default_graph())

# To retrieve outputs of layer while inferring
def getActivations(layer,stimuli):
    units = sess.run(layer,feed_dict={"Placeholder_:0": stimuli, keep_prob:1.0})

# Convert to TF Dataset
dataset_train = tf.data.Dataset.from_tensor_slices((X_train, y_train))
dataset_test = tf.data.Dataset.from_tensor_slices((X_test, y_test))

# Create batches
dataset = dataset_train.batch(32)

# Iterator to iterate over images in batch
iterator = dataset.make_one_shot_iterator()

next_element = iterator.get_next()

try:
    getActivations("resnet/pool:0",sess.run(next_element[1]))
except tf.errors.OutOfRangeError:
    print("End of dataset")  # ==> "End of dataset"

我收到此错误:

  

ValueError:GraphDef不能大于2GB。

我可能会误解图形的确切含义。我不明白为什么对32张图像进行一次迭代会导致图形中的扩展。我的操作是否添加到预训练模型图中?根据我到目前为止所看到的,使用add或tf.'function_name'向TF Graph添加一个操作是正确的吗?

任何帮助或示例的指针,将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:0)

我在这里浏览了类似的问题,并应用了一些方法来消除错误:

  • 使用占位符加载数据:

    features_placeholder = tf.placeholder(X_train.dtype, X_train.shape)
    labels_placeholder = tf.placeholder(y_train.dtype, y_train.shape)
    dataset = tf.data.Dataset.from_tensor_slices((features_placeholder, labels_placeholder))
    
  • 用于创建会话并使用以下方法加载图形:

    with tf.Session() as sess:
    
        initialize_iterator(sess, iterator, X_train, y_train)
        next_element = iterator.get_next()
    
        load_graph(sess)