简体   繁体   English

如何正确批处理图像的张量流数据集形状

[英]How to correctly batch a tensorflow dataset shape of images

I'm trying to create a batch dataset from a tensor dataset and having trouble with the shape.我正在尝试从张量数据集创建批处理数据集,但在形状方面遇到了问题。 I saw some references to enqueue but a couple years out of date and the tf.dataset.batch doesn't have any options.我看到了一些对 enqueue 的引用,但已经过时了几年,并且 tf.dataset.batch 没有任何选项。 My dataset looks like:我的数据集看起来像:

X_test1 = tensorflow.data.Dataset.from_tensors((X_test_images, X_test_labels))
<TensorDataset shapes: ((5512, 256, 256, 3), (5512,)), types: (tf.float32, tf.int32)>

Which is image arrays of 256 x 256 with 3 color channels and a label vector for 5512 images / labels.这是 256 x 256 的图像数组,具有 3 个颜色通道和一个用于 5512 个图像/标签的标签向量。

But when I try to batch it, it creates a new dimension:但是当我尝试对它进行批处理时,它会创建一个新维度:

new = X_test1.batch(32)
<BatchDataset shapes: ((None, 5512, 256, 256, 3), (None, 5512)), types: (tf.float32, tf.int32)>

What I really want is:我真正想要的是:

<BatchDataset shapes: ((None, 256, 256, 3), (None,)), types: (tf.float32, tf.int32)>

Where the None is the batch 32, with maybe some remainder in the last batch.其中 None 是第 32 批,最后一批中可能还有一些剩余部分。

Thanks!!谢谢!!

You should initialize the dataset using from_tensor_slices:您应该使用 from_tensor_slices 初始化数据集:

X_test1 = tf.data.Dataset.from_tensor_slices((X_test, y_test))
new = X_test1.batch(32)

Here the Documentation这里的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM