Convolution layers decrease dimension of output in Keras?

1.3k views Asked by At

I'm trying to build a convolutional autoencoder in Keras. I was under the impression that convolutional layers have the same output dimension as the input unless if you set the stride>1 or set border_mode='valid'. Here is my network

from keras.layers import Convolution2D, MaxPooling2D, UpSampling2D, Activation
from keras.models import Model, Sequential
model = Sequential()
model.add(Convolution2D(16, 3, 3, border_mode='same', input_shape=(1, 1920, 1080)))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2), border_mode='same'))
model.add(Convolution2D(8, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(UpSampling2D((2, 2)))
model.add(Convolution2D(8, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(UpSampling2D((2, 2)))
model.compile(optimizer='adadelta', loss='binary_crossentropy')

When I check to see the output dimension of the first layer it seems like the length of the y coordinate has been decreased.

>>>model.layers[0].output_shape
(None, 1, 1920, 16)

What's causing this? I can't figure out where that 16 is coming from. It doesn't seem to have any simple relationship to 1080.

1

There are 1 answers

0
Marcin Możejko On

The problem lies in a so called image_ordering. Depending on which backend you use - you need to provide data in a specified format:

  • for th (Theano) image ordering you should provide data in a format:

    [batches, channels, image_width, image_weight]
    

    This is a format you are assuming as correct.

  • for tf (TensorFlow) image ordering is:

     [batches, width, height, channels]
    

    This is a format by which your Keras installation is interpretting your input.

A default image ordering in keras is tf and I'm assuming that this is causing your problem. You may check your image ordering by the following commands:

from keras.backend import image_dim_ordering
print image_dim_ordering() # Assuming that you're using a Python 2.*

You may change it by changing a keras.json what is described here.

You may also check your model details to assure yourself that this is actually the case by printing the result of model.summary().