🤵 Author :Horizon John

编程技巧篇:各种操作小结

🎇 机器视觉篇:会变魔术 OpenCV

💥 深度学习篇:简单入门 PyTorch

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

💻 算法篇:再忙也别忘了 LeetCode




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

  • 🚀 Residual Network
  • 🚀 ResNet 详解
  • 🎨 残差网络
  • 🎨 Residual Block
  • 🎨 ResNet50 详解
  • 🚀 ResNet 复现
  • 🚀 ResNet50 结构框图


🚀 Residual Network

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

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

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


resnet分类任务及回归任务代码_人工智能


 论文地址:Deep Residual Learning for Image Recognition



resnet分类任务及回归任务代码_ResNet_02



🚀 ResNet 详解

🎨 残差网络

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

特点:

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

resnet分类任务及回归任务代码_图像分类_03


residual block


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

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

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



🎨 Residual Block

resnet分类任务及回归任务代码_resnet分类任务及回归任务代码_04


浅层网络: 采用的是左侧的 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 为代表进行介绍:

resnet分类任务及回归任务代码_深度学习_05


它是由 4 个 大block 组成 ;

每个 大block 分别由 [3, 4, 6, 3]小block 组成 ;

每个 小block 都有 三个 卷积操作 ;

在网络开始前还有 一个 卷积操作 ;

层数:(3+4+6+3)X 3 + 1 = 50 layer

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

Conv Block 结构框图:


resnet分类任务及回归任务代码_图像分类_06

Identity Block 结构框图:


resnet分类任务及回归任务代码_人工智能_07

ResNet50 [3, 4, 6, 3]可以表示为:
conv2_xConv Block + Identity Block + Identity Blockconv3_xConv Block + Identity Block + Identity Block + Identity Blockconv4_xConv Block + Identity Block + Identity Block + Identity Block + Identity Block + Identity Blockconv5_xConv Block + Identity Block + Identity Block

ResNet50 结构框图:

resnet分类任务及回归任务代码_图像分类_08



🚀 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 结构框图

resnet分类任务及回归任务代码_深度学习_09