2

I'm trying create my first convolutional autoencoder in keras but I have problems with layer output shape. There is my code:

input_img = Input(shape=X_train.shape[1:])

x = Conv2D(32, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3))(input_img)
x = MaxPooling2D(pool_size=(2, 2), padding='same')(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3))(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(16, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3))(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3))(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
print(autoencoder.summary())
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(X_train, X_train, epochs=50, batch_size=32)

and result:

_________________________________________________________________
Layer (type)                 Output Shape              Param   
=================================================================
input_87 (InputLayer)        (None, 32, 32, 3)         0         
_________________________________________________________________
conv2d_327 (Conv2D)          (None, 32, 32, 3)         9248      
_________________________________________________________________
max_pooling2d_136 (MaxPoolin (None, 32, 16, 2)         0         
_________________________________________________________________
conv2d_328 (Conv2D)          (None, 16, 16, 2)         4624      
_________________________________________________________________
max_pooling2d_137 (MaxPoolin (None, 16, 8, 1)          0         
_________________________________________________________________
conv2d_329 (Conv2D)          (None, 16, 8, 1)          2320      
_________________________________________________________________
up_sampling2d_124 (UpSamplin (None, 16, 16, 2)         0         
_________________________________________________________________
conv2d_330 (Conv2D)          (None, 32, 16, 2)         4640      
_________________________________________________________________
up_sampling2d_125 (UpSamplin (None, 32, 32, 4)         0         
_________________________________________________________________
conv2d_331 (Conv2D)          (None, 1, 32, 4)          289       
=================================================================
Total params: 21,121
Trainable params: 21,121
Non-trainable params: 0
_________________________________________________________________
None

And of course error:

ValueError: Error when checking target: expected conv2d_331 to have shape (None, 1, 32, 4) but got array with shape (50000, 32, 32, 3)

Have you any idea what I'm doing wrong? Why last UpSampling2D return that shape?

1

1 Answer 1

2

So, it seems that your keras has set its image dimension to channel_first (and probably Theano as a backend) what means that the last two dimensions of your input are treated as spatial dimension instead of 2nd and 3rd. Another thing is that your output should have 3 filters instead of 1 beacuse this will end up in target dimension mismatch. To sum up:

  1. In order to set your input correctly you need to either switch to tensorflow and change channel ordering to channel_last or transpose your input by:

    X = X.transpose([0, 3, 1, 2])
    
  2. Change the following line:

    decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
    
1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.