tf.nn.conv2d中padding参数的规则介绍

文章来源:《深度学习之TensorFlow:入门、原理与进阶实战》

padding属性的意义是定义元素边框和元素内容之间的空间。

在tf.nn.conv2d函数中,当变量padding为VALID和SAME,函数具体是怎么计算的呢?其实是有公式的。为了方便演示,先定义几个变量:

  • 输入的尺寸高和宽定义成:in_height,in_width
  • 卷积核的高和宽定义成filter_height、filter_width
  • 输入的尺寸中高和宽定义成output_height、out_width
  • 步长的高宽方向定义成strides_height、strides_width.

一 VALID情况:边缘不填充

输入宽和高的公式分别为:
output_width=(in_width-filter_width+1)/strides_width #(结果向上取整)
output_height=(in_height-filter_height+1)/strides_height #(结果向上取整)

二 SAME情况:边缘填充

# 输出的宽和高将与卷积核没关系,具体公式如下:
output_width=in_width/strides_width #(结果向上取整)

output_height=in_height/strides_height #(结果向上取整)

# 这里有一个很重要的知识点——补零的规则,见如下公式:
pad_height=max((out_height-1)*strides_height+filter_height-in_height,0)

pad_width=max((out_width-1)*strides_width+filter_width-in_width,0)

pad_top=pad_height/2

pad_bottom=pad_height-pad_top

pad_left=pad_width/2

pad_right=pad_width-pad_left

上面公式中

  • pad_height:代表高度方向上要填充0的行数。
  • pad_width:代表宽度方向要填充0的列数。
  • pad_top、pad_bottom、pad_left、pad_right分别代表上、下、左、右这4个方向填充0的行、列数。

三 规则举例

下面通过一个例子来理解一下padding规则。

假设用一个一维数据来举例,输入是13,filter是6,步长是5,对于padding的取值有如下表示:

3.1 当padding='VALID’

生成宽度为(13-6+1)/5=2(向上取整)个数字。

inputs:

(1 2 3 4 5 6) 7 8 9 10 11 12 13
1 2 3 4 5 (6 7 8 9 10 11) 12 13

12和13丢弃

3.2 当padding=‘SAME’,

生成的宽度为13/5=3(向上取整)

Padding的方式可以如下计算:

Pad_width=(3-1)*5+6-13=3
Pad_left=pad_width/2=3/2=1
Pad_rigth=Pad_width-pad_left=2
在左边补一个0,右边补2个0

input:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 0 0
(0 1 2 3 4 5) 6 7 8 9 10 11 12 13 0 0
0 1 2 3 4 (5 6 7 8 9 10) 11 12 13 0 0
0 1 2 3 4 5 6 7 8 9 (10 11 12 13 0 0)
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
tf.nn.conv2d is a function in TensorFlow that performs a 2D convolution operation on a given input tensor and a set of filters. It is typically used in deep learning models for image processing and computer vision tasks. The function takes several arguments, including the input tensor, the filter tensor, the strides for the convolution operation, and the padding scheme. The output of the convolution operation is a new tensor that represents the result of applying the filters to the input tensor. Here is an example usage of tf.nn.conv2d: ``` import tensorflow as tf # Define input and filter tensors input_tensor = tf.placeholder(tf.float32, shape=[None, 28, 28, 3]) filter_tensor = tf.Variable(tf.random_normal([5, 5, 3, 32])) # Perform a 2D convolution operation with strides of 1 and padding of 'SAME' conv = tf.nn.conv2d(input_tensor, filter_tensor, strides=[1, 1, 1, 1], padding='SAME') # Run the convolution operation within a TensorFlow session with tf.Session() as sess: sess.run(tf.global_variables_initializer()) # Define a sample input tensor input_data = np.random.rand(1, 28, 28, 3) # Run the convolution operation on the input tensor conv_result = sess.run(conv, feed_dict={input_tensor: input_data}) ``` In this example, we define an input tensor with a shape of (None, 28, 28, 3), which represents a batch of 28x28 RGB images. We also define a filter tensor with a shape of (5, 5, 3, 32), which represents 32 5x5 filters that will be applied to the input tensor. We then call tf.nn.conv2d with the input and filter tensors, specifying a stride of 1 and a padding scheme of 'SAME'. This means that the output tensor will have the same spatial dimensions as the input tensor, and that the edges of the input tensor will be zero-padded to ensure that the filters can be applied to all pixels. Finally, we run the convolution operation within a TensorFlow session, providing a sample input tensor to test the operation. The resulting conv_result tensor will have a shape of (1, 28, 28, 32), representing a batch of 28x28 feature maps for each of the 32 filters applied to the input tensor.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值