title: Why doesn't the UpSampling2d Keras layer work?
body: <p>I tried to build a convolutional autoencoder in keras but it doesn't seem to work properly.
  First of all, here's the Code:</p>
  
  <pre><code>from keras.models import Sequential
  from keras.layers import Reshape
  from keras.layers import Flatten
  from keras.layers import Dense
  from keras.layers import Conv2D
  from keras.layers import MaxPooling2D
  from keras.layers import UpSampling2D
  from keras.optimizers import RMSprop
  from keras.losses import categorical_crossentropy
  import numpy as np
  import matplotlib.pyplot as plt
  import scipy
  import h5py
  import random
  from glob import glob
  from tqdm import tqdm
  from os import getcwd
  
  learning_rate = 0.001
  epochs = 10000
  batch_size = 200
  data_len = 400
  training_data = []
  
  for filename in tqdm(glob('D:/AI/Celeba/*.jpg')[0:data_len],desc='loading images'):
      im = scipy.misc.imread(filename)
      im = scipy.misc.imresize(im,(128,128))
      training_data.append(im)
  training_data = np.array(training_data)
  example = random.choice(training_data)
  
  autoencoder = Sequential()
  autoencoder.add(Conv2D(32,5,activation='relu',input_shape=(128,128,3)))
  autoencoder.add(MaxPooling2D())
  autoencoder.add(Conv2D(16,5,activation='relu'))
  autoencoder.add(MaxPooling2D())
  autoencoder.add(Conv2D(8,5,activation='relu'))
  autoencoder.add(MaxPooling2D())
  autoencoder.add(Conv2D(4,5,activation='relu'))
  autoencoder.add(MaxPooling2D())
  autoencoder.add(Conv2D(2,3,activation='relu'))
  autoencoder.add(MaxPooling2D())
  autoencoder.add(Flatten())
  autoencoder.add(Dense(32,activation='sigmoid'))
  autoencoder.add(Dense(32,activation='sigmoid'))
  autoencoder.add(Reshape((4,4,2)))
  autoencoder.add(Conv2D(2,3,activation='relu'))
  autoencoder.add(UpSampling2D())
  autoencoder.add(Conv2D(4,5,activation='relu'))
  autoencoder.add(UpSampling2D())
  autoencoder.add(Conv2D(8,5,activation='relu'))
  autoencoder.add(UpSampling2D())
  autoencoder.add(Conv2D(16,5,activation='relu'))
  autoencoder.add(UpSampling2D())
  autoencoder.add(Conv2D(32,5,activation='relu'))
  autoencoder.add(UpSampling2D())
  autoencoder.add(Conv2D(3,5,activation='relu'))
  autoencoder.compile(optimizer=RMSprop(lr=learning_rate),loss=categorical_crossentropy)
  
  for epoch in range(epochs):
      autoencoder.fit(x=training_data,y=training_data,batch_size=batch_size,epochs=1)
      output = autoencoder.predict(np.array([example]))[0]
      fig = plt.figure()
      fig.suptitle('training step: {}'.format(epoch+1))
      a=fig.add_subplot(1,2,1)
      imgplot = plt.imshow(example)
      a.set_title('Original')
      a=fig.add_subplot(1,2,2)
      imgplot = plt.imshow(output)
      imgplot.set_clim(0.0,0.7)
      a.set_title('Reconstruction')
      plt.savefig(getcwd()+'/training/{}.png'.format(epoch))
  
      if (epoch+1)%10 == 0:
          autoencoder.save(getcwd()+'/saved/model.h5')
  </code></pre>
  
  <p>So my Problem is that the model is outputting the wrong shape. The Output shape is <code>[batch_size, 2, 2, 3]</code> when it should be <code>[batch_size, 128, 128, 3]</code>.
  I have done some debugging and found out some possible reasons for why the model is outputting the wrong dimensions:</p>
  
  <ol>
  <li>The <code>UpSampling2D</code> layer isn't doing anything</li>
  <li>The last <code>Conv2D</code> layer is reducing the size of the Image by 2 for some reason</li>
  </ol>
  
  <p>Does anybody know how I could fix this?</p>
  
  <p>PS.
  While writing this I realized that my debugging results could be interpreted in another way:</p>
  
  <ol>
  <li>The <code>UpSampling2D</code> layer is working fine</li>
  <li>All of the <code>Conv2D</code> layers are reducing the size of the Image by 2 so the Image always stays 2*2 tall</li>
  </ol>