resnet代码分析

时间:2021-10-01 02:38:44

1.

先导入使用的包,并声明可用的网络和预训练好的模型

import torch.nn as nn
import torch.utils.model_zoo as model_zoo #声明可调用的网络
__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
'resnet152'] #用于加载的预训练好的模型
model_urls = {
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
}

2.

定义要使用到的1*1和3*3的卷积层

#卷积核为3*,padding=,stride=1(默认,根据实际传入参数设定),dilation=,groups=,bias=False的二维卷积
def conv3x3(in_planes, out_planes, stride=):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=, stride=stride,
padding=, bias=False) #卷积核为1*,padding=,stride=1(默认,根据实际传入参数设定),dilation=,groups=,bias=False的二维卷积
def conv1x1(in_planes, out_planes, stride=):
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=, stride=stride, bias=False)

注意:这里bias设置为False,原因是:

下面使用了Batch Normalization,而其对隐藏层 resnet代码分析 有去均值的操作,所以这里的常数项 resnet代码分析可以消去

因为Batch Normalization有一个操作resnet代码分析,所以上面resnet代码分析的数值效果是能由resnet代码分析所替代的

因此我们在使用Batch Norm的时候,可以忽略各隐藏层的常数项 resnet代码分析 。

这样在使用梯度下降算法时,只用对 resnet代码分析 , resnet代码分析和 resnet代码分析 进行迭代更新

3.

实现两层的残差块

比如:

resnet代码分析

#这个实现的是两层的残差块,用于resnet18/
class BasicBlock(nn.Module):
expansion = def __init__(self, inplanes, planes, stride=, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride def forward(self, x):
identity = x out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out) out = self.conv2(out)
out = self.bn2(out) if self.downsample is not None: #当连接的维度不同时,使用1*1的卷积核将低维转成高维,然后才能进行相加
identity = self.downsample(x) out += identity #实现H(x)=F(x)+x或H(x)=F(x)+Wx
out = self.relu(out) return out

4.实现3层的残差块

如图:

resnet代码分析

#这个实现的是三层的残差块,用于resnet50//
class Bottleneck(nn.Module):
expansion = def __init__(self, inplanes, planes, stride=, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = conv1x1(inplanes, planes)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = conv3x3(planes, planes, stride)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = conv1x1(planes, planes * self.expansion)
self.bn3 = nn.BatchNorm2d(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride def forward(self, x):
identity = x out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out) out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out) out = self.conv3(out)
out = self.bn3(out) if self.downsample is not None:
identity = self.downsample(x) #当连接的维度不同时,使用1*1的卷积核将低维转成高维,然后才能进行相加 out += identity #实现H(x)=F(x)+x或H(x)=F(x)+Wx
out = self.relu(out) return out

5.整个网络实现

class ResNet(nn.Module):
#参数block指明残差块是两层或三层,参数layers指明每个卷积层需要的残差块数量,num_classes指明分类数,zero_init_residual是否初始化为0
def __init__(self, block, layers, num_classes=, zero_init_residual=False):
super(ResNet, self).__init__()
self.inplanes = #一开始先使用64**7的卷积核,stride=, padding=
self.conv1 = nn.Conv2d(, , kernel_size=, stride=, padding=,
bias=False) #3通道的输入RGB图像数据变为64通道的数据
self.bn1 = nn.BatchNorm2d()
self.relu = nn.ReLU(inplace=True) #以上是第一层卷积--
self.maxpool = nn.MaxPool2d(kernel_size=, stride=, padding=) #然后进行最大值池化操作--
self.layer1 = self._make_layer(block, , layers[])#下面就是所有的卷积层的设置--
self.layer2 = self._make_layer(block, , layers[], stride=)
self.layer3 = self._make_layer(block, , layers[], stride=)
self.layer4 = self._make_layer(block, , layers[], stride=)
self.avgpool = nn.AdaptiveAvgPool2d((, )) #进行自适应平均池化--
self.fc = nn.Linear( * block.expansion, num_classes)#全连接层-- for m in self.modules():
if isinstance(m, nn.Conv2d):
#kaiming高斯初始化,目的是使得Conv2d卷积层反向传播的输出的方差都为1
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
#初始化m.weight,即gamma的值为1;m.bias即beta的值为0
nn.init.constant_(m.weight, )
nn.init.constant_(m.bias, ) # 在每个残差分支中初始化最后一个BN,即BatchNorm2d
# 以便残差分支以零开始,并且每个残差块的行为类似于一个恒等式。
# This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
if zero_init_residual:
for m in self.modules():
if isinstance(m, Bottleneck):#Bottleneck的最后一个BN是m.bn3
nn.init.constant_(m.bn3.weight, )
elif isinstance(m, BasicBlock):#BasicBlock的最后一个BN是m.bn2
nn.init.constant_(m.bn2.weight, ) #实现一层卷积,block参数指定是两层残差块或三层残差块,planes参数为输入的channel数,blocks说明该卷积有几个残差块
def _make_layer(self, block, planes, blocks, stride=):
downsample = None
#即如果该层的输入的channel数inplanes和其输出的channel数planes * block.expansion不同,
#那要使用1*1的卷积核将输入x低维转成高维,然后才能进行相加
if stride != or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride),
nn.BatchNorm2d(planes * block.expansion),
) layers = []
#只有卷积和卷积直接的连接需要低维转高维
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for _ in range(, blocks):
layers.append(block(self.inplanes, planes)) return nn.Sequential(*layers) def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x) x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x) x = self.avgpool(x)
x = x.view(x.size(), -)
x = self.fc(x) return x

6.不同层次网络实现

#18层的resnet
def resnet18(pretrained=False, **kwargs):
"""Constructs a ResNet-18 model. Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(BasicBlock, [, , , ], **kwargs)
if pretrained:#是否使用已经训练好的预训练模型,在此基础上继续训练
model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
return model #34层的resnet
def resnet34(pretrained=False, **kwargs):
"""Constructs a ResNet-34 model. Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(BasicBlock, [, , , ], **kwargs)
if pretrained:#是否使用已经训练好的预训练模型,在此基础上继续训练
model.load_state_dict(model_zoo.load_url(model_urls['resnet34']))
return model #50层的resnet
def resnet50(pretrained=False, **kwargs):
"""Constructs a ResNet-50 model. Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [, , , ], **kwargs)
if pretrained:#是否使用已经训练好的预训练模型,在此基础上继续训练
model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
return model #101层的resnet
def resnet101(pretrained=False, **kwargs):
"""Constructs a ResNet-101 model. Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [, , , ], **kwargs)
if pretrained:#是否使用已经训练好的预训练模型,在此基础上继续训练
model.load_state_dict(model_zoo.load_url(model_urls['resnet101']))
return model #152层的resnet
def resnet152(pretrained=False, **kwargs):
"""Constructs a ResNet-152 model. Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(Bottleneck, [, , , ], **kwargs)
if pretrained:#是否使用已经训练好的预训练模型,在此基础上继续训练
model.load_state_dict(model_zoo.load_url(model_urls['resnet152']))
return model