关于使用 Keras 在 VGG16 中构建第一个输入层
regarding building the first input layer in VGG16 using 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个零。如何决定加多少个零?
其次,为什么要在first_layer = model.layers[-1]
中设置-1
?这里我们只有一层,应该是0
吧?
is it always true that we normally use ZeroPadding2D to build the first layer reading image as input?
视情况而定。在这个特定的代码中,作者打算执行一个 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 ...
What does (1,1) indicate for the input parameter of ZeroPadding2D?
如果输入图像是 128*128,(1,1)
零填充将创建一个 130x130 的图像,添加一个 1 像素宽的黑框。 (1,1)
表示分别在horizonal/vertical条边上添加多少像素。
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)
填充。
why do we need to set -1 in first_layer = model.layers[-1]?
可以使用精确索引。但是,如果有一天您决定在第一个卷积层下方添加一个预处理层,则无需更改 [-1]
索引,因为它始终给出最顶层。减少错误,以防您忘记。
在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个零。如何决定加多少个零?
其次,为什么要在first_layer = model.layers[-1]
中设置-1
?这里我们只有一层,应该是0
吧?
is it always true that we normally use ZeroPadding2D to build the first layer reading image as input?
视情况而定。在这个特定的代码中,作者打算执行一个 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 ...
What does (1,1) indicate for the input parameter of ZeroPadding2D?
如果输入图像是 128*128,(1,1)
零填充将创建一个 130x130 的图像,添加一个 1 像素宽的黑框。 (1,1)
表示分别在horizonal/vertical条边上添加多少像素。
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)
填充。
why do we need to set -1 in first_layer = model.layers[-1]?
可以使用精确索引。但是,如果有一天您决定在第一个卷积层下方添加一个预处理层,则无需更改 [-1]
索引,因为它始终给出最顶层。减少错误,以防您忘记。