Pytorch ResNet源码学习

一,残差网络架构

1,残差学习单元

 

上图左对应的是浅层网络(18层,34层),而右图对应的是深层网络(50,101,152)。

1. 左图为基本的residual block,residual mapping为两个64通道的3x3卷积,输入输出均为64通道,可直接相加。该block主要使用在相对浅层网络,比如ResNet-34;

2. 右图为针对深层网络提出的block,称为“bottleneck” block,主要目的就是为了降维。首先通过一个1x1卷积将256维通道(channel)降到64通道,最后通过一个256通道的1x1卷积恢复。

ResNet使用两种残差单元,其目的主要就是为了降低参数的数目

2,残差学习好在哪里?

        随着网络加深,梯度消失,模型准确率会先上升然后达到饱和,再持续增加深度时则会导致准确率下降。

        残差跳跃式的结构,打破了传统的神经网络n-1层的输出只能给n层作为输入的惯例,使某一层的输出可以直接跨过几层作为后面某一层的输入,其意义在于为叠加多层网络而使得整个学习模型的错误率不降反升的难题提供了新的方向。至此,神经网络的层数可以超越之前的约束,达到几十层、上百层甚至千层,为高级语义特征提取和分类提供了可行性。

3,ResNet改进版本

新的残差学习单元比以前更容易训练且泛化性更强。

 

 

二,两种残差单元对应的Pytorch源码

  • a,两个卷积层的残差单元
class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
                 base_width=64, dilation=1, norm_layer=None):
        super(BasicBlock, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        if groups != 1 or base_width != 64:
            raise ValueError('BasicBlock only supports groups=1 and base_width=64')
        if dilation > 1:
            raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
        # Both self.conv1 and self.downsample layers downsample the input when stride != 1
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = norm_layer(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = norm_layer(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:
            identity = self.downsample(x)

        out += identity
        out = self.relu(out)

        return out
  • b, 有瓶颈块的三层残差单元
class Bottleneck(nn.Module):
    # Bottleneck in torchvision places the stride for downsampling at 3x3 convolution(self.conv2)
    # while original implementation places the stride at the first 1x1 convolution(self.conv1)
    # according to "Deep residual learning for image recognition"https://arxiv.org/abs/1512.03385.
    # This variant is also known as ResNet V1.5 and improves accuracy according to
    # https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch.

    expansion = 4

    def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
                 base_width=64, dilation=1, norm_layer=None):
        super(Bottleneck, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        width = int(planes * (base_width / 64.)) * groups
        # Both self.conv2 and self.downsample layers downsample the input when stride != 1
        self.conv1 = conv1x1(inplanes, width)
        self.bn1 = norm_layer(width)
        self.conv2 = conv3x3(width, width, stride, groups, dilation)
        self.bn2 = norm_layer(width)
        self.conv3 = conv1x1(width, planes * self.expansion)
        self.bn3 = norm_layer(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)

        out += identity
        out = self.relu(out)

        return out

c,整个ResNet架构

对输入图像使用一个步长为2,卷积核为7×7的卷积层(紧接着BN,Relu),紧接着是4个残差学习块,不同深度的残差网络包含的每层残差学习块的个数不同(参考图1残差网络架构中的参数),最后是一个全局池化层和一个全连接层。

class ResNet(nn.Module):
 
    def __init__(self, block, layers, num_classes=1000):
        self.inplanes = 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.layer1 = self._make_layer(block, 64, layers[0])
        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.AvgPool2d(7, stride=1)
        self.fc = nn.Linear(512 * block.expansion, num_classes)

 

 

 

感谢解读

https://blog.csdn.net/chenyuping333/article/details/82344334

ResNet网络全部源码https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ResNet源码是用于实现残差网络(Residual Network)的PyTorch代码。ResNet是一种深度神经网络,其主要目的是解决深层网络训练中的梯度消失和精度下降等问题。 ResNet源码中的核心思想是引入残差模块,通过将输入信息与输出信息相加,从而保持信息的传递流畅性。该网络模型具有很深的层数,堆叠了大量的残差模块。这种结构使得网络更加易于训练,能够更好地捕捉图像特征。 PyTorch是一个开源的深度学习框架,ResNet源码使用PyTorch库来实现网络的构建、训练和测试等功能。通过PyTorch的动态图机制,我们可以方便地搭建和修改ResNet模型,以适应不同的任务和数据。 在ResNet源码中,我们可以看到各种网络层、激活函数、优化器等的定义和使用。同时,源码还提供了一些预训练的模型权重,这些权重可以加载到网络中,为我们的任务提供一个更好的起点。 通过仔细研究ResNet源码,我们可以了解到网络结构的细节,以及如何在PyTorch中构建和训练深度神经网络。此外,我们还可以根据源码进行修改和扩展,以满足特定的需求。 总之,ResNet源码PyTorch的一个重要示例,它展示了如何使用PyTorch构建和训练深度神经网络,在图像分类等任务中取得出色的效果。通过研究源码,我们可以更好地理解深度学习模型的实现原理,并为自己的研究和应用提供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值