python - 凯拉斯图像数据生成器 : problem with data and label shape

标签 python tensorflow image-processing keras

我想使用 Keras 生成更多图像,您可以在 here 中看到, 使用此代码(几乎与 source>Random Rotations 相同):

# 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 (images tensor) and y (labels) should have the same length. Found: x.shape = (60, 60, 1), y.shape = (12464,)

这可能有助于进一步检查: enter image description here

我想库应该有问题,就好像我将图像的形状更改为 60x60 而不是 60x60x1 我会得到:

ValueError: Input to .fit() should have rank 4. Got array with shape: (12464, 60, 60)

最佳答案

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

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

关于python - 凯拉斯图像数据生成器 : problem with data and label shape,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54005424/

相关文章:

python - Bottle 框架中的多种路线和功能

python - 在大矩阵中找到满足特定条件的元素的所有索引给出 MemoryError

python - 包含 HTML 标记的文件上的 Hadoop MapReduce 作业

从浮雕/浮雕图像中获取近似深度图的算法

python - Numpy 的逻辑函数和 "if cond is False"习惯用法

python - Tensorflow - 条件训练

python - Tensorflow有类似python中set()的数据类型吗?

tensorflow - 在使用 tf.keras.preprocessing.image_dataset_from_directory() 时如何在预测期间获取文件名?

c++ - 如何访问满足ITK C++中特定条件的特定体素?

python - 尝试将 cvtColor 与 cv2.COLOR_YUV2BGR_Y422 一起使用时出错 - 错误:函数 cv::cvtColor 中的 (-215) scn == 2 && depth == 0