jef
jef

Reputation: 4083

Cannot set output dim of Conv2D in keras

Now I am studying AutoEncoder with CNN. For study, I created a model for MNIST data. But I could not set output dim of Conv2d correctly. Please see below model image. Although I expect the first Conv2d output should be (None, 16, 28, 28), the actual output is (None, 1, 28, 16). Regarding to the document, my code does not look bad. https://keras.io/layers/convolutional/#conv2d

Could you find any wrongs of my code?

My environment

Code

from keras.layers import Input, Convolution2D, MaxPool2D, UpSampling2D, Conv2D
from keras.models import Model
input_img = Input(shape=(1, 28, 28))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPool2D((2,2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPool2D((2,2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPool2D((2,2), padding='same')(x)

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

autoencoder= Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

from keras.utils import plot_model
plot_model(autoencoder, to_file="architecture.png", show_shapes=True)

enter image description here

Updated

I added autoencoder.summary(). So my question is why does not the first output of CNN became (None, 16, 28, 28)? (None, 1, 28, 16) is not my expectation.

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_181 (Conv2D)          (None, 1, 28, 16)         4048      
_________________________________________________________________
max_pooling2d_82 (MaxPooling (None, 1, 14, 16)         0         
_________________________________________________________________
conv2d_182 (Conv2D)          (None, 1, 14, 8)          1160      
_________________________________________________________________
max_pooling2d_83 (MaxPooling (None, 1, 7, 8)           0         
_________________________________________________________________
conv2d_183 (Conv2D)          (None, 1, 7, 8)           584       
_________________________________________________________________
max_pooling2d_84 (MaxPooling (None, 1, 4, 8)           0         
_________________________________________________________________
conv2d_184 (Conv2D)          (None, 1, 4, 8)           584       
_________________________________________________________________
up_sampling2d_72 (UpSampling (None, 2, 8, 8)           0         
_________________________________________________________________
conv2d_185 (Conv2D)          (None, 2, 8, 8)           584       
_________________________________________________________________
up_sampling2d_73 (UpSampling (None, 4, 16, 8)          0         
_________________________________________________________________
conv2d_186 (Conv2D)          (None, 4, 16, 16)         1168      

_________________________________________________________________
up_sampling2d_74 (UpSampling (None, 8, 32, 16)         0         
_________________________________________________________________
conv2d_187 (Conv2D)          (None, 8, 32, 1)          145       
=================================================================
Total params: 8,273.0
Trainable params: 8,273.0
Non-trainable params: 0.0
_________________________________________________________________

Updated2

My input_img is designed for Theano. So I have to change like below. Otherwise I changed image_dim_ordering in ~/.keras/keras.json

# Theano style
input_img = Input(shape=(1, 28, 28))

# Tensorflow style
input_img = Input(shape=(28, 28, 1))

Upvotes: 0

Views: 783

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56377

This is the very common problem problem with the image ordering. Theano puts the channels dimension in the second element of the shape array, like (samples, channels, width, height), while TensorFlow puts the channels dimensions at the end, like (samples, width, height, channels). You are using the Theano ordering but the backend is Tensorflow.

Just change the shapes to match the correct ordering and it should work. Alternatively you can change image_dim_ordering to "th" in your ~/.keras/keras.json file.

Upvotes: 1

Related Questions