繁体   English   中英

使用ImageDataGenerator(Keras)时,fit_generator输入尺寸错误

[英]fit_generator input dimensions error when using ImageDataGenerator (Keras)

我正在将Keras 2.04与tensorflow后端一起使用。 我正在尝试在MNIST图像上使用ImageDataGenerator训练一个简单的模型。 但是,我不断从fit_generator收到以下错误:

ValueError:检查输入时出错:预期input_1具有2维,但数组的形状为(8,28,28,1)。

这是代码:

#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),但是从错误看来,它似乎期望一个训练示例。

谢谢!

解决-应该是:

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,
       )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM