膨胀卷积,也被称为空洞卷积或扩张卷积,是一种特殊的卷积运算,它在标准卷积的基础上引入了一个额外的超参数,即膨胀率(dilation rate)。这个超参数决定了在卷积核的元素之间插入多少额外的空间。通过这种方式,膨胀卷积能够在不增加计算复杂度的同时,扩大卷积运算的采样范围,从而增加感受野(receptive field)的大小。感受野指的是卷积神经网络中某一层输出结果中一个元素所对应的输入层的区域大小,它代表了卷积核在图像上看到的区域大小。感受野越大,包含的上下文关系越多,有利于捕捉更广泛的图像信息。
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoaderdata_transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor()]
)
test_data=torchvision.datasets.CIFAR10('./dataset',train=False,transform=data_transform,download=True)
dataloader=DataLoader(dataset=test_data,batch_size=64)
class Yizhou(nn.Module):def __init__(self) -> None:super().__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 xyizhou=Yizhou()
print(yizhou)
输出的是init中定义的卷积
Yizhou(
(conv1): Conv2d(3, 6, kernel_size=(3, 3), stride=(1, 1))
)
卷积后的结果是H-kernel_size +1,W也是
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoaderdata_transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor()]
)
test_data=torchvision.datasets.CIFAR10('./dataset',train=False,transform=data_transform,download=True)
dataloader=DataLoader(dataset=test_data,batch_size=64)
class Yizhou(nn.Module):def __init__(self) -> None:super().__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 xyizhou=Yizhou()
for data in dataloader:imgs,targets=dataoutput=yizhou(imgs)print(imgs.shape)print(output.shape)
如图所示可得输出3通道转为了6通道
大小变为了30x30
一个错误:
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriterdata_transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor()]
)
test_data=torchvision.datasets.CIFAR10('./dataset',train=False,transform=data_transform,download=True)
dataloader=DataLoader(dataset=test_data,batch_size=64)
class Yizhou(nn.Module):def __init__(self) -> None:super().__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 xyizhou=Yizhou()
step=0
writer=SummaryWriter('../logs')
for data in dataloader:imgs,targets=dataoutput=yizhou(imgs)print(imgs.shape)print(output.shape)writer.add_images('input',imgs,step)writer.add_images('output',output,step)step=step+1
这里出现了报错
因为add_images方法一般只接受三通道CHW或者1通道的
因此要用reshape方法进行调整
import torch
import torchvision
from torch import nn
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriterdata_transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor()]
)
test_data=torchvision.datasets.CIFAR10('./dataset',train=False,transform=data_transform,download=True)
dataloader=DataLoader(dataset=test_data,batch_size=64)
class Yizhou(nn.Module):def __init__(self) -> None:super().__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 xyizhou=Yizhou()
step=0
writer=SummaryWriter('logs')#../是父文件夹
for data in dataloader:imgs,targets=dataoutput=yizhou(imgs)print(imgs.shape)print(output.shape)writer.add_images('input',imgs,step)output=torch.reshape(output,(-1,3,30,30))#这里的-1指的是占位,让torch自行计算batchsizewriter.add_images('output',output,step)#SummaryWriter 的 add_images 方法希望输入张量有1个或3个通道#因为这里输出的是6通道,我们需要将6通道转为3通道,多余的放在batchsize里面step=step+1
writer.close()
卷积层:多少个卷积核就输出多少层