CNN中的卷积运算是为了做特征提取。
卷积操作:
import torch
import torchvision
import torch.nn.functional as F
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
# 输入数据
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]])
# 尺寸变换
input = torch.reshape(input,(1,1,5,5)) # 5*5
kernel = torch.reshape(kernel,(1,1,3,3)) # 3*3
print(input.shape)
print(kernel.shape)
# 卷积操作 stride表示一次移动几格
output = F.conv2d(input,kernel,stride=1)
print(output)
# tensor([[[[10, 12, 12],
# [18, 16, 16],
# [13, 9, 3]]]])
output2 = F.conv2d(input,kernel,stride=2)
print(output2)
# tensor([[[[10, 12],
# [13, 3]]]])
# padding=1 表示四边各加一行或一列 全部由0填充
output3 = F.conv2d(input,kernel,stride=1,padding=1)
print(output3)
# 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]]]])
图片展示卷积操作流程:
Conv2d函数的使用:(二维卷积,处理二维数据)
原型:Conv2d(in_channels, out_channels, kernel_size, stride=1,padding=0, dilation=1, groups=1,bias=True, padding_mode=‘zeros’)
参数:
# conv2d的使用
dataset = torchvision.datasets.CIFAR10(root="./dataset",train=False,transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset,batch_size=64)
class NnModule(nn.Module):
def __init__(self):
super(NnModule, self).__init__()
# kernel_size:卷积核大小
self.conv1=Conv2d(in_channels=3,out_channels=6,kernel_size=3,stride=1,padding=0)
def forward(self,x):
x = self.conv1(x) # 对x进行卷积操作
return x
nnModule = NnModule()
writer = SummaryWriter("logs")
step=0
for data in dataloader:
imgs,targets = data
output = nnModule(imgs) # imgs是网络的输入
print(imgs.shape)
# torch.Size([64, 3, 32, 32])
print(output.shape)
# torch.Size([64, 6, 30, 30]) -> [xxx,3,30,30] 否则报错
writer.add_images("input",imgs,step)
output = torch.reshape(output,(-1,3,30,30))
writer.add_images("output", output, step)
step=step+1