helloName
helloName

Reputation: 9

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?

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()

Output:

(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
___________________________

Upvotes: 0

Views: 624

Answers (2)

user13934743
user13934743

Reputation: 41

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)

Upvotes: 0

drops
drops

Reputation: 1604

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.

Upvotes: 3

Related Questions