pabloxrl
pabloxrl

Reputation: 295

Wrong dimensions when building convolutional autoencoder

I'm taking my first steps in Keras and struggling with the dimensions of my layers. I'm currently building a convolutional autoencoder that I would like to train using the MNIST dataset. Unfortunately, I cannot seem to get the dimensions right, and I'm having trouble to understand where is my mistake.

My model is built through:

def build_model(nb_filters=32, nb_pool=2, nb_conv=3):
    input_img = Input(shape=(1, 28, 28))

    x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img)
    x = MaxPooling2D((2, 2), border_mode='same')(x)
    x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
    x = MaxPooling2D((2, 2), border_mode='same')(x)
    x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
    encoded = MaxPooling2D((2, 2), border_mode='same')(x)

    x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
    x = UpSampling2D((2, 2))(x)
    x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
    x = UpSampling2D((2, 2))(x)
    x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x)
    x = UpSampling2D((2, 2))(x)
    decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)

return Model(input_img, decoded)

and the data is retrieved using:

def load_data():
    (x_train, _), (x_test, _) = mnist.load_data()

    x_train = x_train.astype('float32') / 255.
    x_test = x_test.astype('float32') / 255.
    x_train = np.reshape(x_train, (len(x_train), 1, 28, 28))
    x_test = np.reshape(x_test, (len(x_test), 1, 28, 28))
    return x_train, x_test

As you see, I'm trying to normalize the images to display them in black and white, and simply to train an autoencoder to be able to restore them.

Below you can see the error I'm getting:

Traceback (most recent call last): File "C:/Users//Documents/GitHub/main/research/research_framework/experiment.py", line 46, in callbacks=[EarlyStopping(patience=3)]) File "C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\keras\engine\training.py", line 1047, in fit batch_size=batch_size) File "C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\keras\engine\training.py", line 978, in _standardize_user_data exception_prefix='model target') File "C:\Users\AppData\Local\Continuum\Anaconda2\lib\site-packages\keras\engine\training.py", line 111, in standardize_input_data str(array.shape)) Exception: Error when checking model target: expected convolution2d_7 to have shape (None, 8, 32, 1) but got array with shape (60000L, 1L, 28L, 28L) Total params: 8273

Process finished with exit code 1

Could you help me to decyper this error? Are there any materials beyond Keras website about building models and dealing with this kind of issues?

Cheers

Upvotes: 1

Views: 473

Answers (2)

pabloxrl
pabloxrl

Reputation: 295

The reason was that while I changed my backend configuration in keras.json, I didn't change the image dimanesion, so it was still set to tensorflow.

Changing it to:

{
    "image_dim_ordering": "th", 
    "epsilon": 1e-07, 
    "floatx": "float32", 
    "backend": "theano"
}

did the trick.

Upvotes: 1

pyan
pyan

Reputation: 3707

Looks like your input shape isn't correct. Try changing (1,28,28) to (28,28,1) and see if that works for you. For more details and other options to solve the problem, please refer to the answer to another question.

Upvotes: 2

Related Questions