datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=15, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False) # randomly flip images
# (std, mean, and principal components if ZCA whitening is applied).
# datagen.fit(x_train)
print(x_train.shape)
def data_generator(generator, x, y1, y2, batch_size):
genX = generator.flow(x, seed=7, batch_size=batch_size)
genY1 = generator.flow(y1, seed=7, batch_size=batch_size)
genY2 = generator.flow(y2, seed=7, batch_size=batch_size)
while(True):
Xi = genX.next()
Yi1 = genY1.next()
Yi2 = genY2.next()
yield Xi, [Yi1, Yi2]
这就是我调用 model.fit_generator 的方式
model.fit_generator(data_generator(datagen, x_train, y_train, y_aux_train, params['batch_size']),
epochs=params['epochs'], steps_per_epoch=150,
validation_data=data_generator(datagen, x_test, y_test, y_aux_test, params['batch_size']),
validation_steps=100, callbacks=[reduce_lr, tensorboard],verbose=2)
这是我得到的错误 -
ValueError: ('Input data in
NumpyArrayIterator
should have rank 4. You passed an array with shape', (5630, 4))
最佳答案
您的x
、y1
和y2
是多少? ImageDataGenerator 的输入数据必须有 4 个维度(批处理、 channel 、高度、宽度)。您的数据完全不同,这就是您收到错误的原因。
更新:
根据docs :
flow(x, y=None, batch_size=32, shuffle=True, sample_weight=None, seed=None, save_to_dir=None, save_prefix='', save_format='png', subset=None)
x: Input data. Numpy array of rank 4 or a tuple. If tuple, the first element should contain the images and the second element another numpy array or a list of numpy arrays that gets passed to the output without any modifications. Can be used to feed the model miscellaneous data along with the images.
在genY1=generator.flow(y1,seed=7,batch_size=batch_size)
中,您将标签(如我所见,形状为(4,))作为特征传递,并且ImageDataGenerator期望它们有 4 个维度。
你不应该这样传递你的标签。尝试这样的事情:
datagen.flow((x_train, [y_train, y_aux_train]), batch_size=params['batch_size'])
或者:
datagen.flow(x=x_train, y=[y_train, y_aux_train], batch_size=params['batch_size'])
关于python - Keras 中的 ImageDataGenerator 形状问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55160575/