简体   繁体   English

如何在conv2d中添加特定的填充

[英]How to add specific padding in conv2d

I am trying to pass a tensor to a CNN in python. 我正在尝试将张量传递给python中的CNN。 I am using tf.layers.conv2d for this. 我为此使用tf.layers.conv2d I want to get the output dimensions as half of the input. 我想将输出尺寸作为输入的一半。 I understand that I can use a padding of 'same' followed by a max pooling layer to achieve this, but another way that I found out in a recent paper is to use a 3x3 kernel with a 2x2 stride in addition to a 1x1 padding 我知道我可以在最大池化层之后使用“相同”的填充来实现此目的,但是我在最近的一篇论文中发现的另一种方法是,除了使用1x1填充之外,还使用具有2x2跨度的3x3内核

Now, as I understand, conv2d allows for a parameter called padding which can be either 'valid' or 'same', which does not allow for a padding of a specific length. 现在,据我所知,conv2d允许使用一个称为padding的参数,该参数可以是“ valid”或“ same”,它不允许特定长度的padding。

Is there any way that this can be achieved directly in the operation? 有什么方法可以直接在手术中实现吗? I am asking since the idea is to use multiple convolution layers, each successively halving the dimensions both length and width-wise. 我之所以问是因为这种想法是使用多个卷积层,每一层都将长度和宽度尺寸依次减半。

If someone can point out a similar topic or help with this, it would be great. 如果有人可以指出类似的话题或对此提供帮助,那就太好了。

Edit: 编辑:

I have been asked to include the code. 我被要求包括代码。 But there is not much of a code since I am stuck at the very first point where the CNN needs to be designed. 但是没有太多代码,因为我停留在需要设计CNN的第一点。 Nonetheless, here is the part that accepts the initial input and starts defining the CNN (here I have kept padding='same' since I was checking if that would work): 尽管如此,这是接受初始输入并开始定义CNN的部分(在此,我一直保持padding ='same',因为我一直在检查是否可行):

def conv2d(z):
    output = tf.layers.conv2d(z,strides=[2,2],filters=3,padding='same',kernel_size=3);

I am stuck at this point since going any further needs this initial layer to be correctly defined as successive layers will also work on the same principle. 我被困在这一点上,因为任何进一步的工作都需要正确定义初始层,因为连续的层也将以相同的原理工作。

Also, my initial input size is a 224x224 image with 1 channel (grayscale image) 另外,我的初始输入大小是1通道的224x224图像(灰度图像)

So, I found an answer to this. 因此,我找到了答案。 There is an inbuilt function called pad in tensorflow, which can be used to solve it. 在tensorflow中有一个称为pad的内置函数,可以用来解决它。 What I am doing is this 我正在做的是这个

output = tf.pad(output, paddings, "CONSTANT")
output = tf.layers.conv2d(output,strides=[2,2],kernel_size=3,filters=3)

This means I am using a tf.pad before every layer wherein the variable 'paddings' is defined as below 这意味着我在每一层变量“ paddings”定义如下的层之前使用tf.pad。

paddings=tf.constant([[0,0], [1,1],[1,1],[0,0]])

This ensures that each layer is padded before being passed on for convolution, thus giving the output as desired 这样可确保在传递每个卷积之前对其进行填充,从而提供所需的输出

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

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