一、卷积操作
由以下输入图像和卷积核做卷积操作
代码如下:
import torch
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)
print(kernel.shape)
运行结果如下:
torch.Size([5, 5])
torch.Size([3, 3])
再由cov2d的参数规定:输入有四个参数,而运行结果显示只有两个(只有高和宽),所以得改成四维,采用torch.reshape( )
运行如下代码:
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]])
input = torch.reshape(input, [1, 1, 5, 5])
kernel = torch.reshape(kernel, [1, 1, 3, 3])
print(input.shape)
print(kernel.shape)
output = F.conv2d(input, kernel, stride=1)
print(output)
结果如下:
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])
tensor([[[[10, 12, 12],
[18, 16, 16],
[13, 9, 3]]]])
再设置padding参数使得输出图像和输入图像大小一样:
output = F.conv2d(input, kernel, stride=1)
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]]]])
完整代码:
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]])
input = torch.reshape(input, [1, 1, 5, 5])
kernel = torch.reshape(kernel, [1, 1, 3, 3])
print(input.shape)
print(kernel.shape)
output = F.conv2d(input, kernel, stride=1)
print(output)
output2 = F.conv2d(input, kernel, stride=1, padding=1)
print(output2)
二、卷积层
打开Pytorch官网查看卷积层,由于输入图像是二维的,看Conv2d
再搭建一个神经网络:
准备dataset和dataloader,dataset采用Pytorch内置的数据集,跟前面的视频的内容一样,然后dataloader采用继承DataLoader,再定义一个神经网络,重写__init__( )方法和__forward__方法。代码如下:
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=64)
class Cow(nn.Module):
def __init__(self):
super(Cow, self).__init__()
self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
def forward(self, x):
x = self.conv1(x)
return x
cow = Cow()
print(cow)
敲代码时,注意给transform设置参数时ToTensor后面要加(),不然会下载很久以及其它错误。运行结果如下;
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ../data\cifar-10-python.tar.gz
100%|██████████| 170498071/170498071 [07:00<00:00, 405616.21it/s]
Extracting ../data\cifar-10-python.tar.gz to ../data
Cow(
(conv1): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1))
)
再运行下面代码:
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("../data", train=False, transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=64)
class Cow(nn.Module):
def __init__(self):
super(Cow, self).__init__()
self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
def forward(self, x):
x = self.conv1(x)
return x
cow = Cow()
# print(cow)
writer = SummaryWriter("../logs2")
step = 0
for data in dataloader:
imgs, targets = data
output = cow(imgs)
print(imgs.shape)
print(output.shape)
writer.add_images("input", imgs, step)
output = torch.reshape(output, (-1, 3, 30, 30))
writer.add_images("output", output, step)
step = step + 1
打开终端,输入命令:tensorboard --logdir=“…/logs2”,注意是在根目录下创建的"logs2",所以得用"…/"指令,表示返回到根目录。tensorboard结果如下: