赞
踩
对于传统的CNN,层数在25层之前,准确率会随着层数增加提高。但是一旦层数过深,网络过于庞大,准确率反而会下降。而Resnet较好的解决了这一问题。其原因在于:
Resnet具有一定的记忆性,在第一次进入某个卷积块(多个卷积层)时,Tensor0
(输入)通过卷积块进行前向传播得到Tensor1
,之后,如果Tensor0
与Tensor1
维数不一致,Tensor0
通过downsample
(下采样,包含一个卷积层和一个BatchNorm层)转成和Tensor1
维度一致的Tensor2
。然后将Tensor1
和Tensor2
或Tensor0
(Tensor0,Tensor2取决于有没有downsample)相加,最后将上一步的结果通过relu激励函数,便得到一个卷积块最终的输出。这样的方法使得计算Tensor1
等同于计算残差,而Tensor2
就等同于原输入,这样的话即使Tensor1
无法对识别做出贡献,Tensor2
也不会让其影响之后的网络,可以有效避免梯度消失等问题,这种残差网络会使得网络之间的依赖性不会特别强,不会因为前面网络的问题影响整个网络的准确性。
ps:以上仅为个人理解,如有错误,欢迎批评
2.网络结构与复现代码
上图为下述代码中Block的一种特殊情况
上图为完整的网络结构,但是还有一些细节上面没有,比如bn,relu,以及什么时候size减半,下面我写的代码有介绍
import torch.nn as nn import torch.nn.functional as F class Block(nn.Module): def __init__(self,in_planes,out_planes,stride=1,downsample=None): super(Block,self).__init__() # conv1为网络中1*1的卷积核所在的那一层,以图片为例,为1*1,64 self.conv1 = nn.Conv2d(in_planes,out_planes,kernel_size=1,stride=1,bias=False) self.bn1 = nn.BatchNorm2d(out_planes) # conv2为网络中3*3的卷积核所在的那一层,这一层的stride要根据构造函数中的stride变化, # 原因在于conv_2x卷积块的尺寸减半是maxpool的结果,所以之后的卷积不能再减小尺寸了, # 而conv3_x及以后的卷积块尺寸减半是stride=2的结果,同时要注意padding=1才能达到上述的尺寸变换 self.conv2 = nn.Conv2d(out_planes,out_planes,kernel_size=3,stride=stride,padding=1,bias=False) self.bn2 = nn.BatchNorm2d(out_planes) self.conv3 = nn.Conv2d(out_planes,out_planes*4,kernel_size=1,stride=1,bias=False) self.bn3 = nn.BatchNorm2d(out_planes*4) self.relu = nn.ReLU(inplace=True) # downsample默认为None,是否执行downsample由Resnet中make_layers决定, # 只有第一次进入conv_2x,conv_3x,conv_4x,conv_5x时才会downsample self.downsample = downsample def forward(self,x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(x) out = self.bn2(out) out = self.relu(out) out = self.relu(out) out = self.bn3(out) if self.downsample is not None: residual = self.downsample(x) # 下面就是将残差与最初输入的tensor或者downsample的tensor相加 out += residual out = self.relu(out) return out class Resnet(nn.Module): def __init__(self,resnet_layers=50,num_classes=100): #这是我自己定义的字典,以后使用时只要在创建Resnet时传入resnet层数 # 便可创建对应的resnet,如resnet50,resnet101,resnet152 self.layers_dic = {50:[3,4,6,3],101:[3,4,23,3],152:[3,8,36,3]} # 首先可以指定最初输入的filter数量,其次可用来记忆,看是否需要下采样 self.in_planes=64 super(Resnet,self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,bias=False) self.bn1 = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) self.maxPool = nn.MaxPool2d(kernel_size=3,stride=2,padding=1) self.conv2_x = self.make_layers(self.layers_dic[resnet_layers][0],stride=1,planes=64) self.conv3_x = self.make_layers(self.layers_dic[resnet_layers][1],stride=2,planes=128) self.conv4_x = self.make_layers(self.layers_dic[resnet_layers][2],stride=2,planes=256) self.conv5_x = self.make_layers(self.layers_dic[resnet_layers][1],stride=2,planes=512) self.avgpool = nn.AvgPool2d(7, stride=1) self.fc = nn.Linear(512 * 4, num_classes) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) def make_layers(self,layersNum,stride,planes): downsample = None # 判断是否需要下采样(其实此处没有if语句也行) if stride!=1 or self.in_planes != planes*4: downsample = nn.Sequential( nn.Conv2d(self.in_planes, planes *4,kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(planes *4), ) layers=[] layers.append(Block(self.in_planes,planes,stride,downsample)) self.in_planes = self.in_planes*4 for i in range(1,layersNum): layers.append(Block(self.in_planes,planes)) return nn.Sequential(*layers) def forward(self,x): # conv1 out=self.conv1(x) out=self.bn1(out) out=self.relu(out) # conv2_x out=self.maxPool(out) out=self.conv2_x(out) # conv3_x out=self.conv3_x(out) # conv4_x out=self.conv4_x(out) # conv5_x out=self.conv5_x(out) # average pool and fc out=self.avgpool(out) out=out.view(out.size(0),-1) out=self.fc(out) return out
如果有疑问的话,可以自己把网络信息利用上述代码打印出来对比着看
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。