anon
anon

Reputation:

Keras: Why the output size of a Conv2D layer doesn't match the expected shape of feature map?

I am trying to run a convolutional neural network on MNIST images (28x28 gray-scale images) as inputs. To do it, I am using Keras (code is shown below). What I am having trouble understanding is why the shape of the convolutional layers doesn't match the shape I would expect them to have.

Particularizing to the first convolutional layer, what I am trying to use are 32 feature maps, convolving all of them with 3x3 kernels. The batch-size I am using is 200.

Given this I would expect that the shape of of the first Conv2D layer was (200, 26, 26, 32) i.e. 200 samples processed (due to batch size), 32 feature maps whose size was 26x26 (due to the convolution applied to the 28x28 input images with a 3x3 kernel).

However, the size I am getting by using model.summary() is (200, 9, 9, 32) as shown below:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (200, 9, 9, 32)           320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (200, 4, 4, 32)           0         
_________________________________________________________________
dropout_1 (Dropout)          (200, 4, 4, 32)           0         
_________________________________________________________________
flatten_1 (Flatten)          (200, 512)                0         
_________________________________________________________________
dense_2 (Dense)              (200, 256)                131328    
_________________________________________________________________
dense_3 (Dense)              (200, 10)                 2570      
=================================================================
Total params: 134,218
Trainable params: 134,218
Non-trainable params: 0
_________________________________________________________________

The code I am using is:

%tensorflow_version 2.x
# MNIST
from tensorflow.keras.datasets import mnist
from keras.utils import np_utils
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Convolution2D, MaxPooling2D, Dropout, Flatten, Dense

(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape(X_train.shape[0],28,28,1).astype('float32')
X_test  = X_test.reshape(X_test.shape[0],28,28,1).astype('float32')

X_train = X_train/255
X_test  = X_test/255

y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)

model = Sequential()
model.add(Convolution2D(32,3,3,activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(256,activation = 'relu'))
model.add(Dense(10, activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

history = model.fit(X_train, y_train, validation_data = (X_test, y_test), epochs = 10, batch_size = 200, verbose = 2)

model.summary()

Thanks in advance!

Upvotes: 0

Views: 2073

Answers (1)

Tristan Nemoz
Tristan Nemoz

Reputation: 2048

It is due to the fact that Convolution2D(32, 3, 3) is a convolution layer with 32 features maps in output, a kernel size of 3x3 and a stride of 3.

What you meant is probably Convolution2D(32, (3, 3)) or simply Convolution2D(32, 3).

Upvotes: 1

Related Questions