Pytorch中的卷积操作

Pytorch中有两个函数可以用来进行卷积操作:1)torch.nn中Conv2d;2)torch.nn.functional中的conv2d
今天主要介绍一下第二个函数。它的函数调用方式如下:

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

其中input代表的是输入图像/矩阵(这里限制输入必须是以[minibatch,channel,H,W]这样的shape输入)
weight代表的是卷积核(同样是要求以上面的shape输入)
stride代表的是卷积核在输入图像上移动的步长
padding代表的是进行卷积之前是否对输入图像进行边缘填充(默认不填充)
具体代码如下:

import torch 
import torch.nn.functional as F

#卷积操作

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]])

kernel = torch.tensor([[1,2,1],
                       [0,1,0],
                       [2,1,0]])

print(input.shape)   # [5,5]
print(kernel.shape)  # [3,3]
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))

print(input.shape)    # [1,1,5,5]
print(kernel.shape)   # [1,1,3,3]

# stride=1,卷积核每次移动一个元素
output1 = F.conv2d(input, kernel, stride=1)
print("stride=1,不进行padding,卷积操作后:")
print(output1)
print(output1.shape)

# padding=1, 边界填充一格,元素设置为0,扩充完之后是一个7*7的矩阵

output2 = F.conv2d(input, kernel, stride=1, padding=1)
'''
[[0,0,0,0,0,0,0]
 [0,1,2,0,3,1,0],
 [0,0,1,2,3,1,0],
 [0,1,2,1,0,0,0],
 [0,5,2,3,1,1,0],
 [0,2,1,0,1,1,0]
 [0,0,0,0,0,0,0]]
'''
print("stride=1,padding=1,卷积操作后:")
print(output2)
print(output2.shape)

# stride=2,卷积核每次移动2个元素
output3 = F.conv2d(input, kernel, stride=2)
print("stride=2,不进行padding,卷积操作后:")
print(output3)
print(output3.shape)

运行上面的代码得到的结果:
在这里插入图片描述
因为conv2d的input以及weight都有输入限制,因此在进行卷积之前,使用了torch.reshape改变了矩阵的shape

猜你喜欢

转载自blog.csdn.net/Dartao/article/details/124195906