2016-11-30 44 views
2

this blog中,作者包含構建VGG16網絡的代碼段。我有一個關於代碼關於使用Keras構建VGG16中的第一個輸入層

model = Sequential() 
model.add(ZeroPadding2D((1, 1), batch_input_shape=(1, 3, img_width, img_height))) 
first_layer = model.layers[-1] 
# this is a placeholder tensor that will contain our generated images 
input_img = first_layer.input 

相關的以下部分model.add(ZeroPadding2D((1, 1), batch_input_shape=(1, 3, img_width, img_height)))一些問題,就是它總是真的,我們通常使用ZeroPadding2D打造的第一層讀取圖像作爲輸入? (1,1)對輸入 參數ZeroPadding2D指示什麼。根據Keras文檔,這意味着我們爲行和列都添加1個零。如何決定添加多少個零?

其次,爲什麼我們需要在first_layer = model.layers[-1]中設置-1?這裏我們只有一層,應該是0而不是?

回答

4

我們通常使用ZeroPadding2D構建第一層讀取圖像作爲輸入,這總是正確的嗎?

取決於。在這個特定的代碼中,作者打算執行3×3卷積,其輸出圖像特徵與作爲輸入圖像的相同的寬度和高度。如果輸入圖像大小是2的冪,通常會出現這種情況,因爲您希望保留2x2池圖層的編號。

沒有填充:

128x128 -[3x3 conv]-> 126x126 -[2x2 pool]-> 63x63 -[3x3 conv]-> 61x61 -> *how to pool next?* 

隨着填充:

128x128 -[pad 1]-> 130x130 -[3x3 conv]-> 128x128 -[2x2 pool]-> 64x64 
-[pad+conv+pool]-> 32x32 -[...]-> 16x16 -> 8x8 ... 

是什麼(1,1)指示ZeroPadding2D的輸入參數?

如果輸入圖像是128 * 128,(1,1)零填充將創建一個130x130圖像,添加一個1像素寬的黑色框架。 (1,1)表示分別在水平/垂直邊緣添加多少個像素。

  o o o o o 
x x x  o x x x o 
x x x -> o x x x o 
x x x  o x x x o 
      o o o o o 

如果您有意使用5x5的卷積保持圖像的尺寸,你需要一個(2,2)填充。

爲什麼我們需要在first_layer = model.layers [-1]中設置-1?

可以使用精確索引。但是,如果您決定在第一個卷積圖層下面添加預處理圖層,則不需要更改索引,因爲它總是提供最上層的圖層。如果您忘記了,減少錯誤。

+0

感謝您提供非常詳細的解釋。 – user785099