resnet代码分析

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*3,padding=1,stride=1(默认,根据实际传入参数设定),dilation=1,groups=1,bias=False的二维卷积
def conv3x3(in_planes, out_planes, stride=1):
    """3x3 convolution with padding"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=1, bias=False)

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

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

下面使用了Batch Normalization,而其对隐藏层 Z^{[l]}=W^{[l]}A^{[l-1]}+b^{[l]} 有去均值的操作,所以这里的常数项 b^{[l]}可以消去

因为Batch Normalization有一个操作	ilde z^{(i)}=gammacdot z^{(i)}_{norm}+eta,所以上面b^{[l]}的数值效果是能由eta所替代的

因此我们在使用Batch Norm的时候,可以忽略各隐藏层的常数项 b^{[l]} 。

这样在使用梯度下降算法时,只用对 W^{[l]} , eta^{[l]} gamma^{[l]} 进行迭代更新

3.

实现两层的残差块

比如:

#这个实现的是两层的残差块,用于resnet18/34
class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, 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层的残差块

如图:

#这个实现的是三层的残差块,用于resnet50/101/152
class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, inplanes, planes, stride=1, 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=1000, zero_init_residual=False):
        super(ResNet, self).__init__()
        self.inplanes = 64 #一开始先使用64*7*7的卷积核,stride=2, padding=3
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
                               bias=False) #3通道的输入RGB图像数据变为64通道的数据
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True) #以上是第一层卷积--1
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) #然后进行最大值池化操作--2
        self.layer1 = self._make_layer(block, 64, layers[0])#下面就是所有的卷积层的设置--3
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) #进行自适应平均池化--4
        self.fc = nn.Linear(512 * block.expansion, num_classes)#全连接层--5

        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, 1)
                nn.init.constant_(m.bias, 0)

        # 在每个残差分支中初始化最后一个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, 0)
                elif isinstance(m, BasicBlock):#BasicBlock的最后一个BN是m.bn2
                    nn.init.constant_(m.bn2.weight, 0)

    #实现一层卷积,block参数指定是两层残差块或三层残差块,planes参数为输入的channel数,blocks说明该卷积有几个残差块
    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        #即如果该层的输入的channel数inplanes和其输出的channel数planes * block.expansion不同,
        #那要使用1*1的卷积核将输入x低维转成高维,然后才能进行相加
        if stride != 1 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(1, 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(0), -1)
        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, [2, 2, 2, 2], **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, [3, 4, 6, 3], **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, [3, 4, 6, 3], **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, [3, 4, 23, 3], **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, [3, 8, 36, 3], **kwargs)
    if pretrained:#是否使用已经训练好的预训练模型,在此基础上继续训练
        model.load_state_dict(model_zoo.load_url(model_urls['resnet152']))
    return model
原文地址:https://www.cnblogs.com/wanghui-garcia/p/10775860.html