当前位置:网站首页>pytorch 卷积操作
pytorch 卷积操作
2022-07-01 04:35:00 【booze-J】
文章
pytorch卷积操作官方文档
这里我们用nn.conv2d来讲解卷积操作。
什么是卷积?
就是卷积核在输入图像上移动,然后将卷积核上与输入图像上对应位置上的值相乘求和。Stride=1使用来控制卷积核的移动步长的。
卷积操作示例代码:
import torch.nn.functional as F
import torch
# 输入图像(5X5)
input = torch.tensor([[1,2,0,3,1],
[0,1,2,3,1],
[1,2,1,0,0],
[5,2,3,1,1],
[2,1,0,1,1]])
# 卷积核(3X3)
kernel = torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])
# input: torch.Size([5, 5])
print("input:\n",input.shape)
# kernel:torch.Size([3, 3])
print("kernel:\n",kernel.shape)
input = torch.reshape(input,(1,1,5,5))
kernel = torch.reshape(kernel,(1,1,3,3))
# input:torch.Size([1, 1, 5, 5])
print("input:\n",input.shape)
# kernel:torch.Size([1, 1, 3, 3])
print("kernel:\n",kernel.shape)
# 进行卷积操作 观察stride对卷积结果的影响
output = F.conv2d(input,kernel,stride=1)
print('output\n',output)
output2 = F.conv2d(input,kernel,stride=2)
print('output2\n',output2)
# 进行卷及操作 对输入图像进行边界进行扩展填充 观察padding对卷积结果的影响
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print("output\n",output3)
部分代码解释:
1.reshape的作用
# reshape前
# input: torch.Size([5, 5]) kernel:torch.Size([3, 3])
input = torch.reshape(input,(1,1,5,5))
kernel = torch.reshape(kernel,(1,1,3,3))
# reshape后
# input:torch.Size([1, 1, 5, 5]) kernel:torch.Size([1, 1, 3, 3])
为什么需要对input和kernel进行reshape这个操作呢?
因为使用torch.nn.functional.conv2d对输入的参数进行了限制,可以看到conv2d对输入参数的要求,要求input的输入是(minibatch,in_channels,iH,iW),其中in_channels表示通道数,iH表示输入图像的高,iW表示输入图像的宽。weigt的输入是kernel(卷积核),可以看到对weight的参数要求类似于input,其中outchannels表示输出通道数,in_channels表示输入通道数(groups默认等于1),kH表示卷积核的高,kW表示卷积核的宽。所以需要对input和kernel进行reshape操作。
2.stride参数
# 进行卷积操作 观察stride对卷积结果的影响
output = F.conv2d(input,kernel,stride=1)
print('output\n',output)
output2 = F.conv2d(input,kernel,stride=2)
print('output2\n',output2)
运行结果:
可以看到官方文档对Stride的解释:
- stride – the stride of the convolving kernel. Can be a single number or a tuple (sH, sW). Default: 1
当stride输入的是一个数时,则这个数为卷积核横向和纵向的移动步长,当stride输入的是一个元组的时候,可以分别设置卷积核横纵向移动的步长。
3.padding参数
# 进行卷及操作 对输入图像进行边界进行扩展填充 观察padding对卷积结果的影响
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print("output\n",output3)
在上述代码中padding参数的作用相当于就是,对输入图像横向和纵向边界进行扩展1个长度并填充0,再进行卷积操作。
可以看到官方文档对Padding的解释:
- padding – implicit paddings on both sides of the input. Can be a string {‘valid’, ‘same’}, single number or a tuple (padH, padW). Default: 0 padding=‘valid’ is the same as no padding. padding=‘same’ pads the input so the output has the same shape as the input. However, this mode doesn’t support any stride values other than 1.
当padding输入的是一个数时,则这个数为图像横向和纵向边界扩展填充(默认填充值为0)的长度,当padding输入的是一个元组的时候,可以分别设置图像横纵向边界扩展填充的长度。
边栏推荐
- 2022年化工自动化控制仪表操作证考试题库及答案
- Introduction to JVM stack and heap
- Codeforces Round #771 (Div. 2) ABCD|E
- Summary of testing experience - Testing Theory
- VR线上展览所具备应用及特色
- JS rotation chart
- About the transmission pipeline of stage in spark
- 2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
- 总结全了,低代码还需要解决这4点问题
- Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
猜你喜欢

JS image path conversion Base64 format
![[2020 overview] overview of link prediction based on knowledge map embedding](/img/69/22983c5f37bb67a8dc0e2b87c73238.jpg)
[2020 overview] overview of link prediction based on knowledge map embedding

Rule method: number of effective triangles

Maixll-Dock 快速上手

Offline installation of Wireshark 2.6.10
![[ue4] event distribution mechanism of reflective event distributor and active call event mechanism](/img/44/6a26ad24d56ddd5156f3a31fa7e0b9.jpg)
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism

2022 hoisting machinery command registration examination and hoisting machinery command examination registration

Programs and processes, process management, foreground and background processes

Task04 | statistiques mathématiques

Internet winter, how to spend three months to make a comeback
随机推荐
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism
2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
PgSQL failed to start after installation
Codeforces Round #771 (Div. 2) ABCD|E
Summary of acl2021 information extraction related papers
[learn C and fly] S1E20: two dimensional array
How to use maixll dock
Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
TCP server communication flow
什么是uid?什么是Auth?什么是验证器?
Pytorch(四) —— 可视化工具 Visdom
[godot] unity's animator is different from Godot's animplayer
Extension fragment
Daily question - line 10
【硬十宝典】——1.【基础知识】电源的分类
Obtain detailed ideas for ABCDEF questions of 2022 American Games
OdeInt与GPU
使用WinMTR软件简单分析跟踪检测网络路由情况
Pytorch(二) —— 激活函数、损失函数及其梯度
做网站数据采集,怎么选择合适的服务器呢?