从 Tensorflow 中的图像列表中提取批次
Extracting batches from image list in Tensorflow
我有一个图像列表(尝试使用自定义图像训练 CNN 模型),由以下人员定义和重塑:
reader.images = tf.reshape(self.images, [-1, 256, 256, 3])
打印:
reader.Images :
[[[[ 127. 255. 127.]
[ 140. 255. 114.]
[ 217. 255. 38.]
...,
[[ 255. 240. 0.]
[ 255. 241. 0.]
[ 249. 246. 6.]
...,
[ 203. 237. 52.]
[ 152. 251. 102.]
[ 143. 253. 111.]]
[[ 255. 184. 0.]
[ 248. 192. 7.]
[ 205. 242. 50.]
...,
[ 255. 139. 0.]
[ 255. 171. 0.]
[ 255. 177. 0.]]
[[ 255. 178. 0.]
[ 237. 187. 18.]
[ 131. 240. 124.]
...,
[ 255. 123. 0.]
[ 255. 156. 0.]
[ 255. 162. 0.]]]]
我想通过每次迭代提取一批来训练我的模型。但是当我使用:
image_batch = tf.train.batch(
[reader.images], batch_size=batch_size, dynamic_pad=True)
和batch_size =1
我将整个图像作为输出(与上面打印的整个列表图像相同)。
我是 Tensorflow 的新手,所以欢迎任何提示。
简答:
对于您要求的内容,您应该添加 enqueue_many=True
。
长答案:
tf.train.batch
通常用于从来自例如队列的单项张量创建批次。
因此,您可能会为其提供一个图像张量 ([256, 256, 3]),然后得到一个批量张量 ([batch_size, 256, 256, 3])。
它通常不用于从列表张量中切出批次。如果以这种方式使用它,则必须将所有图像加载到内存中,这可扩展性不强。
那至少是默认的,enqueue_many=False
:
If enqueue_many is False, tensors is assumed to represent a single
example. An input tensor with shape [x, y, z] will be output as a
tensor with shape [batch_size, x, y, z].
如果您真的想将所有图像加载到内存中,您可以将 enqueue_many=True
添加到 tf.train.batch
调用中。
If enqueue_many is True, tensors is assumed to represent a batch of
examples, where the first dimension is indexed by example, and all
members of tensors should have the same size in the first dimension.
If an input tensor has shape [*, x, y, z], the output will have shape
[batch_size, x, y, z]. The capacity argument controls the how long the
prefetching is allowed to grow the queues.
我建议阅读 https://www.tensorflow.org/programmers_guide/datasets 以获得进一步的指导(现在记录了使用数据集而不是之前推荐的队列的新推荐方式 - 您可以直接从数据集创建批处理而不是调用 tf.train.batch).
我有一个图像列表(尝试使用自定义图像训练 CNN 模型),由以下人员定义和重塑:
reader.images = tf.reshape(self.images, [-1, 256, 256, 3])
打印:
reader.Images :
[[[[ 127. 255. 127.]
[ 140. 255. 114.]
[ 217. 255. 38.]
...,
[[ 255. 240. 0.]
[ 255. 241. 0.]
[ 249. 246. 6.]
...,
[ 203. 237. 52.]
[ 152. 251. 102.]
[ 143. 253. 111.]]
[[ 255. 184. 0.]
[ 248. 192. 7.]
[ 205. 242. 50.]
...,
[ 255. 139. 0.]
[ 255. 171. 0.]
[ 255. 177. 0.]]
[[ 255. 178. 0.]
[ 237. 187. 18.]
[ 131. 240. 124.]
...,
[ 255. 123. 0.]
[ 255. 156. 0.]
[ 255. 162. 0.]]]]
我想通过每次迭代提取一批来训练我的模型。但是当我使用:
image_batch = tf.train.batch(
[reader.images], batch_size=batch_size, dynamic_pad=True)
和batch_size =1
我将整个图像作为输出(与上面打印的整个列表图像相同)。 我是 Tensorflow 的新手,所以欢迎任何提示。
简答:
对于您要求的内容,您应该添加 enqueue_many=True
。
长答案:
tf.train.batch
通常用于从来自例如队列的单项张量创建批次。
因此,您可能会为其提供一个图像张量 ([256, 256, 3]),然后得到一个批量张量 ([batch_size, 256, 256, 3])。
它通常不用于从列表张量中切出批次。如果以这种方式使用它,则必须将所有图像加载到内存中,这可扩展性不强。
那至少是默认的,enqueue_many=False
:
If enqueue_many is False, tensors is assumed to represent a single example. An input tensor with shape [x, y, z] will be output as a tensor with shape [batch_size, x, y, z].
如果您真的想将所有图像加载到内存中,您可以将 enqueue_many=True
添加到 tf.train.batch
调用中。
If enqueue_many is True, tensors is assumed to represent a batch of examples, where the first dimension is indexed by example, and all members of tensors should have the same size in the first dimension. If an input tensor has shape [*, x, y, z], the output will have shape [batch_size, x, y, z]. The capacity argument controls the how long the prefetching is allowed to grow the queues.
我建议阅读 https://www.tensorflow.org/programmers_guide/datasets 以获得进一步的指导(现在记录了使用数据集而不是之前推荐的队列的新推荐方式 - 您可以直接从数据集创建批处理而不是调用 tf.train.batch).