CNN 中的 Conv2D 输出形状太小

问题描述 投票:0回答:2

第一个 Conv2D 层中的输入形状应该是 (100, 100, 1),但输出是 (None, 98, 98, 200)。我理解 200 和 None 确定什么,但我不确定 98 作为参数。 此外,我还随机选择了 200 作为模型 Conv2D 中的滤波器数量。我应该如何为我的模型确定合适的过滤器数量。是基于反复试验吗?

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
___________________________
python machine-learning keras conv-neural-network
2个回答
3
投票

padding="same"
作为参数添加到
conv2d
,输出维度将与输入维度相同。

默认设置为

padding="valid"
,由于您使用 3x3 过滤器且步长为 1,因此您最终会得到 98x98 尺寸,因为您的 3x3 过滤器适合 100x100 98 次。


0
投票

这就是上述情况下尺寸的计算方式

img_height = 100
img_width = 100
filter_height = 3
filter_width = 3
img_height-(filter_height-1),img_width-(filter_width-1) #(98,98)
© www.soinside.com 2019 - 2024. All rights reserved.