简体   繁体   English

如何在Conv2D图层中使用keras指定填充?

[英]How to specify padding with keras in Conv2D layer?

I am trying to implement AlexNet with Keras and was inspecting the network design in MATLAB which is given as follows 我正在尝试使用Keras来实现AlexNet ,并且正在检查MATLAB中的网络设计,如下所示

在此处输入图片说明

As could be seen, the second convolution layer has 256 filters of size 5x5, 48 channels and a padding of [ 2 2 2 2 ]. 可以看出,第二卷积层具有256个大小为5x5的滤波器,48个通道和[2 2 2 2]的填充。 How could I specify padding of [ 2 2 2 2] with Keras? 如何指定padding的[2 2 2 2] padding I went through the documentation of Conv2D . 我浏览了Conv2D文档 It accepts only 2 values for padding namely valid and same . 它仅接受2个填充值,即validsame I could not understand this. 我不明白这一点。 For what I know, valid would mean zero padding. 据我所知, valid意味着零填充。 How could I specify [2 2 2 2] padding with the second convolution layer? 如何指定第二个卷积层的[2 2 2 2]填充? I created the first layer as: 我将第一层创建为:

model.add(keras.layers.Conv2D(filters = 96, kernel_size = (11,11), 
 strides = (4,4), padding = "valid", input_shape=(227,227,3)))

Also, since in the second layer there are 48 channels, do I need to be explicit about it? 另外,由于在第二层中有48个通道,因此我需要对此进行明确说明吗?

A specific padding isn't specified in Conv2D but instead a ZeroPadding2D layer. Conv2D未指定特定的填充,而是在ZeroPadding2D层中指定。

valid and same are really just shorthands for common paddings - valid means that you don't pad the input and same means you add padding such that the output length is the same as the input length. validsame实际上只是常见填充的简写- valid表示您不填充输入,而same表示您添加填充以使输出长度与输入长度相同。

In your case if you wanted to add a specific padding of size 2: 如果您要添加大小为2的特定填充:

model.add(keras.layers.ZeroPadding2D(padding=(2, 2)))
model.add(keras.layers.Conv2D(filters = 96, kernel_size = (11,11), strides = (4,4), padding = "valid"))

I would also strongly suggest checking out this keras implementation of alexnet . 我也强烈建议您检查一下alexnet的这种keras实现 Note that you can also find docs for padding layers in the keras convolutional docs (it's all the way at the bottom). 请注意,您还可以在keras卷积文档中找到用于填充层的文档 (始终在底部)。

You got valid padding right, please notice that width and height will be smaller after layer with this parameter. 您的valid填充正确无误,请注意,使用此参数进行分层后,宽度和高度会变小。

Padding same on the other hand means that specific padding size will be used to ensure image dimensions will not change. 填充same ,另一方面是指特定的填充大小将被用来确保图像尺寸将不会改变。

For your specific case, if you pad input image with 2 pixels on each side you will get exactly same size of image as output from the layer. 对于您的特定情况,如果您在输入图像的每一侧填充2像素,您将获得与图层输出完全相同的图像尺寸。 So specifying same will perform exact same padding as [2 2 2 2] . 因此,指定same将执行与[2 2 2 2]完全相同的填充。

If you want formula for calculating output size after convolutional layer check first answer to this Quora question . 如果您想要在卷积层之后计算输出大小的公式, 请首先检查此Quora问题的答案

I have rarely (if at all) seen different padding schemes so those usually suffice. 我很少(如果有的话)看到不同的填充方案,因此通常就足够了。

BTW. 顺便说一句。 All layers in AlexNet use padding same except the first one (as correctly pointed out in the comments to another answer). AlexNet中的所有层都使用same的填充,但第一个除外(如对另一个答案的注释中正确指出的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM