Conv2d、conv2d是pytorch中进行卷积操作的2个类,虽然只是首字母大小写不同,使用起来方法也不一样,一个是类,一个是函数。
1 Conv2d
Conv2d是torch.nn中的类
1.1 初始化
CLASS
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)
参数说明
-
in_channels (int) – 输入图像通道
-
out_channels (int) – 输出图像通道,也就是用了多少层卷积核
-
kernel_size (int or tuple) – 卷积核大小
-
stride (int or tuple, optional) – 步长. Default: 1
-
padding (int, tuple or str, optional) – Padding 大小. Default: 0
-
padding_mode (string, optional) – 填充模式,默认填充0,
'zeros'
,'reflect'
,'replicate'
or'circular'
. Default:'zeros'
-
dilation (int or tuple, optional) – Spacing between kernel elements. Default: 1,一般不用管
-
groups (int, optional) – Number of blocked connections from input channels to output channels. Default: 1,一般不用管
-
bias (bool, optional) –是否添加偏值, Default:
True
样例
from torch.nn import Conv2d
conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
print(conv1)
输出
Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1))
1.2 输入输出大小
- N:样本数
- C:通道数
- H:高
- W:宽
import torch
input = torch.randn(1, 3, 5, 5)
output = conv1(input)
print(input.shape)
print(output.shape)
输出
torch.Size([1, 3, 5, 5])
torch.Size([1, 6, 3, 3])
2 conv2d
conv2d是torch.nn.functional中的函数
2.1 代码格式
torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)
参数说明
-
input –输入一个tensor
-
weight – 卷积核,也是一个tensor
-
bias – 偏值,tensor,大小是输出通道的大小. Default:
None
-
stride –步长. Default: 1
-
padding –{‘valid’, ‘same’},默认valid,,也可以是int数值
-
dilation – the spacing between kernel elements. Can be a single number or a tuple (dH, dW). Default: 1,不用管
-
groups – split input into groups, \text{in\_channels}in_channels should be divisible by the number of groups. Default: 1,不用管
2.2 输入输出大小
input = torch.tensor([[1, 2, 0, 3, 1],[0, 1, 3, 3, 1],[1, 2, 1, 0, 0],[5, 5, 3, 1, 4],[3, 1, 0, 4, 1]])kernel = torch.tensor([[1, 1, 1],[0, 2, 0],[2, 1, 0]])input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))print(input.shape)
print(kernel.shape)
输出
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])
样例
import torch.nn.functional as F
output = F.conv2d(input, kernel, stride=1, padding=“same”)
print(output)
输出
tensor([[[[ 1, 3, 4, 10, 8],[ 5, 10, 12, 12, 6],[ 7, 18, 16, 16, 8],[11, 13, 9, 3, 4],[14, 13, 9, 7, 4]]]])
本文链接:https://my.lmcjl.com/post/2219.html
4 评论