• 卷积相关-2


    这几天是每个都有第二弹啊 好了 说正题

    卷积是图像神经网络中的重要组成部分,它担起提取特征的重任,每当你编写一个网络结构的时候,它总会大喊"我来组成头部!",这么重要的头部自然值得我们好好地重视起来了"认真脸jpg",本篇文章将回顾那些年的一些经典卷积神经网络,并提炼要点且从以下几方面来进行阐述。

    1. 可供参考的资料、ImageNet 1000分类效果(采用224大小图片的效果,部分来自paperwithcode部分来自论文自身)。

    2. 网络的整体 or 核心结构图。

    3. 作者构建这些卷积网络的亮点。

    4. 具体的核心实现代码。

    神经网络架构

    (63.3% - 2012) AlexNet

    论文: ImageNet Classification with Deep Convolutional Neural Networks

    (https://proceedings.neurips.cc/paper/2012/file/c399862d3b9d6b76c8436e924a68c45b-Paper.pdf)

    Blog : AlexNet: The First CNN to win Image Net

    (https://www.kaggle.com/code/blurredmachine/alexnet-architecture-a-complete-guide/notebook)

    效果: ImageNet top-1 accuracy 63.3%

    结构图:

    成就

    • 第一个在ImageNet上跑起来的神经网络,在当年的竞赛中成绩大幅度领先第二名。

    创新

    • 2张GTX580 3G显存上训练百万级别的数据,在模型训练上做了一些工程的改进,现在单张A100显存能到80G,足以见当年的艰难。

    • 使用大卷积(11x11、5x5)和 全连接层,事实证明潮流是一个cycle,现在大卷积又开始流行起来了= =。

    • RELU:非线性激活单元,直到现在依然很流。

    • Dropout:防止过拟合,有模型ensemble的效果,后续应用广泛。

    • Local Response Normalization:一种正则化方法帮助模型更好的训练,后续基本没人用,大家可以阅读原文了解下。

    代码:

    1. class AlexNet(nn.Module):
    2.     def __init__(self, num_classes: int = 1000) -> None:
    3.         super(AlexNet, self).__init__()
    4.         self.features = nn.Sequential(
    5.             nn.Conv2d(364, kernel_size=11, stride=4, padding=2),
    6.             nn.ReLU(inplace=True),
    7.             nn.MaxPool2d(kernel_size=3, stride=2),
    8.             nn.Conv2d(64192, kernel_size=5, padding=2),
    9.             nn.ReLU(inplace=True),
    10.             nn.MaxPool2d(kernel_size=3, stride=2),
    11.             nn.Conv2d(192384, kernel_size=3, padding=1),
    12.             nn.ReLU(inplace=True),
    13.             nn.Conv2d(384256, kernel_size=3, padding=1),
    14.             nn.ReLU(inplace=True),
    15.             nn.Conv2d(256256, kernel_size=3, padding=1),
    16.             nn.ReLU(inplace=True),
    17.             nn.MaxPool2d(kernel_size=3, stride=2),
    18.         )
    19.         self.avgpool = nn.AdaptiveAvgPool2d((66))
    20.         self.classifier = nn.Sequential(
    21.             nn.Dropout(),
    22.             nn.Linear(256 * 6 * 64096),
    23.             nn.ReLU(inplace=True),
    24.             nn.Dropout(),
    25.             nn.Linear(40964096),
    26.             nn.ReLU(inplace=True),
    27.             nn.Linear(4096, num_classes),
    28.         )
    29.     def forward(self, x: torch.Tensor) -> torch.Tensor:
    30.         x = self.features(x)
    31.         x = self.avgpool(x)
    32.         x = torch.flatten(x, 1)
    33.         x = self.classifier(x)
    34.         return x

    (74.5% - 2014) VGG

    论文: Very Deep Convolutional Networks for Large-Scale Image Recognition

    (https://link.zhihu.com/?target=https%3A//arxiv.org/abs/1409.1556)

    Blog: 一文读懂VGG网络

    (https://zhuanlan.zhihu.com/p/41423739)

    效果: ImageNet top-1 accuracy 74.5%

    结构图:

    成就: ImageNet成绩大幅超过AlexNet,引领了未来网络朝着深度加深的方向进行。

    创新: 使用3X3卷积核代替11X11, 5X5,将网络的深度做进一步加深的同时引入更多的非线性层。

    代码:

    1. import torch.nn as nn
    2. cfg = {
    3.     "vgg11": [64"M"128"M"256256"M"512512"M"512512"M"],
    4.     "vgg13": [6464"M"128128"M"256256"M"512512"M"512512"M"],
    5.     "vgg16": [6464"M"128128"M"256256256"M"512512512"M"512512512"M"],
    6.     "vgg19": [6464"M"128128"M"256256256256"M"512512512512"M"512512512512"M"],
    7. }
    8. class VGG(nn.Module):
    9.     def __init__(self, vgg_name, num_outputs=10):
    10.         super().__init__()
    11.         self.features = self._make_layers(cfg[vgg_name])
    12.         self.classifier = nn.Linear(512, num_outputs)
    13.     def forward(self, x):
    14.         out = self.features(x)
    15.         out = out.view(out.size(0), -1)
    16.         out = self.classifier(out)
    17.         return out
    18.     def _make_layers(self, cfg):
    19.         layers = []
    20.         in_channels = 3
    21.         for x in cfg:
    22.             if x == "M":
    23.                 layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
    24.             else:
    25.                 layers += [
    26.                     nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
    27.                     nn.BatchNorm2d(x),
    28.                     nn.ReLU(inplace=True),
    29.                 ]
    30.                 in_channels = x
    31.         layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
    32.         return nn.Sequential(*layers)

    (80.0% - 2016) Inception

    论文:

    • Inception V1(https://arxiv.org/pdf/1409.4842v1.pdf)

    • Inception V2&3(https://arxiv.org/pdf/1512.00567v3.pdf)

    • Inception V4(https://arxiv.org/pdf/1602.07261.pdf)

    Blog : [A Simple Guide to the Versions of the Inception Network]

    (https://towardsdatascience.com/a-simple-guide-to-the-versions-of-the-inception-network-7fc52b863202)

    效果: ImageNet top-1 accuracy 80.00%

    结构图:

    创新:

    • 使用多尺度卷积核来提取信息,V1-V4基本就是在做这件事,无非是不断的优化性能。

    • 提出了Label Smoothing,这个东西比赛用的挺多的。

    (78.6% - 2015) ResNet

    论文:[Deep Residual Learning for Image Recognition]

    (https://arxiv.org/abs/1512.03385)

    Blog :

    • Resnet到底在解决一个什么问题呢?(https://www.zhihu.com/question/64494691/answer/786270699)

    • 你必须要知道CNN模型:ResNet(https://zhuanlan.zhihu.com/p/31852747)

    效果: ImageNet top-1 accuracy 78.2% or 82.4%([ResNet strikes back: An improved training procedure in timm]

    (https://paperswithcode.com/paper/resnet-strikes-back-an-improved-training))

    结构图:

    成就: 利用残差结构使得网络达到了前所未有的深度同时性能继续提升、同时使损失函数平面更加光滑(看过很多解释,这个个人觉得比较靠谱)

    创新: 残差网络

    代码:! key是关键代码、其实就一行~

    1. class ResNetBasicBlock(nn.Module):
    2.     expansion = 1
    3.     def __init__(selfin_planes, out_planes, stride=1):
    4.         super().__init__()
    5.         self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
    6.         self.bn1 = nn.BatchNorm2d(out_planes)
    7.         self.conv2 = nn.Conv2d(out_planes, out_planes, kernel_size=3, stride=1, padding=1, bias=False)
    8.         self.bn2 = nn.BatchNorm2d(out_planes)
    9.         self.shortcut = nn.Sequential()
    10.         # print(f"in_planes : {in_planes} | self.expansion * out_planes : {self.expansion * out_planes}")
    11.         if stride != 1 or in_planes != self.expansion * out_planes:
    12.             self.shortcut = nn.Sequential(
    13.                 nn.Conv2d(in_planes, self.expansion * out_planes, kernel_size=1, stride=stride, bias=False),
    14.                 nn.BatchNorm2d(self.expansion * out_planes),
    15.             )
    16.     def forward(self, x):
    17.         out = F.relu(self.bn1(self.conv1(x)))
    18.         # print(f"  conv 1: {out.shape}")
    19.         out = self.bn2(self.conv2(out))
    20.         # print(f"  conv 2: {out.shape}")
    21.         out += self.shortcut(x)  #! key
    22.         # print(f"shortcut: {out.shape}")
    23.         out = F.relu(out)  # 然后一起relu
    24.         # print("===" * 10)
    25.         return out

    (77.8% - 2016) DenseNet

    *论文: *Densely Connected Convolutional Networks

    (https://arxiv.org/abs/1608.06993)

    Blog :

    • CVPR 2017最佳论文作者解读:DenseNet 的“what”、“why”和“how”(https://www.leiphone.com/category/ai/0MNOwwfvWiAu43WO.html)

    • [pytorch源码解读]之DenseNet的源码解读(https://blog.csdn.net/u014453898/article/details/105670550)

    效果: ImageNet top-1 accuracy 77.8%

    结构图:

    创新: 利用DenseBlock进行新特征的探索和原始特征的多次重用

    代码:! key是关键代码、其实就一行~

    1. class Bottleneck(nn.Module):
    2.     def __init__(selfin_planes, growth_rate):
    3.         super(Bottleneck, self).__init__()
    4.         self.bn1 = nn.BatchNorm2d(in_planes)
    5.         self.conv1 = nn.Conv2d(in_planes, 4 * growth_rate, kernel_size=1, bias=False)
    6.         self.bn2 = nn.BatchNorm2d(4 * growth_rate)
    7.         self.conv2 = nn.Conv2d(4 * growth_rate, growth_rate, kernel_size=3, padding=1, bias=False)
    8.     def forward(self, x):
    9.         out = self.conv1(F.relu(self.bn1(x)))
    10.         out = self.conv2(F.relu(self.bn2(out)))
    11.         out = torch.cat([out, x], 1) #! key
    12.         return out

    (80.9% - 2016) ResNext

    论文: ResNext : Aggregated Residual Transformations for Deep Neural Networks

    (https://arxiv.org/abs/1611.05431)

    Blog :

    • ResNeXt详解(https://zhuanlan.zhihu.com/p/51075096)

    • ResNeXt的分类效果为什么比Resnet好?(https://www.zhihu.com/question/323424817/answer/1078704765)

    效果: ImageNet top-1 accuracy 80.9%

    结构图:

    创新: 提出Group的概念、利用Group增加特征的丰富度和多样性,类似multi-head attention。

    代码:! key是关键代码、其实就一行~

    1. import torch.nn as nn
    2. import torch.nn.functional as F
    3. class Block(nn.Module):
    4.     """Grouped convolution block."""
    5.     expansion = 2
    6.     def __init__(selfin_planes, cardinality=32, bottleneck_width=4, stride=1):
    7.         super(Blockself).__init__()
    8.         group_width = cardinality * bottleneck_width
    9.         self.conv1 = nn.Conv2d(in_planes, group_width, kernel_size=1, bias=False)
    10.         self.bn1 = nn.BatchNorm2d(group_width)
    11.         self.conv2 = nn.Conv2d(
    12.             group_width, group_width, kernel_size=3, stride=stride, padding=1, groups=cardinality, bias=False
    13.         )  #! key
    14.         self.bn2 = nn.BatchNorm2d(group_width)
    15.         self.conv3 = nn.Conv2d(group_width, self.expansion * group_width, kernel_size=1, bias=False)
    16.         self.bn3 = nn.BatchNorm2d(self.expansion * group_width)
    17.         self.shortcut = nn.Sequential()
    18.         if stride != 1 or in_planes != self.expansion * group_width:
    19.             self.shortcut = nn.Sequential(
    20.                 nn.Conv2d(in_planes, self.expansion * group_width, kernel_size=1, stride=stride, bias=False),
    21.                 nn.BatchNorm2d(self.expansion * group_width),
    22.             )
    23.     def forward(self, x):
    24.         out = F.relu(self.bn1(self.conv1(x)))
    25.         out = F.relu(self.bn2(self.conv2(out)))
    26.         out = self.bn3(self.conv3(out))
    27.         out += self.shortcut(x)
    28.         out = F.relu(out)
    29.         return out

    (81.2% - 2016) Res2Net

    论文: Res2Net: A New Multi-scale Backbone Architecture

    (https://arxiv.org/pdf/1904.01169.pdf)

    Blog: Res2Net:新型backbone网络,超越ResNet(https://zhuanlan.zhihu.com/p/86331579)

    效果: ImageNet top-1 accuracy 81.23%

    结构图

     

    亮点: 将多特征图的处理从layer并行的形势改为hierarchical

    代码: 因为修改了特征图的交互为hierarchical,所以代码有点多

    1. class Bottle2neck(nn.Module):
    2.     expansion = 4
    3.     def __init__(self, inplanes, planes, stride=1, downsample=None, baseWidth=26, scale=4, stype="normal"):
    4.         """Constructor
    5.         Args:
    6.             inplanes: input channel dimensionality
    7.             planes: output channel dimensionality
    8.             stride: conv stride. Replaces pooling layer.
    9.             downsample: None when stride = 1
    10.             baseWidth: basic width of conv3x3
    11.             scale: number of scale.
    12.             type: 'normal': normal set. 'stage': first block of a new stage.
    13.         """
    14.         super(Bottle2neck, self).__init__()
    15.         # todo baseWidth, width, scale的含义
    16.         width = int(math.floor(planes * (baseWidth / 64.0)))
    17.         print(f"width : {width}")
    18.         self.conv1 = nn.Conv2d(inplanes, width * scale, kernel_size=1, bias=False)
    19.         print(f"width * scale : {width * scale}")
    20.         self.bn1 = nn.BatchNorm2d(width * scale)
    21.         # nums的含义
    22.         if scale == 1:
    23.             self.nums = 1
    24.         else:
    25.             self.nums = scale - 1
    26.             
    27.         # todo stype的含义
    28.         if stype == "stage":
    29.             self.pool = nn.AvgPool2d(kernel_size=3, stride=stride, padding=1)
    30.         # 这里似乎是核心改进点
    31.         convs = []
    32.         bns = []
    33.         for i in range(self.nums):
    34.             convs.append(nn.Conv2d(width, width, kernel_size=3, stride=stride, padding=1, bias=False))
    35.             bns.append(nn.BatchNorm2d(width))
    36.         self.convs = nn.ModuleList(convs)
    37.         self.bns = nn.ModuleList(bns)
    38.         print(f"convs : {len(convs)}")
    39.         self.conv3 = nn.Conv2d(width * scale, planes * self.expansion, kernel_size=1, bias=False)
    40.         self.bn3 = nn.BatchNorm2d(planes * self.expansion)
    41.         self.relu = nn.ReLU(inplace=True)
    42.         self.downsample = downsample
    43.         self.stype = stype
    44.         self.scale = scale
    45.         self.width = width
    46.         print("============= init finish =============")
    47.     def forward(self, x):
    48.         residual = x
    49.         print(f"x : {x.shape}")
    50.         out = self.conv1(x)
    51.         out = self.bn1(out)
    52.         out = self.relu(out)
    53.         print(f"conv1 : {out.shape}")        
    54.         spx = torch.split(out, self.width, 1)
    55.         for i in spx:
    56.             print(i.shape)
    57.         print(f"len(spx) : {len(spx)}")
    58.         for i in range(self.nums):
    59.             if i == 0 or self.stype == "stage":
    60.                 sp = spx[i]
    61.             else:
    62.                 sp = sp + spx[i]
    63.             
    64.             print(f"sp : {sp.shape}")
    65.             sp = self.convs[i](sp)
    66.             sp = self.relu(self.bns[i](sp))
    67.             if i == 0:
    68.                 out = sp
    69.             else:
    70.                 out = torch.cat((out, sp), 1) # 相当于y2-y3-y4
    71.         if self.scale != 1 and self.stype == "normal":
    72.             out = torch.cat((out, spx[self.nums]), 1) # 相当于y1的部分
    73.         elif self.scale != 1 and self.stype == "stage":
    74.             out = torch.cat((out, self.pool(spx[self.nums])), 1)
    75.         out = self.conv3(out)
    76.         out = self.bn3(out)
    77.         if self.downsample is not None:
    78.             residual = self.downsample(x)
    79.         out += residual
    80.         out = self.relu(out)
    81.         return out

    (81.3% - 2017) SENet

    论文:[Squeeze-and-Excitation Networks]

    (https://arxiv.org/abs/1709.01507)

    Blog : 最后一届ImageNet冠军模型:SENet(https://zhuanlan.zhihu.com/p/65459972)

    效果: ImageNet top-1 accuracy 81.3%

    结构图:

    创新: 提出SELayer、利用可插拔的SELayer调节不同Channel的重要性,和Attention效果类似。

    代码:

    1. class SELayer(nn.Module):
    2.     def __init__(self, channel, reduction=16):
    3.         super(SELayer, self).__init__()
    4.         self.avg_pool = nn.AdaptiveAvgPool2d(1)
    5.         self.fc = nn.Sequential(
    6.             nn.Linear(channel, channel // reduction, bias=False),
    7.             nn.ReLU(inplace=True),
    8.             nn.Linear(channel // reduction, channel, bias=False),
    9.             nn.Sigmoid(),
    10.         )
    11.     def forward(self, x):
    12.         b, c, _, _ = x.size()
    13.         y = self.avg_pool(x).view(b, c)  # 将(b,c,1,1)转换为(b,c)
    14.         y = self.fc(y).view(b, c, 11)  # 将(b,c)转换为(b,c,1,1), 方便做attention
    15.         return x * y.expand_as(x)

    (80.1% - 2017) DPN

    论文: Dual Path Networks

    (https://arxiv.org/pdf/1707.01629.pdf)

    Blog : DPN详解(Dual Path Networks)(https://zhuanlan.zhihu.com/p/351198007)

    效果: ImageNet top-1 accuracy 80.07%

    结构图:

    创新:将resnet和densenet的思想做了结合。

    代码:! key是关键代码、其实就一行~

    1. class DPNBottleneck(nn.Module):
    2.     def __init__(selflast_planes, in_planes, out_planes, dense_depth, stride, first_layer):
    3.         super(DPNBottleneck, self).__init__()
    4.         self.out_planes = out_planes
    5.         self.dense_depth = dense_depth
    6.         self.conv1 = nn.Conv2d(last_planes, in_planes, kernel_size=1, bias=False)
    7.         self.bn1 = nn.BatchNorm2d(in_planes)
    8.         self.conv2 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=stride, padding=1, groups=32, bias=False)
    9.         self.bn2 = nn.BatchNorm2d(in_planes)
    10.         self.conv3 = nn.Conv2d(in_planes, out_planes + dense_depth, kernel_size=1, bias=False)
    11.         self.bn3 = nn.BatchNorm2d(out_planes + dense_depth)
    12.         self.shortcut = nn.Sequential()
    13.         if first_layer:
    14.             self.shortcut = nn.Sequential(
    15.                 nn.Conv2d(last_planes, out_planes + dense_depth, kernel_size=1, stride=stride, bias=False),
    16.                 nn.BatchNorm2d(out_planes + dense_depth),
    17.             )
    18.     def forward(self, x):
    19.         out = F.relu(self.bn1(self.conv1(x)))
    20.         out = F.relu(self.bn2(self.conv2(out)))
    21.         out = self.bn3(self.conv3(out))
    22.         x = self.shortcut(x)
    23.         d = self.out_planes
    24.         out = torch.cat(
    25.             [x[:, :d, :, :] + out[:, :d, :, :], x[:, d:, :, :], out[:, d:, :, :]], 1
    26.         )  #! key + is residual, cat is dense
    27.         out = F.relu(out)
    28.         return out

    (78.0% - 2019) DLA

    论文: Deep Layer Aggregation

    (https://larxiv.org/pdf/1707.06484.pdf)

    效果: ImageNet top-1 accuracy **78%**,来自论文,但是我觉得效果应该至少是resnext级别的,至少他在cifar10上的表现是最好的,可参考 https://github.com/kuangliu/pytorch-cifar 的效果列表                              whaosoft aiot http://143ai.com 

    结构图:

    创新: 采用IDA和HDA两种结构来进一步提炼conv的表达

    代码:

    1. """DLA in PyTorch.
    2. Reference:
    3.     Deep Layer Aggregation. https://arxiv.org/abs/1707.06484
    4. """
    5. import torch
    6. import torch.nn as nn
    7. import torch.nn.functional as F
    8. # dla相当于只有HDA + IDA
    9. class BasicBlock(nn.Module):
    10.     expansion = 1
    11.     def __init__(selfin_planes, planes, stride=1):
    12.         super(BasicBlock, self).__init__()
    13.         self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
    14.         self.bn1 = nn.BatchNorm2d(planes)
    15.         self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
    16.         self.bn2 = nn.BatchNorm2d(planes)
    17.         self.shortcut = nn.Sequential()
    18.         if stride != 1 or in_planes != self.expansion * planes:
    19.             self.shortcut = nn.Sequential(
    20.                 nn.Conv2d(in_planes, self.expansion * planes, kernel_size=1, stride=stride, bias=False),
    21.                 nn.BatchNorm2d(self.expansion * planes),
    22.             )
    23.     def forward(self, x):
    24.         out = F.relu(self.bn1(self.conv1(x)))
    25.         out = self.bn2(self.conv2(out))
    26.         out += self.shortcut(x)
    27.         out = F.relu(out)
    28.         return out
    29. class Root(nn.Module):
    30.     def __init__(selfin_channels, out_channels, kernel_size=1):
    31.         super(Root, self).__init__()
    32.         self.conv = nn.Conv2d(
    33.             in_channels, out_channels, kernel_size, stride=1, padding=(kernel_size - 1// 2, bias=False
    34.         )
    35.         self.bn = nn.BatchNorm2d(out_channels)
    36.     def forward(self, xs):
    37.         x = torch.cat(xs, 1)
    38.         out = F.relu(self.bn(self.conv(x)))
    39.         return out
    40. class Tree(nn.Module):
    41.     def __init__(selfblockin_channels, out_channels, level=1, stride=1):
    42.         super(Tree, self).__init__()
    43.         self.level = level
    44.         if level == 1:
    45.             self.root = Root(2 * out_channels, out_channels)
    46.             self.left_node = block(in_channels, out_channels, stride=stride)
    47.             self.right_node = block(out_channels, out_channels, stride=1)
    48.         else:
    49.             self.root = Root((level + 2* out_channels, out_channels)
    50.             for i in reversed(range(1, level)):
    51.                 subtree = Tree(blockin_channels, out_channels, level=i, stride=stride)
    52.                 self.__setattr__("level_%d" % i, subtree)
    53.             self.prev_root = block(in_channels, out_channels, stride=stride)
    54.             self.left_node = block(out_channels, out_channels, stride=1)
    55.             self.right_node = block(out_channels, out_channels, stride=1)
    56.     def forward(self, x):
    57.         xs = [self.prev_root(x)] if self.level > 1 else []
    58.         for i in reversed(range(1self.level)):
    59.             level_i = self.__getattr__("level_%d" % i)
    60.             x = level_i(x)
    61.             xs.append(x)
    62.         x = self.left_node(x)
    63.         xs.append(x)
    64.         x = self.right_node(x)
    65.         xs.append(x)
    66.         out = self.root(xs)
    67.         return out
    68. class DLA(nn.Module):
    69.     def __init__(selfblock=BasicBlock, num_classes=10):
    70.         super(DLA, self).__init__()
    71.         self.base = nn.Sequential(
    72.             nn.Conv2d(316, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(16), nn.ReLU(True)
    73.         )
    74.         self.layer1 = nn.Sequential(
    75.             nn.Conv2d(1616, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(16), nn.ReLU(True)
    76.         )
    77.         self.layer2 = nn.Sequential(
    78.             nn.Conv2d(1632, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(32), nn.ReLU(True)
    79.         )
    80.         self.layer3 = Tree(block3264, level=1, stride=1)
    81.         self.layer4 = Tree(block64128, level=2, stride=2)
    82.         self.layer5 = Tree(block128256, level=2, stride=2)
    83.         self.layer6 = Tree(block256512, level=1, stride=2)
    84.         self.linear = nn.Linear(512, num_classes)
    85.     def forward(self, x):
    86.         out = self.base(x)
    87.         out = self.layer1(out)
    88.         out = self.layer2(out)
    89.         out = self.layer3(out)
    90.         out = self.layer4(out)
    91.         out = self.layer5(out)
    92.         out = self.layer6(out)
    93.         out = F.avg_pool2d(out, 4)
    94.         out = out.view(out.size(0), -1)
    95.         out = self.linear(out)
    96.         return out
    97. def test():
    98.     net = DLA()
    99.     print(net)
    100.     x = torch.randn(133232)
    101.     y = net(x)
    102.     print(y.size())
    103. if __name__ == "__main__":
    104.     test()

    (86.8% - 2021) EfficientNet v1 v2

    论文:

    • EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks (https://arxiv.org/pdf/1905.11946v5.pdf)

    • EfficientNetV2: Smaller Models and Faster Training (https://arxiv.org/pdf/2104.00298v3.pdf)

    Blog :

    • 令人拍案叫绝的EfficientNet和EfficientDet (https://zhuanlan.zhihu.com/p/96773680)

    • 时隔两年,EfficientNet v2来了!更快,更小,更强!(https://zhuanlan.zhihu.com/p/361947957)

    效果: ImageNet top-1 accuracy 86.8%

    创新:是AutoDL在深度学习上一次非常成功的尝试

    • EfficientNet V1 uniformly scales all three dimensions(width, depth, resolution) with a fixed ratio。

    • EfficientNet V1 加入一些新block,扩大了搜索空间,并且不是equally scaling up every stage。

    代码:

    1. from torchvision.models import efficientnet_b0
    2. model = efficientnet_b0()

    其实这些年的CNN其实一直在尝试各种卷积的组合,从深度、宽度、注意力机制、各种Block组合形式上作文章,和传统机器学习的特征工程何其相似,只是需要更多的成本代价去尝试,大多都是经验性质的创新,原理上的不多,后面的VIT系列会有更多的一些创新,从整体设计上创造新的局面,但是也无法完全丢弃CNN,所以了解历史CNN的设计模式还是十分有必要的,本文理解有误的地方希望多多指正。

     

  • 相关阅读:
    【golang】1769. 移动所有球到每个盒子所需的最小操作数---时间复杂度O(N)
    iOS替换应用图标
    JS 中 对象 基础认识
    Nginx的跨域问题解决
    学习笔记|ADC|NTC原理|测温程序|STC32G单片机视频开发教程(冲哥)|第十九集:ADC应用之NTC
    EdgeCloudSim官方Sample运行——Windows+IntelliJ IDEA+Matlab
    B站批量取消关注
    保研笔记八——YOLOV5项目复习
    软件测试入门之接口测试
    ICML 2017: 基于卷积的Seq2Seq解决方案
  • 原文地址:https://blog.csdn.net/qq_29788741/article/details/127562365