卷积网络的使用
用2d用的多,1、3用的很少。
参数Parameters
- in_channels (int) –输入图像的通道数,需设置,彩色图像一般是3通道 Number of channels in the input image
- out_channels (int) –通过卷积之后输出的通道。 Number of channels produced by the convolution
- kernel_size (int or tuple) – 卷积核大小,如果是整数则为正方形,如果是元组则是指定大小。Size of the convolving kernel
- stride (int or tuple, optional) –卷积过程中步径大小, Stride of the convolution. Default: 1
- padding (int, tuple or str, optional) –卷积过程中输入图像边缘是否需要填充,默认为0, Padding added to all four sides of the input. Default: 0
- padding_mode (string*,* optional) –卷积的模式选择,默认0填充,还是其他填充。
'zeros'
,'reflect'
,'replicate'
or'circular'
. Default:'zeros'
- dilation (int or tuple, optional) –卷积核每个对应位距离。默认为1。 Spacing between kernel elements. Default: 1
- groups (int, optional) – 一般设为1,除非空度卷积?分度卷积。Number of blocked connections from input channels to output channels. Default: 1
- bias (bool, optional) –常设置为true。看是否对卷积结果+-一个常数。 If
True
, adds a learnable bias to the output. Default:True
- 需设置参数只有in_channel , out_channel , ternel_size。其余可默认。常用还是有stride, padding。
in&out
详细看看。inchannel👉
outchannel👉
in&out都为1时,用1卷积核进行计算。
in=1,out=2,卷积层会生成两个卷积核,就会得到两个out。
很多算法会不断增加channel数。
instance 实例
import torchvision
from tensorboardX import SummaryWriter
from torch import nn
from torch.utils.data import DataLoader
test_data = torchvision.datasets.CIFAR10(root="hymenoptera_data/val/CIFAR10",
train = False,
transform = torchvision.transforms.ToTensor())
data_loader = DataLoader(dataset = test_data,
batch_size = 64)
class Tudui(nn.Module):
def __init__(self):
#完成父类的初始化
super(Tudui , self).__init__()
#定义卷积层
#使用self,类中其他函数也可以使用
#in_channels = 3 -->输入是彩色图像,所以是3层
#out_channels = 6 -->想让它输出6层
#kernel_size = 3 -->3*3的卷积核
#stride = 1 , padding = 0 -->默认即可
self.conv1 = nn.Conv2d(in_channels = 3 , out_channels = 6,
kernel_size = 3 , stride = 1,padding = 0)
def forward(self , x):
output = self.conv1(x)
return output
tudui = Tudui()
print(f"the network of tudui is {tudui}")
step = 0
writer = SummaryWriter('tb_logs')
for data in data_loader:
imgs , targets = data
print(f"type of imgs = {type(imgs)}")
print(f"type of targets = {type(targets)}")
output = tudui(imgs)
print(f"type of output = {type(output)}")
print(f"input imgs` shape = {(imgs.shape)}")
print(f"output imgs` shape = {(output.shape)}")
writer.add_images("conv1" , output, step )
step+=1
writer.close()
运行结果👇
可以看到,输入图像batch_size是64,3通道,大小是32$\times$32
输出图像batch_size一致,但是6通道,大小是30$\times$30。
6通道的图像无法显示。
解决方案:
output = torch.reshape(output , (-1 , 3,30 ,30))
添加到writer.add_images之前,reshape第二个参数第一个数不知道是多少时,可以填-1,会自动计算。
修改后代码:
import torch
import torchvision
from tensorboardX import SummaryWriter
from torch import nn
from torch.utils.data import DataLoader
test_data = torchvision.datasets.CIFAR10(root="hymenoptera_data/val/CIFAR10",
train = False,
transform = torchvision.transforms.ToTensor())
data_loader = DataLoader(dataset = test_data,
batch_size = 64)
class Tudui(nn.Module):
def __init__(self):
#完成父类的初始化
super(Tudui , self).__init__()
#定义卷积层
#使用self,类中其他函数也可以使用
#in_channels = 3 -->输入是彩色图像,所以是3层
#out_channels = 6 -->想让它输出6层
#kernel_size = 3 -->3*3的卷积核
#stride = 1 , padding = 0 -->默认即可
self.conv1 = nn.Conv2d(in_channels = 3 , out_channels = 6,
kernel_size = 3 , stride = 1,padding = 0)
def forward(self , x):
output = self.conv1(x)
return output
tudui = Tudui()
print(f"the network of tudui is {tudui}")
step = 0
writer = SummaryWriter('tb_logs')
for data in data_loader:
imgs , targets = data
print(f"type of imgs = {type(imgs)}")
print(f"type of targets = {type(targets)}")
output = tudui(imgs)
print(f"type of output = {type(output)}")
print(f"input imgs` shape = {(imgs.shape)}")
print(f"output imgs` shape = {(output.shape)}")
#大小不变,通道数变为3,首位不知道是多少,所以写-1,让它自动算
output = torch.reshape(output , (-1,3 ,30,30 ))
writer.add_images("conv1" , output, step )
step+=1
print("="*100)
writer.close()
打开tensorboard查看