user2775878
user2775878

Reputation: 81

ImageDataGenerator error

I need help trying to fix this code for a simple autoencoder in Keras. I was trying to add some image preprocessing for the autoencoder tutorial on the Keras blog. This is what I've done

input_image = Input(shape=(1,256,256,))
flattened = Flatten()(input_image)
encoded = Dense(128,activation='relu',name='Dense1')(flattened)
decoded = Dense(256*256, activation='sigmoid',name='Dense2')(encoded)
output_image = Reshape((1,256,256,))(decoded)
autoencoder = Model(input_image,output_image)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

datagen = ImageDataGenerator(
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True)

autoencoder.fit_generator(datagen.flow(train_imgs, train_imgs,
            batch_size=32),
            samples_per_epoch=train_imgs.shape[0],
            nb_epoch=50,
            validation_data=(test_imgs,test_imgs))

train_imgs has shape (1000,256,256) where 1000 is the number of training samples. test_imgs has shape(50,256,256).

This is the error I got

Exception: output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: None

This was raised by the fit_generator function.

Upvotes: 3

Views: 3981

Answers (3)

Aymen
Aymen

Reputation: 133

You need to change class_mode to 'input' like this :

autoencoder.fit_generator(datagen.flow(train_imgs, train_imgs,
        batch_size=32,class_mode='input'),
        samples_per_epoch=train_imgs.shape[0],
        nb_epoch=50,
        validation_data=(test_imgs,test_imgs))

You can read more here

Upvotes: 0

user2775878
user2775878

Reputation: 81

Figured this thing out myself. Turns out that ImageDataGenerator assumes that the input is in the shape (number_of_samples,number_of_channels,width,height). Reshaping train_imgs and test_imgs did the trick. I have modified the code in the question to include this extra dimension.

Upvotes: 4

Avijit Dasgupta
Avijit Dasgupta

Reputation: 2065

I think you have forgotten to fit the datagen model. Please add datagen.fit(train_imgs) before autoencoder.fit_generator and try to train your model.

Upvotes: -1

Related Questions