[ 图像分类 ] 经典网络模型4——ResNet 详解与复现


🤵 AuthorHorizon John

编程技巧篇各种操作小结

🎇 机器视觉篇会变魔术 OpenCV

💥 深度学习篇简单入门 PyTorch

🏆 神经网络篇经典网络模型

💻 算法篇再忙也别忘了 LeetCode


🚀 Residual Network

Residual Network 简称 ResNet (残差网络),何凯明团队于2015年提出的一种网络;

在2015年 ImageNet 挑战赛(ILSVRC) classification 任务中获得了 冠军

目前在检测,分割,识别等领域里得到了广泛的应用;


🔗 论文地址:Deep Residual Learning for Image Recognition


ResNet


🚀 ResNet 详解

🎨 残差网络

对于一个网络,如果简单地增加深度,就会导致 梯度弥散梯度爆炸,我们采取的解决方法是 正则化
随着网络层数进一步增加,又会出现模型退化问题,在训练集上的 准确率出现饱和甚至下降 的现象 ;

特点:

通过利用内部的残差块实现跳跃连接,解决神经网络深度加深带来的 模型退化 问题:

residual block

residual block

传统网络 中采用的输入输出函数为:F(x)output1 = xinput

残差网络 中利用残差模块使输入输出函数为:F(x)output2 = F(x)output1 + xinput

xinput 直接跳过多层 加入到最后的输出 F(x)output2 单元当中,解决 F(x)output1 可能带来的 模型退化问题


🎨 Residual Block

residual block
浅层网络: 采用的是左侧的 residual block 结构(18-layer、34-layer)
深层网络: 采用的是右侧的 residual block 结构(50-layer、101-layer、152-layer)

很有意思的是,这两个设计具有参数量:
左侧: (3 X 3 X 64)X 64 +(3 X 3 X 64)X 64 = 73728
右侧: (1 X 1 X 64)X 64 +(3 X 3 X 64)X 64 +(1 X 1 X 64)X 256 +(1 X 1 X 64)X 256 = 73728

通过 1X1卷积 既能够改变通道数,又能大幅减少计算量参数量

可以对比 34-layer50-layer 发现它们的参数量分别为 3.6 X 1093.8 X 109


🎨 ResNet50 详解

以下以 ResNet50 为代表进行介绍:
ResNet50
它是由 4 个 大block 组成 ;
每个 大block 分别由 [3, 4, 6, 3]小block 组成 ;
每个 小block 都有 三个 卷积操作 ;
在网络开始前还有 一个 卷积操作 ;
层数:(3+4+6+3)X 3 + 1 = 50 layer

其中每个 大的 block 里面都是由两部分组成:Conv BlockIdentity Block
Conv Block :输入和输出维度不相同,不能串联,主要用于 改变网络维度
Identity Block :输入和输出维度相同,可以串联,主要用于 加深网络层数

Conv Block 结构框图:

Identity Block 结构框图:

ResNet50 [3, 4, 6, 3]可以表示为:
conv2_xConv Block + Identity Block + Identity Block
conv3_xConv Block + Identity Block + Identity Block + Identity Block
conv4_xConv Block + Identity Block + Identity Block + Identity Block + Identity Block + Identity Block
conv5_xConv Block + Identity Block + Identity Block

ResNet50 结构框图:

ResNet50


🚀 ResNet 复现

# Here is the code :

import torch
import torch.nn as nn
import torch.nn.functional as F
from torchinfo import summary


class BasicBlock(nn.Module):      # 左侧的 residual block 结构(18-layer、34-layer)
    expansion = 1

    def __init__(self, in_planes, planes, stride=1):      # 两层卷积 Conv2d + Shutcuts
        super(BasicBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3,
                               stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3,
                               stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)

        self.shortcut = nn.Sequential()
        if stride != 1 or in_planes != self.expansion*planes:      # Shutcuts用于构建 Conv Block 和 Identity Block
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_planes, self.expansion*planes,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*planes)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.bn2(self.conv2(out))
        out += self.shortcut(x)
        out = F.relu(out)
        return out


class Bottleneck(nn.Module):      # 右侧的 residual block 结构(50-layer、101-layer、152-layer)
    expansion = 4

    def __init__(self, in_planes, planes, stride=1):      # 三层卷积 Conv2d + Shutcuts
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3,
                               stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(planes, self.expansion*planes,
                               kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(self.expansion*planes)

        self.shortcut = nn.Sequential()
        if stride != 1 or in_planes != self.expansion*planes:      # Shutcuts用于构建 Conv Block 和 Identity Block
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_planes, self.expansion*planes,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*planes)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = F.relu(self.bn2(self.conv2(out)))
        out = self.bn3(self.conv3(out))
        out += self.shortcut(x)
        out = F.relu(out)
        return out


class ResNet(nn.Module):
    def __init__(self, block, num_blocks, num_classes=1000):
        super(ResNet, self).__init__()
        self.in_planes = 64

        self.conv1 = nn.Conv2d(3, 64, kernel_size=3,
                               stride=1, padding=1, bias=False)                  # conv1
        self.bn1 = nn.BatchNorm2d(64)
        self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)       # conv2_x
        self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)      # conv3_x
        self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)      # conv4_x
        self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)      # conv5_x
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.linear = nn.Linear(512 * block.expansion, num_classes)

    def _make_layer(self, block, planes, num_blocks, stride):
        strides = [stride] + [1]*(num_blocks-1)
        layers = []
        for stride in strides:
            layers.append(block(self.in_planes, planes, stride))
            self.in_planes = planes * block.expansion
        return nn.Sequential(*layers)

    def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)))
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        out = self.linear(x)
        return out


def ResNet18():
    return ResNet(BasicBlock, [2, 2, 2, 2])


def ResNet34():
    return ResNet(BasicBlock, [3, 4, 6, 3])


def ResNet50():
    return ResNet(Bottleneck, [3, 4, 6, 3])


def ResNet101():
    return ResNet(Bottleneck, [3, 4, 23, 3])


def ResNet152():
    return ResNet(Bottleneck, [3, 8, 36, 3])


def test():
    net = ResNet50()
    y = net(torch.randn(1, 3, 224, 224))
    print(y.size())
    summary(net, (1, 3, 224, 224))


if __name__ == '__main__':
    test()

输出结果:

torch.Size([1, 1000])
==========================================================================================
Layer (type:depth-idx)                   Output Shape              Param #
==========================================================================================
ResNet                                   --                        --
├─Conv2d: 1-1                            [1, 64, 224, 224]         1,728
├─BatchNorm2d: 1-2                       [1, 64, 224, 224]         128
├─Sequential: 1-3                        [1, 256, 224, 224]        --
│    └─Bottleneck: 2-1                   [1, 256, 224, 224]        --
│    │    └─Conv2d: 3-1                  [1, 64, 224, 224]         4,096
│    │    └─BatchNorm2d: 3-2             [1, 64, 224, 224]         128
│    │    └─Conv2d: 3-3                  [1, 64, 224, 224]         36,864
│    │    └─BatchNorm2d: 3-4             [1, 64, 224, 224]         128
│    │    └─Conv2d: 3-5                  [1, 256, 224, 224]        16,384
│    │    └─BatchNorm2d: 3-6             [1, 256, 224, 224]        512
│    │    └─Sequential: 3-7              [1, 256, 224, 224]        16,896
│    └─Bottleneck: 2-2                   [1, 256, 224, 224]        --
│    │    └─Conv2d: 3-8                  [1, 64, 224, 224]         16,384
│    │    └─BatchNorm2d: 3-9             [1, 64, 224, 224]         128
│    │    └─Conv2d: 3-10                 [1, 64, 224, 224]         36,864
│    │    └─BatchNorm2d: 3-11            [1, 64, 224, 224]         128
│    │    └─Conv2d: 3-12                 [1, 256, 224, 224]        16,384
│    │    └─BatchNorm2d: 3-13            [1, 256, 224, 224]        512
│    │    └─Sequential: 3-14             [1, 256, 224, 224]        --
│    └─Bottleneck: 2-3                   [1, 256, 224, 224]        --
│    │    └─Conv2d: 3-15                 [1, 64, 224, 224]         16,384
│    │    └─BatchNorm2d: 3-16            [1, 64, 224, 224]         128
│    │    └─Conv2d: 3-17                 [1, 64, 224, 224]         36,864
│    │    └─BatchNorm2d: 3-18            [1, 64, 224, 224]         128
│    │    └─Conv2d: 3-19                 [1, 256, 224, 224]        16,384
│    │    └─BatchNorm2d: 3-20            [1, 256, 224, 224]        512
│    │    └─Sequential: 3-21             [1, 256, 224, 224]        --
├─Sequential: 1-4                        [1, 512, 112, 112]        --
│    └─Bottleneck: 2-4                   [1, 512, 112, 112]        --
│    │    └─Conv2d: 3-22                 [1, 128, 224, 224]        32,768
│    │    └─BatchNorm2d: 3-23            [1, 128, 224, 224]        256
│    │    └─Conv2d: 3-24                 [1, 128, 112, 112]        147,456
│    │    └─BatchNorm2d: 3-25            [1, 128, 112, 112]        256
│    │    └─Conv2d: 3-26                 [1, 512, 112, 112]        65,536
│    │    └─BatchNorm2d: 3-27            [1, 512, 112, 112]        1,024
│    │    └─Sequential: 3-28             [1, 512, 112, 112]        132,096
│    └─Bottleneck: 2-5                   [1, 512, 112, 112]        --
│    │    └─Conv2d: 3-29                 [1, 128, 112, 112]        65,536
│    │    └─BatchNorm2d: 3-30            [1, 128, 112, 112]        256
│    │    └─Conv2d: 3-31                 [1, 128, 112, 112]        147,456
│    │    └─BatchNorm2d: 3-32            [1, 128, 112, 112]        256
│    │    └─Conv2d: 3-33                 [1, 512, 112, 112]        65,536
│    │    └─BatchNorm2d: 3-34            [1, 512, 112, 112]        1,024
│    │    └─Sequential: 3-35             [1, 512, 112, 112]        --
│    └─Bottleneck: 2-6                   [1, 512, 112, 112]        --
│    │    └─Conv2d: 3-36                 [1, 128, 112, 112]        65,536
│    │    └─BatchNorm2d: 3-37            [1, 128, 112, 112]        256
│    │    └─Conv2d: 3-38                 [1, 128, 112, 112]        147,456
│    │    └─BatchNorm2d: 3-39            [1, 128, 112, 112]        256
│    │    └─Conv2d: 3-40                 [1, 512, 112, 112]        65,536
│    │    └─BatchNorm2d: 3-41            [1, 512, 112, 112]        1,024
│    │    └─Sequential: 3-42             [1, 512, 112, 112]        --
│    └─Bottleneck: 2-7                   [1, 512, 112, 112]        --
│    │    └─Conv2d: 3-43                 [1, 128, 112, 112]        65,536
│    │    └─BatchNorm2d: 3-44            [1, 128, 112, 112]        256
│    │    └─Conv2d: 3-45                 [1, 128, 112, 112]        147,456
│    │    └─BatchNorm2d: 3-46            [1, 128, 112, 112]        256
│    │    └─Conv2d: 3-47                 [1, 512, 112, 112]        65,536
│    │    └─BatchNorm2d: 3-48            [1, 512, 112, 112]        1,024
│    │    └─Sequential: 3-49             [1, 512, 112, 112]        --
├─Sequential: 1-5                        [1, 1024, 56, 56]         --
│    └─Bottleneck: 2-8                   [1, 1024, 56, 56]         --
│    │    └─Conv2d: 3-50                 [1, 256, 112, 112]        131,072
│    │    └─BatchNorm2d: 3-51            [1, 256, 112, 112]        512
│    │    └─Conv2d: 3-52                 [1, 256, 56, 56]          589,824
│    │    └─BatchNorm2d: 3-53            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-54                 [1, 1024, 56, 56]         262,144
│    │    └─BatchNorm2d: 3-55            [1, 1024, 56, 56]         2,048
│    │    └─Sequential: 3-56             [1, 1024, 56, 56]         526,336
│    └─Bottleneck: 2-9                   [1, 1024, 56, 56]         --
│    │    └─Conv2d: 3-57                 [1, 256, 56, 56]          262,144
│    │    └─BatchNorm2d: 3-58            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-59                 [1, 256, 56, 56]          589,824
│    │    └─BatchNorm2d: 3-60            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-61                 [1, 1024, 56, 56]         262,144
│    │    └─BatchNorm2d: 3-62            [1, 1024, 56, 56]         2,048
│    │    └─Sequential: 3-63             [1, 1024, 56, 56]         --
│    └─Bottleneck: 2-10                  [1, 1024, 56, 56]         --
│    │    └─Conv2d: 3-64                 [1, 256, 56, 56]          262,144
│    │    └─BatchNorm2d: 3-65            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-66                 [1, 256, 56, 56]          589,824
│    │    └─BatchNorm2d: 3-67            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-68                 [1, 1024, 56, 56]         262,144
│    │    └─BatchNorm2d: 3-69            [1, 1024, 56, 56]         2,048
│    │    └─Sequential: 3-70             [1, 1024, 56, 56]         --
│    └─Bottleneck: 2-11                  [1, 1024, 56, 56]         --
│    │    └─Conv2d: 3-71                 [1, 256, 56, 56]          262,144
│    │    └─BatchNorm2d: 3-72            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-73                 [1, 256, 56, 56]          589,824
│    │    └─BatchNorm2d: 3-74            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-75                 [1, 1024, 56, 56]         262,144
│    │    └─BatchNorm2d: 3-76            [1, 1024, 56, 56]         2,048
│    │    └─Sequential: 3-77             [1, 1024, 56, 56]         --
│    └─Bottleneck: 2-12                  [1, 1024, 56, 56]         --
│    │    └─Conv2d: 3-78                 [1, 256, 56, 56]          262,144
│    │    └─BatchNorm2d: 3-79            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-80                 [1, 256, 56, 56]          589,824
│    │    └─BatchNorm2d: 3-81            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-82                 [1, 1024, 56, 56]         262,144
│    │    └─BatchNorm2d: 3-83            [1, 1024, 56, 56]         2,048
│    │    └─Sequential: 3-84             [1, 1024, 56, 56]         --
│    └─Bottleneck: 2-13                  [1, 1024, 56, 56]         --
│    │    └─Conv2d: 3-85                 [1, 256, 56, 56]          262,144
│    │    └─BatchNorm2d: 3-86            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-87                 [1, 256, 56, 56]          589,824
│    │    └─BatchNorm2d: 3-88            [1, 256, 56, 56]          512
│    │    └─Conv2d: 3-89                 [1, 1024, 56, 56]         262,144
│    │    └─BatchNorm2d: 3-90            [1, 1024, 56, 56]         2,048
│    │    └─Sequential: 3-91             [1, 1024, 56, 56]         --
├─Sequential: 1-6                        [1, 2048, 28, 28]         --
│    └─Bottleneck: 2-14                  [1, 2048, 28, 28]         --
│    │    └─Conv2d: 3-92                 [1, 512, 56, 56]          524,288
│    │    └─BatchNorm2d: 3-93            [1, 512, 56, 56]          1,024
│    │    └─Conv2d: 3-94                 [1, 512, 28, 28]          2,359,296
│    │    └─BatchNorm2d: 3-95            [1, 512, 28, 28]          1,024
│    │    └─Conv2d: 3-96                 [1, 2048, 28, 28]         1,048,576
│    │    └─BatchNorm2d: 3-97            [1, 2048, 28, 28]         4,096
│    │    └─Sequential: 3-98             [1, 2048, 28, 28]         2,101,248
│    └─Bottleneck: 2-15                  [1, 2048, 28, 28]         --
│    │    └─Conv2d: 3-99                 [1, 512, 28, 28]          1,048,576
│    │    └─BatchNorm2d: 3-100           [1, 512, 28, 28]          1,024
│    │    └─Conv2d: 3-101                [1, 512, 28, 28]          2,359,296
│    │    └─BatchNorm2d: 3-102           [1, 512, 28, 28]          1,024
│    │    └─Conv2d: 3-103                [1, 2048, 28, 28]         1,048,576
│    │    └─BatchNorm2d: 3-104           [1, 2048, 28, 28]         4,096
│    │    └─Sequential: 3-105            [1, 2048, 28, 28]         --
│    └─Bottleneck: 2-16                  [1, 2048, 28, 28]         --
│    │    └─Conv2d: 3-106                [1, 512, 28, 28]          1,048,576
│    │    └─BatchNorm2d: 3-107           [1, 512, 28, 28]          1,024
│    │    └─Conv2d: 3-108                [1, 512, 28, 28]          2,359,296
│    │    └─BatchNorm2d: 3-109           [1, 512, 28, 28]          1,024
│    │    └─Conv2d: 3-110                [1, 2048, 28, 28]         1,048,576
│    │    └─BatchNorm2d: 3-111           [1, 2048, 28, 28]         4,096
│    │    └─Sequential: 3-112            [1, 2048, 28, 28]         --
├─AdaptiveAvgPool2d: 1-7                 [1, 2048, 1, 1]           --
├─Linear: 1-8                            [1, 1000]                 2,049,000
==========================================================================================
Total params: 25,549,352
Trainable params: 25,549,352
Non-trainable params: 0
Total mult-adds (G): 63.59
==========================================================================================
Input size (MB): 0.60
Forward/backward pass size (MB): 2691.05
Params size (MB): 102.20
Estimated Total Size (MB): 2793.85
==========================================================================================

🚀 ResNet50 结构框图

ResNet50



  • 27
    点赞
  • 132
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
### 回答1: dualresnet结构图是一个神经网络模型的结构示意图,主要由两个ResNet模块相互交替组成。这个模型的设计旨在解决深度神经网络训练时的梯度消失和梯度爆炸等问题,以提高模型的训练效果和精度。具体结构图可以通过搜索引擎或相关学术论文进行查找。 ### 回答2: Dual ResNet结构图是用于深度学习中的一个网络架构。它的主要目的是通过融合不同层级的特征信息来提高模型的性能。该结构图包括两个ResNet模型,分别为源模型和目标模型。 源模型接收输入数据并通过一系列卷积和池化等操作学习特征。该模型中的残差连接用于解决梯度消失问题,通过直接将前一层的特征进行无损复制和直接相加操作,使得网络能够更好地学习输入数据的高阶特征。同时,该模型还引入了批量归一化和修正线性单元(ReLU)等激活函数来提高网络的非线性表示能力。 目标模型的结构与源模型类似,但使用不同的参数进行训练。这可以通过使用不同的数据集或者采用迁移学习等方式来实现。目标模型的目的是学习到源模型没有学到的特征,从而提高模型的泛化能力。 使用Dual ResNet结构时,源模型和目标模型可以共享部分层。这种共享有助于模型的参数有效利用,并减少训练过程中的计算量,从而提高模型的训练效率。此外,源模型和目标模型可以通过反向传播算法来进行训练,并使用梯度下降优化方法来最小化预测误差。 总之,Dual ResNet结构图是一种利用两个ResNet模型进行特征融合的网络架构,通过学习源模型和目标模型的不同特征信息来提高模型的性能。它可以应用于各种深度学习任务,如图像分类、目标检测和语义分割等。 ### 回答3: Dual ResNet是一种深度学习网络结构,主要用于图像识别和分类任务。其结构图如下所示: Dual ResNet由两个ResNet模块组成,分别为主干网络和辅助网络。主干网络通常是一个更深的ResNet模型,用于提取图像的高级特征。而辅助网络是一个较浅的ResNet模型,用于提取图像的低级特征。 在Dual ResNet中,图像首先经过主干网络的多个卷积层和残差块,用于提取图像的复杂特征。这些特征具有较高的抽象能力,可用于更精确的图像分类。 接下来,主干网络的输出特征图被分为两个分支,分别连接到辅助网络的输入。辅助网络由一系列较浅的卷积层和残差块组成,用于提取图像的低级特征。 最后,主干网络和辅助网络的输出特征图被融合在一起,并经过全局平均池化层和全连接层,得到最终的分类结果。 Dual ResNet的结构图展示了其特殊的网络组成方式,充分利用了主干网络和辅助网络的特点。主干网络可以提取更复杂的特征,而辅助网络可以提取更细节的特征。通过融合两者的输出,Dual ResNet能够同时利用高级和低级特征,提高图像分类的准确性。 总结起来,Dual ResNet结构图展示了主干网络和辅助网络的连接方式,通过融合不同级别的特征,提高图像分类任务的性能。这种结构图的设计充分考虑到了特征的多样性和层次性,使得Dual ResNet在图像识别领域取得了非常好的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Horizon John

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值