AndreyIto
AndreyIto

Reputation: 974

Keras Convolutional Autoencoder: Layer Shapes

I've got a list of about 70,000 training images, each shaped (no. of colour channels, height width) = (3, 30, 30), and about 20,000 testing images. My convolutional autoencoder is defined as:

 # Same as the code above, but with some params changed
# Now let's define the model. 

# Set input dimensions:
input_img = Input(shape=(3, 30, 30))

# Encoder: define a chain of Conv2D and MaxPooling2D layers
x = Convolution2D(128, 3, 3, 
                  activation='relu', 
                  border_mode='same')(input_img)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(64, 3, 3, 
                  activation='relu', 
                  border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(64, 3, 3, 
                  activation='relu', 
                  border_mode='same')(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)

# at this point, the representation is (8, 4, 4) i.e. 128-dimensional

# Decoder: a stack of Conv2D and UpSampling2D layers
x = Convolution2D(64, 3, 3, 
                  activation='relu', 
                  border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(64, 3, 3, 
                  activation='relu', 
                  border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(128, 3, 3, 
                  activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, 3, 3, 
                        activation='sigmoid', 
                        border_mode='same')(x)

autoencoder2 = Model(input_img, decoded)
autoencoder2.compile(optimizer='adadelta', loss='mse')

Which is the autoencoder from here.

It throws an error:

Error when checking model target: expected convolution2d_14 to have shape (None, 1, 28, 28) but got array with shape (76960, 3, 30, 30)

which is weird because I've clearly changed the specified the input shape as (3, 30, 30). Is there some implementation technicality I'm missing?

Upvotes: 2

Views: 3281

Answers (3)

random_user
random_user

Reputation: 1

You should chage the shape of the last convolutional layer from (1,3,3) to (3,3,3) as follows:

decoded = Convolution2D(3, 3, 3, 
                    activation='sigmoid', 
                    border_mode='same')(x)

Upvotes: 0

Nufa
Nufa

Reputation: 122

In the https://blog.keras.io/building-autoencoders-in-keras.html, they forgot to add the

'border_mode='same''.

For instance, in your 2nd last convolution layer;

x = Convolution2D(128, 3, 3, activation='relu')(x)

Upvotes: 0

Md Shopon
Md Shopon

Reputation: 823

You forgot to add border_mode='same' in the last convnet layer of the decoder .

Upvotes: 2

Related Questions