0
$\begingroup$

I need to train an autoencoder in Keras with the JPG images I took myself.

model = Sequential()

#1st convolution layer
model.add(Conv2D(16, (3, 3), padding='same', data_format='channels_first', input_shape=(3,224,224)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), padding='same'))

#2nd convolution layer
model.add(Conv2D(2,(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), padding='same'))

#-------------------------

#3rd convolution layer
model.add(Conv2D(2,(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(UpSampling2D((2, 2)))

#4rd convolution layer
model.add(Conv2D(16,(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(UpSampling2D((2, 2)))

#-------------------------

model.add(Conv2D(1,(3, 3), padding='same'))
model.add(Activation('sigmoid'))

model.summary()

which generates a model as:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 16, 224, 224)      448       
_________________________________________________________________
activation_1 (Activation)    (None, 16, 224, 224)      0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 112, 112)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 2, 112, 112)       290       
_________________________________________________________________
activation_2 (Activation)    (None, 2, 112, 112)       0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 2, 56, 56)         0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 2, 56, 56)         38        
_________________________________________________________________
activation_3 (Activation)    (None, 2, 56, 56)         0         
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 2, 112, 112)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 16, 112, 112)      304       
_________________________________________________________________
activation_4 (Activation)    (None, 16, 112, 112)      0         
_________________________________________________________________
up_sampling2d_2 (UpSampling2 (None, 16, 224, 224)      0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 1, 224, 224)       145       
_________________________________________________________________
activation_5 (Activation)    (None, 1, 224, 224)       0         
=================================================================
Total params: 1,225
Trainable params: 1,225
Non-trainable params: 0
_________________________________________________________________

I compile and train as:

model.compile(optimizer='adadelta', loss='binary_crossentropy')
batch_size = 16 #16

train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
    'cropped/',
    target_size=(224, 224),
    batch_size=batch_size,
    class_mode='categorical'
    )
test_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = test_datagen.flow_from_directory(
    'cropped/',
    target_size=(224, 224),
    batch_size=batch_size,
    class_mode='categorical'
    )

model.fit_generator(
        train_generator,
        steps_per_epoch=1000,
        epochs=20,
        validation_data=validation_generator,
        validation_steps=1000)

I end up with the error message:

ValueError: Error when checking target: expected activation_5 to have 4 dimensions, but got array with shape (16, 2)

Should I use Conv2D instead?

$\endgroup$
1
$\begingroup$

It was not about the model, but about ImageDataGenerator. I found the answer here: https://stackoverflow.com/a/51673998/493080

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.