本文介绍了使用ImageDataGenerator(Keras)时,fit_generator输入尺寸错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将Keras 2.04与tensorflow后端一起使用.我正在尝试在MNIST图像上使用ImageDataGenerator训练一个简单的模型.但是,我不断从fit_generator收到以下错误:
I am using Keras 2.04 with tensorflow backend. I am trying to train a simple model with ImageDataGenerator on MNIST images. However, I keep getting the following error from fit_generator:
这是代码:
#loading data & reshaping
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], 28, 28,1)
#building the model
input_img = Input(shape=(784,))
encoded = Dense(30, activation='relu')(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='mse')
#creating ImageDataGenerator
datagen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True)
datagen.fit(x_train)
autoencoder.fit_generator(
#x_train, x_train because the target is to reconstruct the input
datagen.flow(x_train, x_train, batch_size=8),
steps_per_epoch=int(len(x_train)/8),
epochs=64,
)
据我了解,ImageDataGenerator应该在每次迭代时都生成一批训练示例,就像它实际所做的那样(在这种情况下,batch_size = 8),但是从错误中看来,它似乎期望一个训练示例.
As far as I understand ImageDataGenerator should generate a batch of training examples each iteration, as it actually does (in this case, batch_size=8) but from the error it seems as if it expects a single training example.
谢谢!
推荐答案
已解决-应该应该是:
autoencoder = Sequential()
autoencoder.add(Reshape((784,), input_shape=(28,28,1)))
autoencoder.add(Dense(30, activation='relu'))
autoencoder.add(Dense(784, activation='relu'))
.
.
.
autoencoder.fit_generator(
datagen.flow(x_train, x_train.reshape(len(x_train),784,), batch_size=8),
steps_per_epoch=int(len(x_train)/8),
epochs=64,
)
这篇关于使用ImageDataGenerator(Keras)时,fit_generator输入尺寸错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!