Tensorflow返回ValueError:无法创建内容大于2GB的张量原型

时间:2019-07-25 11:00:18

标签: python tensorflow

 def loadData():
      images_dir = os.path.join(current_dir, 'image_data')
      images = []
      for each in os.listdir(images_dir):
          images.append(os.path.join(images_dir,each))
      all_images = tf.convert_to_tensor(images, dtype = tf.string)
      images_batch = tf.train.shuffle_batch(
                                [all_images], batch_size = BATCH_SIZE)
      return images_batch

返回

ValueError: Cannot create a tensor proto whose content is larger than 2GB.

我正在尝试加载约11GB的图像。我该如何克服这些限制?

编辑:可能重复: 建议将输出类拆分为多个操作,最后将它们连接起来,但是我没有多个可以拆分的类。

Edit2: 解决此问题的方法建议使用占位符。所以现在我不确定在那种情况下谁使用占位符,以及在哪里可以将图像数组提供给张量流。

这是我的训练函数的最低版本,以显示如何初始化会话。

def train():
     images_batch = loadData()
     sess = tf.Session()
     saver = tf.train.Saver()
     sess.run(tf.global_variables_initializer())
     sess.run(tf.local_variables_initializer())
     for i in range(EPOCH):
            train_image = sess.run(image_batch)

1 个答案:

答案 0 :(得分:1)

使用convert_to_tensor具有将图像添加到计算图的意外效果,其硬限制为2GB。如果达到此限制,则应重新考虑如何在训练过程中提供图像。

我们在TensorFlow中已经有一个简单的解决方案,只需在tf.placeholder中使用占位符(feed_dict)和session.run。这种情况下的唯一缺点是,您必须手动生成批量数据。