关于使用Keras在VGG16中构建第一个输入层

时间:2016-11-30 20:10:22

标签: tensorflow deep-learning theano keras

this blog中,作者包含用于构建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个零。如何确定要添加多少个零?

其次,为什么我们需要在-1中设置first_layer = model.layers[-1]?这里我们只有一个图层,而不是0

1 个答案:

答案 0 :(得分:4)

  

我们通常使用ZeroPadding2D来构建第一层读取图像作为输入吗?

取决于。在此特定代码中,作者打算执行3x3卷积,输出与相同宽度和高度的图像特征作为输入图像。如果输入图像大小为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?

使用精确索引是可以的。但是,如果有一天您决定在第一个卷积层下面添加预处理层,则不需要更改[-1]索引,因为它总是给出最顶层。如果忘记了,可以减少错误。