2017-08-08 80 views
2

我想了解Keras中的model.summary()。我有以下的卷積神經網絡。第一個卷積的值如下:瞭解model.summary Keras

conv2d_4 (Conv2D)   (None, 148, 148, 16)  448 

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

輸出

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  

回答

1

Keras documentation,你可以看到,填充是default=valid所以沒有填充,而且進步大小爲1。那麼你的輸出形狀顯然是148×148。

爲了計算此可以使用以下公式:

O = (W - K + 2P)/S + 1 

其中O是輸出高度/寬度,W是輸入高度/寬度,K是過濾器的大小,P是填充和S是步幅大小。

關於第二個參數,你有一個16的特徵映射,你的內核大小是3×3,所以你有16×(3×3)這是144.然後你有三個顏色通道,所以144×3 = 432,然後你需要添加16個偏見,這使得448;)希望這會有所幫助!