理解 model.summary Keras

gog*_*sca 6 python keras tensorflow

我试图理解model.summary()Keras。我有以下卷积神经网络。第一个卷积的值为:

conv2d_4 (Conv2D)            (None, 148, 148, 16)      448 
Run Code Online (Sandbox Code Playgroud)

148和448从哪里来?

代码

image_input = layers.Input(shape=(150, 150, 3))
x = layers.Conv2D(16, 3, activation='relu')(image_input)

x = layers.MaxPooling2D(2)(x)
x = layers.Conv2D(32, 3, activation='relu')(x)

x = layers.MaxPooling2D(2)(x)
x = layers.Conv2D(64, 3, activation='relu')(x)

x = layers.MaxPooling2D(2)(x)
x = layers.Flatten()(x)
x = layers.Dense(512, activation='relu')(x)
output = layers.Dense(1, activation='sigmoid')(x)

# Keras Model definition
# input = input feature map
# output = input feature map + stacked convolution/maxpooling layers + fully connected layer + sigmoid output layer
model = Model(image_input, output)
model.summary()
Run Code Online (Sandbox Code Playgroud)

输出

Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         (None, 150, 150, 3)       0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 148, 148, 16)      448       
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 74, 74, 16)        0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 72, 72, 32)        4640      
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 36, 36, 32)        0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 34, 34, 64)        18496     
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 17, 17, 64)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 18496)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 512)               9470464   
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 513     
Run Code Online (Sandbox Code Playgroud)

Dat*_*ran 3

Keras 文档中,您可以看到填充是通过default=valid,因此没有填充,并且步幅大小为 1。那么您的输出形状显然是 148 x 148。

要计算此值,您可以使用以下公式:

O = (W - K + 2P)/S + 1
Run Code Online (Sandbox Code Playgroud)

其中 O 是输出高度/宽度,W 是输入高度/宽度,K 是过滤器大小,P 是填充,S 是步幅大小。

关于第二个参数,您的特征图为 16,内核大小为 3 x 3,因此您有 16 x (3 x 3),即 144。然后您有三个颜色通道,因此 144 x 3 = 432,然后您需要添加 16 个偏差,即 448;)希望这会有所帮助!