Keras ImageDataGenerator:数据和标签形状的问题

5

我希望能够使用Keras生成更多的图像,您可以在这里看到, 使用以下代码(与源代码>随机旋转几乎相同):

# Random Rotations
from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
from keras import backend as K
datagen = ImageDataGenerator(rotation_range=90)
# fit parameters from data
datagen.fit(cats["images"])

print(np.asarray(cats["label"]).shape)  #output=(12464,)
print(np.asarray(cats["images"]).shape) #output=(12464, 60, 60, 1)

# configure batch size and retrieve one batch of images
for X_batch, y_batch in datagen.flow(cats["images"], cats["label"], batch_size=9):
    # create a grid of 3x3 images
    for i in range(0, 9):
        pyplot.subplot(330 + 1 + i)
        pyplot.imshow(X_batch[i].reshape(28, 28), cmap=pyplot.get_cmap('gray'))
    # show the plot
    pyplot.show()
    break

但是我收到了以下的错误提示:
ValueError: x(图像张量)和y(标签)应该有相同的长度。找到的结果为:x.shape = (60, 60, 1),y.shape = (12464,)
这可能有助于进一步检查: enter image description here
我想库里应该有什么问题,如果我将我的图像形状改成60x60而不是60x60x1,我会得到以下错误提示:
ValueError: .fit()的输入应该具有rank 4。数组形状(12464,60,60)。
2个回答

16

很有可能cats['images']cats['labels']是Python列表。首先使用np.array将它们转换为数组,然后将它们传递给flow方法:

cats['images'] = np.array(cats['images'])
cats['labels'] = np.array(cats['labels'])

我对这种情况预料到了以下错误:“AttributeError: 'list' object has no attribute 'shape'”,所以我从未考虑过这一点。顺便说一句,非常感谢。 - M at

1
你需要改变标签的形状。
labels = np.asarray(cats["label"]).reshape(( -1 , 1 ))

这段代码无法运行,并且产生了几乎相同的错误信息:“ValueError: x(图像张量)和y(标签)应该具有相同的长度。发现:x.shape =(60,60,1),y.shape =(12464,1)”。 - M at

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接