【问题标题】:Tensorflow: Create a batch from a list of image tensorsTensorflow:从图像张量列表中创建一个批次
【发布时间】:2017-02-20 21:33:14
【问题描述】:

我有一个函数get_image(...) 对我的输入图像执行预处理。我在这样的列表中收集属于同一批次的所有图像:

batch = [get_image(file_path) for file_path in batch_files]

现在我想将此列表转换为一个张量,其中第一个维度是批量大小维度,这样我就可以将它提供给我的网络的输入占位符。

_ = self.sess.run([loss],feed_dict={ input_placeholder: batch })

知道我该怎么做吗?

            batch_concat = tf.placeholder(shape=[None] + self.image_shape, dtype=tf.float32) 
            for i in xrange(0,self.batch_size):
                if i == 0:
                    tmp_batch = tf.expand_dims(batch[i], 0)
                    batch_concat = tmp_batch
                else:
                    tmp_batch = tf.expand_dims(batch[i], 0)                        
                    batch_concat = tf.concat(0, [batch_concat, tmp_batch])

当我尝试连接所有张量时,出现以下错误:

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, or numpy ndarrays.

那么在将张量输入网络之前将其转换回 numpy 数组就足够了?

【问题讨论】:

  • 不清楚你想在这里做什么。为什么不将包含图像的 python 列表作为 input_batch 传递? tensorflow 会为你转换它
  • 我想使用tf提供的预处理功能。否则我将不得不在 python 中重写所有内容......

标签: tensorflow


【解决方案1】:

在 TF r1.1 包中已替换为 tf.stack

【讨论】:

    【解决方案2】:

    您可以使用tf.pack 将张量列表打包成批处理。

    image_list = [get_image(file_path) for file_path in batch_files]
    image_batch = tf.pack(image_list)
    

    您还可以使用 tf.concat 沿第一个维度连接列表并对其进行整形。

    【讨论】:

      【解决方案3】:

      这里的问题是在 feed_dict 中使用张量作为值。假设batch 是您的批处理张量,您为什么不使用batch 而不是input_placeholder,而不是提供batch 作为input_placeholder 的值?

      所以,而不是:

      input_placeholder = tf.Placeholder(tf.int32)
      loss = some_function(input_placeholder)
      sess.run(loss, feed_dict={input_placeholder: batch})
      

      做:

      loss = some_function(batch)
      sess.run(batch)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多