Wrong dimensions when building convolutional autoencoder
<p>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.</p>

<p>My model is built through:</p>

<pre><code>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)
</code></pre>

<p>and the data is retrieved using:</p>

<pre><code>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
</code></pre>

<p>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.</p>

<p>Below you can see the error I'm getting:</p>

<blockquote>
  <p>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</p>
  
  <p>Process finished with exit code 1</p>
</blockquote>

<p>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?</p>

<p>Cheers</p>

<p>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.</p>

<p>Changing it to:</p>

<pre><code>{
    "image_dim_ordering": "th", 
    "epsilon": 1e-07, 
    "floatx": "float32", 
    "backend": "theano"
}
</code></pre>

<p>did the trick.</p>

python python-2.7 deep-learning keras autoencoder