当前位置:网站首页>PyTorch中 nn.Conv2d与nn.ConvTranspose2d函数的用法
PyTorch中 nn.Conv2d与nn.ConvTranspose2d函数的用法
2022-07-26 14:31:00 【代码的路】
1. 通道数问题
描述一个像素点,如果是灰度,那么只需要一个数值来描述它,就是单通道。如果有RGB三种颜色来描述它,就是三通道。最初输入的图片样本的 channels,取决于图片类型;卷积操作完成后输出的 out_channels,取决于卷积核的数量。此时的out_channels也会作为下一次卷积时的卷积核的in_channels;卷积核中的 in_channels,上面已经说了,就是上一次卷积的out_channels,如果是第一次做卷积,就是样本图片的channels。
举个例子,假设现有一个为 6×6×3的图片样本,使用 3×3×3 的卷积核(filter)进行卷积操作。此时输入图片的 channels 为 3,而卷积核中的 in_channels 与需要进行卷积操作的数据的 channels 一致(就是图片样本,为3)。接下来进行卷积操作,卷积核中的27个数字与分别与样本对应相乘后,再进行求和,得到第一个结果。依次进行,最终得到 4×4的结果。由于只有一个卷积核,所以最终得到的结果为 4×4×1,out_channels 为 1。如下图所示:

在实际应用中,都会使用多个卷积核。这里如果再加一个卷积核,就会得到 4×4×2的结果。如下图所示:

2. nn.Conv2d
class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True)
nn.Conv2d的功能是:对由多个输入平面组成的输入信号进行二维卷积。输入信号的形式为:
(1)参数说明
N:表示batch size(批处理参数)
**C_{in}**:表示channel个数
H,W:分别表示特征图的高和宽。
stride(步长):步长,默认为1,可以设为1个int型数或者一个(int, int)型的tuple。
kernel_size:卷积核的宽度和长度,单个整数或由两个整数构成的list/tuple。如为单个整数,则表示在各个空间维度的相同长度。
padding(补0):控制zero-padding的数目,padding是在卷积之前补0。
dilation(扩张):控制kernel点(卷积核点)的间距; 可以在此github地址查看:Dilated convolution animations
groups(卷积核个数):通常来说,卷积个数唯一,但是对某些情况,可以设置范围在1 —— in_channels中数目的卷积核:
(2)图像尺寸
经过一次卷积之后,生成的图的大小:
(original_size - (kernal_size - 1)) / stride
3. nn.ConvTranspose2d
nn.ConvTranspose2d的功能是进行反卷积操作
(1)输入格式
nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1)
(2)参数的含义
in_channels(int) – 输入信号的通道数 out_channels(int) – 卷积产生的通道数 kerner_size(int or tuple) - 卷积核的大小 stride(int or tuple,optional) - 卷积步长,即要将输入扩大的倍数。 padding(int or tuple, optional) - 输入的每一条边补充0的层数,高宽都增加2*padding output_padding(int or tuple, optional) - 输出边补充0的层数,高宽都增加padding groups(int, optional) – 从输入通道到输出通道的阻塞连接数 bias(bool, optional) - 如果bias=True,添加偏置 dilation(int or tuple, optional) – 卷积核元素之间的间距
学习更多编程知识,请关注我的公众号:

边栏推荐
猜你喜欢

My creation Anniversary - from the heart

Multi task text classification model based on tag embedded attention mechanism

C common function integration

【无标题】

The development of smart home industry pays close attention to edge computing and applet container technology

Plato farm is expected to further expand its ecosystem through elephant swap

Canvas laser JS special effect code

Use of LINGO software
![[Yugong series] July 2022 go teaching course 017 - if of branch structure](/img/e4/b3aa4b8bda738aadded3127a8b3485.png)
[Yugong series] July 2022 go teaching course 017 - if of branch structure

Jzoffer51- reverse pairs in the array (merge sort solution)
随机推荐
《MySQL高级篇》五、InnoDB数据存储结构
请问数据库规范的文档吗 参考一下?
Mysql-03 database operation
Keyboard shortcut to operate the computer (I won't encounter it myself)
C nanui related function integration
C# NanUI 相关功能整合
Win11运行虚拟机死机了?Win11运行VMware虚拟机崩溃的解决方法
请问下大家,flink sql有没有办法不输出update_before?
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
VP视频结构化框架
Fill in the questionnaire and receive the prize | we sincerely invite you to fill in the Google play academy activity survey questionnaire
c# 用移位 >> 和运算与 &判断两个 二进制数 是否发生过改变
C# 常用功能整合
Embedded development: skills of debugging embedded software
TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
Summary and analysis of image level weakly supervised image semantic segmentation
[untitled]
C common function integration
SSH that must be read on cloud native
Flask send_ Absolute path traversal caused by file function