深入浅出理解TensorFlow的padding填充算法

一、参考资料

notes_on_padding_2

二、TensorFlow的padding算法

本文以TensorFlow v2.14.0版本为例,介绍TensorFlow的padding算法。

1. 引言

tf.nn.conv2d and tf.nn.max_pool2d 函数都有padding参数,在执行函数之前,都需要进行填充padding(零元素)操作。padding参数可以是 VALIDSAMEVALID 表示no-padding不填充,SAME表示需要padding。

对于convolutions,用零元素填充;对于pools,填充值可以忽略,例如max_pool,其滑动窗口会忽略填充值。

2. VALID padding

padding='VALID' 表示不填充,这种情况下,输出的尺寸一般小于输入的尺寸。

对于 conv2d,它的输出尺寸为:

out_height = ceil((in_height - filter_height + 1) / stride_height)
out_width  = ceil((in_width - filter_width + 1) / stride_width)

其中,filter_height and filter_width 表示滤波器fillter的尺寸。

3. SAME padding

padding='SAME' 可以对空间的各个维度进行padding。对于 conv2d,它的输出尺寸为:

out_height = ceil(in_height / stride_height)
out_width  = ceil(in_width / stride_width)

重要说明:如果不关注padding的内部实现机制,该结论可以直接使用。

对于每个维度方向的padding,可以表示为:

if (in_height % strides[1] == 0):pad_along_height = max(filter_height - stride_height, 0)
else:pad_along_height = max(filter_height - (in_height % stride_height), 0)
if (in_width % strides[2] == 0):pad_along_width = max(filter_width - stride_width, 0)
else:pad_along_width = max(filter_width - (in_width % stride_width), 0)

最终,对于 top, bottom, left and right 各维度的padding为:

pad_top = pad_along_height // 2
pad_bottom = pad_along_height - pad_top
pad_left = pad_along_width // 2
pad_right = pad_along_width - pad_left

其中,the division by 2 表示两侧(top vs bottom, right vs left)的padding,而 the bottom and right sides 两侧需要填充剩余的padding。例如,when pad_along_height is 5, we pad 2 pixels at the top and 3 pixels at the bottom. 注意:该种padding方式与其他的深度学习框架(例如,PyTorch and Caffe)不同,其他的深度学习框架需要明确padding的数量,且在两侧padding相同的数量。

Note that this is different from existing libraries such as PyTorch and Caffe, which explicitly specify the number of padded pixels and always pad the same number of pixels on both sides.

3.1 代码示例

in_height = 5
filter_height = 3
stride_height = 2in_width = 2
filter_width = 2
stride_width = 1inp = tf.ones((2, in_height, in_width, 2))
filter = tf.ones((filter_height, filter_width, 2, 2))
strides = [stride_height, stride_width]
output = tf.nn.conv2d(inp, filter, strides, padding='SAME')
output.shape[1]  # output_height: ceil(5 / 2)=3output.shape[2] # output_width: ceil(2 / 1)=2

3.2 计算padding尺寸

已知条件:

(in_height, in_width)=(5, 2)
(filter_height, filter_width)=(3, 2)
(strides[1], strides[2])=(2, 1)

先计算Height方向的padding,可得:

in_height % strides[1] = 5%2 = 1

则满足以下公式:

pad_along_height = max(filter_height - (in_height % stride_height), 0)

代入公式,可得:

pad_along_height = max(3-(5%2), 0)=max(3-1, 0)=2
pad_top = pad_along_height // 2 = 2 // 2 = 1
pad_bottom = pad_along_height - pad_top = 2-1 = 1

由此可知,在 top 方向填充1,在 bottom 方向填充1。

再计算 Width 方向的padding,可得:

in_width % strides[2] = 2%1 = 0

则满足以下公式:

pad_along_width = max(filter_width - stride_width, 0)

代入公式,可得:

pad_along_heght = max(2-1, 0) = 1
pad_left = pad_along_width // 2 = 1 // 2 = 0
pad_right = pad_along_width - pad_left = 1-0 = 1

由此可知,在 left 方向不填充,在 right 方向填充1。

综上所述,填充的示意图如下:
在这里插入图片描述

填充之后,输入尺寸由(5,2) 扩充为 (7,3)。

3.3 计算output尺寸

标准卷积输出尺寸的计算公式:
o = i + 2 p − k s + 1 i = size of input o = size of output p = p a d d i n g k = size of kernel s = s t r i d e s ( 1 ) o=\frac{i+2p-k}s+1 \quad \begin{array}{l} \\i=\textit{size of input}\\o=\textit{size of output}\\p=padding\\k=\textit{size of kernel}\\s=strides\end{array}\quad (1) o=si+2pk+1i=size of inputo=size of outputp=paddingk=size of kernels=strides(1)
计算Height方向的输出尺寸,可得:
o u t _ h e i g h t = i n _ h e i g h t + ( p a d _ t o p + p a d _ b o t t o m ) − f i l t e r _ h e i g h t s t r i d e s [ 1 ] + 1 ( 2 ) out\_height=\frac{in\_height+(pad\_top+pad\_bottom)-filter\_height}{strides[1]}+1\quad (2) out_height=strides[1]in_height+(pad_top+pad_bottom)filter_height+1(2)
将已知条件代入上述 公式 ( 2 ) 公式(2) 公式(2) 中,可得:
o u t _ h e i g h t = 5 + ( 1 + 1 ) − 3 2 + 1 = 3 out\_height=\frac{5+(1+1)-3}{2}+1=3 out_height=25+(1+1)3+1=3
计算Width方向的输出尺寸,可得:
o u t _ w i d t h = i n _ w i d t h + ( p a d _ l e f t + p a d _ r i g h t ) − f i l t e r _ w i d t h s t r i d e s [ 2 ] + 1 ( 3 ) out\_width=\frac{in\_width+(pad\_left+pad\_right)-filter\_width}{strides[2]}+1\quad (3) out_width=strides[2]in_width+(pad_left+pad_right)filter_width+1(3)
将已知条件代入上述 公式 ( 3 ) 公式(3) 公式(3) 中,可得:
o u t _ w i d t h = 2 + ( 0 + 1 ) − 2 1 + 1 = 2 out\_width=\frac{2+(0+1)-2}{1}+1=2 out_width=12+(0+1)2+1=2
综上所述,输出尺寸为(3, 2),与代码验证的结果一致。

4. Explicit padding

在TensorFlow中,也可以指定padding的数量。但需要注意的是,padding 参数为 list 类型,而不是Tensor,且该参数的格式与 tf.pad 相同。

对于 conv2d,当 data_format='NHWC'padding 的参数格式为 [[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]] ,第一个 [[0, 0]] 表示 batch维度上no-padding不填充,最后一个 [[0, 0]] 表示 channel 维度上no-padding不填充。

For example, in the 2D case, the list is in the format [[0, 0], [pad_top, pad_bottom], [pad_left, pad_right], [0, 0]] when data_format is its default value of 'NHWC'. The two [0, 0] pairs indicate the batch and channel dimensions have no padding, which is required, as only spatial dimensions can have padding.

4.1 代码示例

inp = tf.ones((1, 3, 3, 1))
filter = tf.ones((2, 2, 1, 1))
strides = [1, 1]
padding = [[0, 0], [1, 2], [0, 1], [0, 0]]
output = tf.nn.conv2d(inp, filter, strides, padding=padding)
tuple(output.shape)  # (1, 5, 3, 1)# Equivalently, tf.pad can be used, since convolutions pad with zeros.
inp = tf.pad(inp, padding)
# 'VALID' means to use no padding in conv2d (we already padded inp)
output2 = tf.nn.conv2d(inp, filter, strides, padding='VALID')
tf.debugging.assert_equal(output, output2)

4.2 计算padding尺寸

已知条件:

(in_height, in_width)=(3, 3)
(filter_height, filter_width)=(2, 2)
(strides[1], strides[2])=(1, 1)
(pad_top, pad_bottom)=(1, 2)
(pad_left, pad_right)=(0, 1)

从已知条件中可以看出,在 top 方向填充1,在 bottom 方向填充2。在 left 方向不填充,在 right 方向填充1。

综上所述,填充的示意图如下:
在这里插入图片描述

填充之后,输入尺寸由(3,3) 扩充为 (6,4)。

4.3 计算output尺寸

将已知条件代入上述 公式 ( 2 ) 公式(2) 公式(2) 中,可得:
o u t _ h e i g h t = 3 + ( 1 + 2 ) − 2 1 + 1 = 5 out\_height=\frac{3+(1+2)-2}{1}+1=5 out_height=13+(1+2)2+1=5
将已知条件代入上述 公式 ( 3 ) 公式(3) 公式(3) 中,可得:
o u t _ w i d t h = 3 + ( 0 + 1 ) − 2 1 + 1 = 3 out\_width=\frac{3+(0+1)-2}{1}+1=3 out_width=13+(0+1)2+1=3
综上所述,输出尺寸为(5, 3),与代码验证的结果一致。

5. 区分卷积层和池化层中的padding

卷积层与池化层中的padding不一样。对于卷积层,以零元素进行padding,再与kernel相乘(卷积操作)。对于池化层,没有相乘的过程。例如,对于一个4x4的 average pooling,其padding对最终结果没有影响。

5.1 代码示例

x_in = np.array([[[[2], [2]],[[1], [1]],[[1], [1]]]])
kernel_in = np.array([  # simulate the avg_pool with conv2d[ [[0.25]], [[0.25]] ],[ [[0.25]], [[0.25]] ]])
x = tf.constant(x_in, dtype=tf.float32)
kernel = tf.constant(kernel_in, dtype=tf.float32)
conv_out = tf.nn.conv2d(x, kernel, strides=[1, 1, 1, 1], padding='SAME')
pool_out = tf.nn.avg_pool(x, [2, 2], strides=[1, 1, 1, 1], padding='SAME')
print(conv_out.shape, pool_out.shape)
# (1, 3, 2, 1) (1, 3, 2, 1)
tf.reshape(conv_out, [3, 2]).numpy()  # conv2d takes account of padding
"""
array([[1.5, 0.75],[1., 0.5],[0.5, 0.25]], dtype=float32)
"""tf.reshape(pool_out, [3, 2]).numpy()  # avg_pool excludes padding
"""
array([[1.5, 1.5],[1., 1.],[1., 1.0]], dtype=float32)
"""

5.2 计算padding尺寸

已知条件:

(in_height, in_width)=(3, 2)
(filter_height, filter_width)=(2, 2)
(strides[1], strides[2])=(1, 1)

先计算Height方向的padding,可得:

in_height % strides[1] = 3%1 = 0

则满足以下公式:

pad_along_height = max(filter_height - stride_height, 0)

代入公式,可得:

pad_along_height = max(2-1, 0) = 1
pad_top = pad_along_height // 2 = 1 // 2 = 0
pad_bottom = pad_along_height - pad_top = 1-0 = 1

由此可知,在 top 方向填充0,在 bottom 方向填充1。

再计算 Width 方向的padding,可得:

in_width % strides[2] = 2%1 = 0

则满足以下公式:

pad_along_width = max(filter_width - stride_width, 0)

代入公式,可得:

pad_along_heght = max(2-1, 0) = 1
pad_left = pad_along_width // 2 = 1 // 2 = 0
pad_right = pad_along_width - pad_left = 1-0 = 1

由此可知,在 left 方向不填充,在 right 方向填充1。

综上所述,填充的示意图如下:
在这里插入图片描述

填充之后,输入尺寸由(3,2) 扩充为 (4,3)。

5.3 计算output尺寸

将已知条件代入上述 公式 ( 2 ) 公式(2) 公式(2) 中,可得:
o u t _ h e i g h t = 3 + ( 0 + 1 ) − 2 1 + 1 = 3 out\_height=\frac{3+(0+1)-2}{1}+1=3 out_height=13+(0+1)2+1=3
将已知条件代入上述 公式 ( 3 ) 公式(3) 公式(3) 中,可得:
o u t _ w i d t h = 2 + ( 0 + 1 ) − 2 1 + 1 = 2 out\_width=\frac{2+(0+1)-2}{1}+1=2 out_width=12+(0+1)2+1=2
综上所述,输出尺寸为(3, 2),与代码验证的结果一致。

5.4 计算卷积操作的结果

在这里插入图片描述

5.5 计算池化操作的结果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/824146.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

系列六、Consul

一、Consul 1.1、概述 Consul是一套开源的分布式服务发现和配置管理系统,由HashiCorp公司用Go语言开发。他提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个功能都可以单独使用,也可以一起使用以构建全方位的服务网格&…

AI模型训练【偏差/方差】与【欠拟合/过拟合】

在我们拿到一个数据集,高高兴兴准备训练一个模型时,会遇到欠拟合或过拟合的问题,业内也喜欢用偏差和方差这两指标去定义它们,那这些词什么意思呢?有什么方法能避免/解决 欠拟合和过拟合呢? 这其实是非常非常…

将本地工作空间robot_ws上传到gitee仓库

git config --global user.name "geniusChinaHN" git config --global user.email "12705243geniuschinahnuser.noreply.gitee.com" cd ~/robot_ws #git init#创建原始仓库时候用 git add . git commit -m "上传文件内容描述" #git remote add r…

day9--java高级编程:多线程

1 Day16–多线程01 1.1 程序概念 程序(program):是为完成特定任务、用某种语言编写的一组指令的集合。即指一段静态的代码,静态对象。 1.2 进程 1.2.1 概念 进程(process):是程序的一次执行过程,或是正在运行的一个程序。是一…

『番外篇七』SwiftUI 获取视图全局位置在 NavigationStack 中失效的解决方法

概览 在 番外篇六』SwiftUI 取得任意视图全局位置的三种方法 这篇博文里,我们详细讨论了在 SwiftUI 中获取任意视图全局坐标的几种方法。 不过,我们也从中提到了某些方法无法适用于 NavigationStack 视图,本篇博文由此应运而生。 在本篇博文种,您将学到如下内容: 概览1.…

git是什么,git入门常用基本命令

文章目录 git是什么1 .git init--初始化2.git status--检测当前文件夹下面文件状态3. git add--要管理的文件4.git add . --管理当前文件夹下的所有文件5.git commit -m--生成第一个版本6.git log--查看版本的记录 git是什么 分布式,版本控制,软件 版本…

通信原理课设(gec6818) 007:语音识别

目录 1、去科大讯飞官网下载对应的sdk 2、科大讯飞文件夹的意思 3、配置ARM的录音环境 4、编程实现语音识别 我们的需求是将一个语音文件从客户端传到服务器,因此我们最好是选用tcp 现在市面上面常用的语音识别解决方案为:科大讯飞c和百度c 离…

zookeeper之集群搭建

1. 集群角色 zookeeper集群下,有3种角色,分别是领导者(Leader)、跟随着(Follower)、观察者(Observer)。接下来我们分别看一下这三种角色的作用。 领导者(Leader): 事务请求(写操作)的唯一调度者和处理者,保…

【C语言】一篇文章深入解析联合体和枚举且和结构体的区别

文章目录 📝前言🌠 联合体类型的声明🌉联合体的特点 🌠相同成员的结构体和联合体对⽐🌉联合体⼤⼩的计算 🌠联合体应用🌉枚举类型的声明 🌠枚举类型的优点🌉 枚举类型的使…

MacBook查看本机IP

嘚吧嘚 其实这也不是什么困难的问题,但是今年刚刚入坑Mac,外加用的频率不是很高,每次使用的时候都查,用完就忘,下次用的时候再查🤮。真的把自己恶心坏了🙈。 所以写篇文章记录一下&#x1f92…

软件设计师——软件工程(三)

📑前言 本文主要是【软件工程】——软件设计师——软件工程的文章,如果有什么需要改进的地方还请大佬指出⛺️ 🎬作者简介:大家好,我是听风与他🥇 ☁️博客首页:CSDN主页听风与他 &#x1f304…

Rust开发⼲货集(1)--迭代器与消费器

本内容是对 Rust开发干货集[1] 的实践与扩展. iter() 不转移所有权 先简单解释下什么叫"转移所有权": 在 Rust 中,"转移所有权"(Ownership Transfer)是一种核心概念,它涉及变量和数据的所有权从一个实体转移…

基于AM62x的ARM+FPGA+Codesys低成本软PLC解决方案

GPMC并口简介 GPMC(General Purpose Memory Controller)是TI处理器特有的通用存储器控制器接口,支持8/16bit数据位宽,支持128MB访问空间,最高时钟速率133MHz。GPMC是AM62x、AM64x、AM437x、AM335x、AM57x等处理器专用于与外部存储器设备的接口…

2023年03月22日_腾讯2022年财报解读

文章目录 1 - 腾讯营收增长停滞2 - 腾讯游戏业务低迷3 - 小程序和视频号拉动广告增长4 - 腾讯云和金融科技表现不佳5 - 营销费用减半6 - 裁员但福利上涨 2023年03月22日 今天晚上呢 腾讯披露了2022年第四季度和全年的财报 看过之后呢不禁要说 腾讯在2022年真的是过得不容易啊…

【Java开发岗面试】八股文—操作系统

声明: 背景:本人为24届双非硕校招生,已经完整经历了一次秋招,拿到了三个offer。本专题旨在分享自己的一些Java开发岗面试经验(主要是校招),包括我自己总结的八股文、算法、项目、HR面和面试技巧…

3D视觉-结构光测量-线结构光测量

概述 线结构光测量中,由激光器射出的激光光束透过柱面透镜扩束,再经过准直,产生一束片状光。这片光束像刀刃一样横切在待测物体表面,因此线结构光法又被成为光切法。线结构光测量常采用二维面阵 CCD 作为接受器件,因此…

【Web】vulhub-httpd apache解析漏洞复现(1)

目录 ①CVE-2017-15715 ②apache_parsing_vulnerability ①CVE-2017-15715 贴出源码&#xff1a; <?php if(isset($_FILES[file])) {$name basename($_POST[name]);$ext pathinfo($name,PATHINFO_EXTENSION);if(in_array($ext, [php, php3, php4, php5, phtml, pht]))…

python+django大自然环境保护宣传网站62r9b

本课题使用Python语言进行开发。基于web,代码层面的操作主要在PyCharm中进行&#xff0c;将系统所使用到的表以及数据存储到MySQL数据库中 本系统由后台管理子系统&#xff0c;登录子系统&#xff0c;按登陆角色及权限划分为管理员:个人中心&#xff0c;用户管理&#xff0c;文…

macos 打开终端提示 You have new mail. 去除方法

这个提示信息是macos里面的mail消息提示, 如果需要查看详细的信息可以在终端输入 mail 命令即可查看所有信息, 这些信息都保存在 /private/var/mail/xxx 文件中 xxx 是你的macos的登录用户名, 要去除这些提示,只需要删除这个文件即可 # 删除mail信息存储文件 sudo rm -rf /…

androidStudio 没有新建flutter工程的入口?

装了flutter dart 插件 执行了 flutter doctor 也执行了 flutter doctor --android-license 最后重启了 androidStudio 还是没发现在哪新建flutter项目工程 原来 plugins 下的 Android APK Support没有勾选