python - Keras ImageDataGenerator:数据和标签形状的问题
问题描述
我想使用 Keras 生成更多图像,如您在此处看到的,使用此代码(几乎与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
图像张量)和y
(标签)应该具有相同的长度。找到:x.shape = (60, 60, 1), y.shape = (12464,)
我想图书馆应该有问题,就好像我将图像的形状更改为 60x60 而不是 60x60x1 我会得到:
ValueError: 输入
.fit()
应该有 4 级。得到的数组形状为: (12464, 60, 60)
解决方案
cats['images']
和很可能cats['labels']
是 Python 列表。首先使用将它们转换为数组np.array
,然后将它们传递给flow
方法:
cats['images'] = np.array(cats['images'])
cats['labels'] = np.array(cats['labels'])
推荐阅读
- hibernate - 由于内部连接导致休眠导致重复结果:如何解决?
- mysql - 想要在 aws lambda 中的 nodejs 上实现快速 csv,并给出错误:退出状态 129 Runtime.ExitError
- firebase - 如何从具有属性匹配/或子集合不匹配的父集合中检索云存储库中的文档
- c# - 解析时如何循环遍历xml文档?
- java - 检查外部活动是否成功打开?
- php - SQL:连接两个表,每行重复计数?
- html - 在 htm 中创建 wsc 文件的对象并访问 wsc 函数
- ssis - 减少运行时转换包
- mysql - 有条件地选择表和连接计数
- ios - 如何使用cordova检测文件在iOS上是否不可用?