卷积
输入通道数, 输出通道数,核大小
参数具体含义
直观理解各个参数的网站(gif)
https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
大概长这样,cyan是青色的意思
channel数(终于理解论文里图片放好多层的原因了)
查看网络结构可以直接print
dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64, shuffle=True)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
def forward(self, x):
x = self.conv1(x)
return x
tudui = Tudui()
print(tudui)
注意kernel_size是(3, 3),而不是3
想自己算出输出图像的大小,可以按照下面公式进行计算
https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d
完整流程
import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64, shuffle=True)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
def forward(self, x):
x = self.conv1(x)
return x
tudui = Tudui()
writer = SummaryWriter('./logs') # ../logs是放在上级目录
step = 0
for data in dataloader:
imgs, targets = data
output = tudui(imgs)
# print(imgs.shape)
# print(output.shape)
# torch.Size([64, 3, 32, 32])
writer.add_images('input', imgs, global_step=step) # 注意是add_images
# torch.Size([64, 6, 30, 30]) -> [xxx, 3, 30, 30]
output = torch.reshape(output, (-1, 3, 30, 30))
writer.add_images('output', output, global_step=step)
step += 1
writer.close()
至于为什么最后显示的不一样,是因为kernel是随机生成的,并未设置