我使用卷积网络对不同形状的图像进行分类。我找不到在 Tensorflow 中加载图像的方法。基于this issue它应该与 tf.data.Dataset() 一起使用。我以这种方式创建数据集:
dataset = tf.data.Dataset.from_tensor_slices(filenames)
dataset = dataset.map(read_file)
dataset = dataset.shuffle(samples_in_buffer)
dataset = dataset.batch(batch_size)
dataset = dataset.repeat()
执行时出现这个错误
HandleElementToSlice Cannot copy slice: number of elements does not match. Shapes are: [element]: [295,256,3], [parent slice]: [276,128,3]
是否可以使用 Tensorflow 加载不同尺寸的图像、随机播放和批处理它们?
备注:我想用Spatial pyramid pooling来处理不同尺寸的图片。
最佳答案
dataset.batch() 正在尝试从不同大小的张量(您的不同大小的图像)构建密集批处理,如此处所述:tf.contrib.data.DataSet batch size can only set to 1
如果 1. 您正在设置 batch_size = 1 或 2. 将所有图像调整为相同大小,例如在您的 read_file 函数中使用 tf.image.resize_image_with_crop_or_pad() 。
另一种选择是使用 dataset.padded_batch(...) 而不是 dataset.batch 并指定 padded_shape 以便所有图像具有相同的大小(包括填充),即可能是 dataset.padded_batch(batch_size, padded_shape= [无])。
最后,在即将推出的 TF r1.4 中,您可能会使用 dataset.from_generator()。
关于python - 具有不同形状的 Tensorflow 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46791765/