'Conv2D output shape in CNN too small

The input shape in the first Conv2D layer is supposed to be (100, 100, 1) however the output is (None, 98, 98, 200). I understand what 200 and None determine but I'm not sure about 98 as the parameter. Also, adding to this, I randomly selected 200 as the number of filters in Conv2D for my model. How should I determine a suitable number of filters for my model. Is it based on trial and error? Please help. Thanks!!

from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten, Dropout
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import ModelCheckpoint

print(data.shape[1:])
model = Sequential()
model.add(Conv2D(200, (3,3), input_shape = data.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2,2)))

model.add(Conv2D(100,(3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(50, activation = 'relu'))
model.add(Dense(2, activation = 'softmax'))

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

(100, 100, 1) Model: "sequential_3"


Layer (type) Output Shape Param #

conv2d_5 (Conv2D) (None, 98, 98, 200) 2000


activation_5 (Activation) (None, 98, 98, 200) 0


max_pooling2d_5 (MaxPooling2 (None, 49, 49, 200) 0


conv2d_6 (Conv2D) (None, 47, 47, 100) 180100


activation_6 (Activation) (None, 47, 47, 100) 0


max_pooling2d_6 (MaxPooling2 (None, 23, 23, 100) 0


flatten_3 (Flatten) (None, 52900) 0


dropout_3 (Dropout) (None, 52900) 0


dense_5 (Dense) (None, 50) 2645050


dense_6 (Dense) (None, 2) 102

Total params: 2,827,252 Trainable params: 2,827,252 Non-trainable params: 0




Solution 1:[1]

add padding="same" as parameter to conv2d and the output dimension will be the same as the input dimension.

The default setting is padding="valid" and since you use a 3x3 filter and a step size of 1, you end up with 98x98 dimension because your 3x3 filter fits in 100x100 98 times.

Solution 2:[2]

This is how the dimensions are calculated in the above case

img_height = 100
img_width = 100
filter_height = 3
filter_width = 3
img_height-(filter_height-1),img_width-(filter_width-1) #(98,98)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 drops
Solution 2 user13934743