【图像分类】用通俗易懂代码的复现EfficientNetV2,入门的绝佳选择(pytorch)

举报
AI浩 发表于 2021/12/23 00:00:46 2021/12/23
3.9k+ 0 0
【摘要】 目录 摘要 代码实现 激活函数 SE模块  定义MBConv模块和Fused-MBConv模块 主体模块 完整代码 摘要 上周学习了EfficientNetV2的论文,并对其进行了翻译,如果对论文感兴趣的可以参考我的文章:https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/...

目录

摘要

代码实现

激活函数

SE模块

 定义MBConv模块和Fused-MBConv模块

主体模块

完整代码


摘要

上周学习了EfficientNetV2的论文,并对其进行了翻译,如果对论文感兴趣的可以参考我的文章:https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/117399085

在EfficientNets的第一个版本中存在三个缺点:

(1) 用非常大的图像尺寸训练很慢;而且输入较大的图像必须以较小的批大小训练这些模型,这大大减慢了训练速度,但是精度反而下降了。

(2) 在网络浅层中使用Depthwise convolutions速度会很慢。

(3) 每个阶段都按比例放大是次优的。 

EfficientNetV2针对这三个方面的缺点做了改进:

1、针对图像的大小的问题,作者提出了自适应正则化的渐进式学习的方法,详见论文的4.2节

2、针对Depthwise convolutions在早期层中很慢的问题,作者提出了 Fused-MBConv 模块来代替部分的MBConv。

3、针对每个阶段都按比例放大是次优的问题,作者使用非均匀缩放策略来逐步添加 到后期阶段。

作者对EfficientNetV2做了三方面的总结

          •我们引入了 EfficientNetV2,这是一个新的更小、更快的模型系列。 通过我们的训练感知NAS 和扩展发现,EfficientNetV2 在训练速度和参数效率方面都优于以前的模型。

          • 我们提出了一种改进的渐进式学习方法,它可以根据图像大小自适应地调整正则化。 我们表明它可以加快训练速度,同时提高准确性。

          • 我们在 ImageNet、CIFAR、Cars 和 Flowers 数据集上展示了比现有技术快 11 倍的训练速度和 6.8 倍的参数效率。

总之,一句话,我们的新模型又快又准而且还小,大家赶快用吧!下面我就讲讲如何使用Pytorch实现EfficientNetV2。

代码实现

EfficientNetV2和EfficientNet一样也是一个家族模型,包括:efficientnetv2_s、efficientnetv2_m,、efficientnetv2_l、efficientnetv2_xl。所以我们要实现四个模型。

激活函数

激活函数使用SiLU激活函数,我对激活函数做了总结,感兴趣的可以查看:CNN基础——激活函数_AI浩-CSDN博客


      # SiLU (Swish) activation function
      if hasattr(nn, 'SiLU'):
          SiLU = nn.SiLU
      else:
         # For compatibility with old PyTorch versions
         class SiLU(nn.Module):
             def forward(self, x):
                 return x * torch.sigmoid(x)
  
 

SE模块

SE模块,我在前面的文章中已经介绍了。现在直接将SE模块拿过来使用。


      class SELayer(nn.Module):
         def __init__(self, inp, oup, reduction=4):
             super(SELayer, self).__init__()
              self.avg_pool = nn.AdaptiveAvgPool2d(1)
              self.fc = nn.Sequential(
                      nn.Linear(oup, _make_divisible(inp // reduction, 8)),
                      SiLU(),
                      nn.Linear(_make_divisible(inp // reduction, 8), oup),
                      nn.Sigmoid()
              )
         def forward(self, x):
              b, c, _, _ = x.size()
              y = self.avg_pool(x).view(b, c)
              y = self.fc(y).view(b, c, 1, 1)
             return x * y
  
 

 定义MBConv模块和Fused-MBConv模块

这两个模块是整个模型实现的核心,模块的详细构造如下图:

我们可以看到MBConv模块,经过1×1的卷积,然后channel放大四倍,再经过depthwise conv3×3的卷积,然后经过SE模块后,再经过1×1的卷积,把channel恢复到输入的大小,最后和上层的输入融合。

Fused-MBConv模块比MBConv模块简单些,先经过3×3的卷积,把channel放大四倍,然后经过SE模块,再经过1×1的卷积,最后和上层的输入融合。下面是实现MBConv模块和Fused-MBConv模块的详细代码:


      class MBConv(nn.Module):
         """
       定义MBConv模块和Fused-MBConv模块,将fused设置为1或True是Fused-MBConv,否则是MBConv
       :param inp:输入的channel
       :param oup:输出的channel
       :param stride:步长,设置为1时图片的大小不变,设置为2时,图片的面积变为原来的四分之一
       :param expand_ratio:放大的倍率
       :return:
       """
         def __init__(self, inp, oup, stride, expand_ratio, fused):
             super(MBConv, self).__init__()
             assert stride in [1, 2]
              hidden_dim = round(inp * expand_ratio)
              self.identity = stride == 1 and inp == oup
             if fused:
                  self.conv = nn.Sequential(
                     # fused
                      nn.Conv2d(inp, hidden_dim, 3, stride, 1, bias=False),
                      nn.BatchNorm2d(hidden_dim),
                      SiLU(),
                      SELayer(inp, hidden_dim),
                     # pw-linear
                      nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
                      nn.BatchNorm2d(oup),
                  )
             else:
                  self.conv = nn.Sequential(
                     # pw
                      nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
                      nn.BatchNorm2d(hidden_dim),
                      SiLU(),
                     # dw
                      nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False),
                      nn.BatchNorm2d(hidden_dim),
                      SiLU(),
                      SELayer(inp, hidden_dim),
                     # pw-linear
                      nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
                      nn.BatchNorm2d(oup),
                  )
         def forward(self, x):
             if self.identity:
                 return x + self.conv(x)
             else:
                 return self.conv(x)
  
 

主体模块


      class EfficientNetv2(nn.Module):
         def __init__(self, cfgs, num_classes=1000, width_mult=1.):
             super(EfficientNetv2, self).__init__()
              self.cfgs = cfgs
             # building first layer
              input_channel = _make_divisible(24 * width_mult, 8)
              layers = [conv_3x3_bn(3, input_channel, 2)]
             # building inverted residual blocks
              block = MBConv
             for t, c, n, s, fused in self.cfgs:
                  output_channel = _make_divisible(c * width_mult, 8)
                 for i in range(n):
                      layers.append(block(input_channel, output_channel, s if i == 0 else 1, t, fused))
                      input_channel = output_channel
              self.features = nn.Sequential(*layers)
             # building last several layers
              output_channel = _make_divisible(1792 * width_mult, 8) if width_mult > 1.0 else 1792
              self.conv = conv_1x1_bn(input_channel, output_channel)
              self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
              self.classifier = nn.Linear(output_channel, num_classes)
              self._initialize_weights()
         def forward(self, x):
              x = self.features(x)
              x = self.conv(x)
              x = self.avgpool(x)
              x = x.view(x.size(0), -1)
              x = self.classifier(x)
             return x
         def _initialize_weights(self):
             for m in self.modules():
                 if isinstance(m, nn.Conv2d):
                      n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                      m.weight.data.normal_(0, math.sqrt(2. / n))
                     if m.bias is not None:
                          m.bias.data.zero_()
                 elif isinstance(m, nn.BatchNorm2d):
                      m.weight.data.fill_(1)
                      m.bias.data.zero_()
                 elif isinstance(m, nn.Linear):
                      m.weight.data.normal_(0, 0.001)
                      m.bias.data.zero_()
  
 

 理解这段代码,我们还需要了解输入参数cfgs,以efficientnetv2_s为例:


      def efficientnetv2_s(**kwargs):
         """
       Constructs a EfficientNetV2-S model
       """
          cfgs = [
             # t, c, n, s, fused
              [1,  24,  2, 1, 1],
              [4,  48,  4, 2, 1],
              [4,  64,  4, 2, 1],
              [4, 128,  6, 2, 0],
              [6, 160,  9, 1, 0],
              [6, 272, 15, 2, 0],
          ]
         return EfficientNetv2(cfgs, **kwargs)
  
 

 第一列“t”指的是MBConv模块和Fused-MBConv模块第一个输入后放大的倍率。

 第二列“c”,channel,指的是输出的channel。

第三列“n”,指定的是MBConv模块和Fused-MBConv模块堆叠的个数。

第四列“s”,指的是卷积的步长,步长为1,图片的大小不变,步长为图片的面积缩小为原来的四分之一,实现降维。

第五列“fused”,选择MBConv模块或Fused-MBConv模块,为1这是Fused-MBConv模块,0则是MBConv模块,对应了前面摘要提过了,在浅层用Fused-MBConv代替MBConv。

完整代码


      import torch
      import torch.nn as nn
      import math
      __all__ = ['efficientnetv2_s', 'efficientnetv2_m', 'efficientnetv2_l', 'efficientnetv2_xl']
      from torchsummary import summary
      #这个函数的目的是确保Channel能被8整除。
      def _make_divisible(v, divisor, min_value=None):
         """
       这个函数的目的是确保Channel能被8整除。
       :param v:
       :param divisor:
       :param min_value:
       :return:
       """
         if min_value is None:
              min_value = divisor
          new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
         # Make sure that round down does not go down by more than 10%.
         if new_v < 0.9 * v:
              new_v += divisor
         return new_v
      # SiLU (Swish) activation function
      if hasattr(nn, 'SiLU'):
          SiLU = nn.SiLU
      else:
         # For compatibility with old PyTorch versions
         class SiLU(nn.Module):
             def forward(self, x):
                 return x * torch.sigmoid(x)
      class SELayer(nn.Module):
         def __init__(self, inp, oup, reduction=4):
             super(SELayer, self).__init__()
              self.avg_pool = nn.AdaptiveAvgPool2d(1)
              self.fc = nn.Sequential(
                      nn.Linear(oup, _make_divisible(inp // reduction, 8)),
                      SiLU(),
                      nn.Linear(_make_divisible(inp // reduction, 8), oup),
                      nn.Sigmoid()
              )
         def forward(self, x):
              b, c, _, _ = x.size()
              y = self.avg_pool(x).view(b, c)
              y = self.fc(y).view(b, c, 1, 1)
             return x * y
      def conv_3x3_bn(inp, oup, stride):
         return nn.Sequential(
              nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
              nn.BatchNorm2d(oup),
              SiLU()
          )
      def conv_1x1_bn(inp, oup):
         return nn.Sequential(
              nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
              nn.BatchNorm2d(oup),
              SiLU()
          )
      class MBConv(nn.Module):
         """
       定义MBConv模块和Fused-MBConv模块,将fused设置为1或True是Fused-MBConv,否则是MBConv
       :param inp:输入的channel
       :param oup:输出的channel
       :param stride:步长,设置为1时图片的大小不变,设置为2时,图片的面积变为原来的四分之一
       :param expand_ratio:放大的倍率
       :return:
       """
         def __init__(self, inp, oup, stride, expand_ratio, fused):
             super(MBConv, self).__init__()
             assert stride in [1, 2]
              hidden_dim = round(inp * expand_ratio)
              self.identity = stride == 1 and inp == oup
             if fused:
                  self.conv = nn.Sequential(
                     # fused
                      nn.Conv2d(inp, hidden_dim, 3, stride, 1, bias=False),
                      nn.BatchNorm2d(hidden_dim),
                      SiLU(),
                      SELayer(inp, hidden_dim),
                     # pw-linear
                      nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
                      nn.BatchNorm2d(oup),
                  )
             else:
                  self.conv = nn.Sequential(
                     # pw
                      nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
                      nn.BatchNorm2d(hidden_dim),
                      SiLU(),
                     # dw
                      nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False),
                      nn.BatchNorm2d(hidden_dim),
                      SiLU(),
                      SELayer(inp, hidden_dim),
                     # pw-linear
                      nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
                      nn.BatchNorm2d(oup),
                  )
         def forward(self, x):
             if self.identity:
                 return x + self.conv(x)
             else:
                 return self.conv(x)
      class EfficientNetv2(nn.Module):
         def __init__(self, cfgs, num_classes=1000, width_mult=1.):
             super(EfficientNetv2, self).__init__()
              self.cfgs = cfgs
             # building first layer
              input_channel = _make_divisible(24 * width_mult, 8)
              layers = [conv_3x3_bn(3, input_channel, 2)]
             # building inverted residual blocks
              block = MBConv
             for t, c, n, s, fused in self.cfgs:
                  output_channel = _make_divisible(c * width_mult, 8)
                 for i in range(n):
                      layers.append(block(input_channel, output_channel, s if i == 0 else 1, t, fused))
                      input_channel = output_channel
              self.features = nn.Sequential(*layers)
             # building last several layers
              output_channel = _make_divisible(1792 * width_mult, 8) if width_mult > 1.0 else 1792
              self.conv = conv_1x1_bn(input_channel, output_channel)
              self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
              self.classifier = nn.Linear(output_channel, num_classes)
              self._initialize_weights()
         def forward(self, x):
              x = self.features(x)
              x = self.conv(x)
              x = self.avgpool(x)
              x = x.view(x.size(0), -1)
              x = self.classifier(x)
             return x
         def _initialize_weights(self):
             for m in self.modules():
                 if isinstance(m, nn.Conv2d):
                      n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                      m.weight.data.normal_(0, math.sqrt(2. / n))
                     if m.bias is not None:
                          m.bias.data.zero_()
                 elif isinstance(m, nn.BatchNorm2d):
                      m.weight.data.fill_(1)
                      m.bias.data.zero_()
                 elif isinstance(m, nn.Linear):
                      m.weight.data.normal_(0, 0.001)
                      m.bias.data.zero_()
      def efficientnetv2_s(**kwargs):
         """
       Constructs a EfficientNetV2-S model
       """
          cfgs = [
             # t, c, n, s, fused
              [1,  24,  2, 1, 1],
              [4,  48,  4, 2, 1],
              [4,  64,  4, 2, 1],
              [4, 128,  6, 2, 0],
              [6, 160,  9, 1, 0],
              [6, 272, 15, 2, 0],
          ]
         return EfficientNetv2(cfgs, **kwargs)
      def efficientnetv2_m(**kwargs):
         """
       Constructs a EfficientNetV2-M model
       """
          cfgs = [
             # t, c, n, s, fused
              [1,  24,  3, 1, 1],
              [4,  48,  5, 2, 1],
              [4,  80,  5, 2, 1],
              [4, 160,  7, 2, 0],
              [6, 176, 14, 1, 0],
              [6, 304, 18, 2, 0],
              [6, 512,  5, 1, 0],
          ]
         return EfficientNetv2(cfgs, **kwargs)
      def efficientnetv2_l(**kwargs):
         """
       Constructs a EfficientNetV2-L model
       """
          cfgs = [
             # t, c, n, s, fused
              [1,  32,  4, 1, 1],
              [4,  64,  7, 2, 1],
              [4,  96,  7, 2, 1],
              [4, 192, 10, 2, 0],
              [6, 224, 19, 1, 0],
              [6, 384, 25, 2, 0],
              [6, 640,  7, 1, 0],
          ]
         return EfficientNetv2(cfgs, **kwargs)
      def efficientnetv2_xl(**kwargs):
         """
       Constructs a EfficientNetV2-XL model
       """
          cfgs = [
             # t, c, n, s, fused
              [1,  32,  4, 1, 1],
              [4,  64,  8, 2, 1],
              [4,  96,  8, 2, 1],
              [4, 192, 16, 2, 0],
              [6, 256, 24, 1, 0],
              [6, 512, 32, 2, 0],
              [6, 640,  8, 1, 0],
          ]
         return EfficientNetv2(cfgs, **kwargs)
      if __name__ == '__main__':
          device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
          model = efficientnetv2_s()
          model.to(device)
          summary(model, (3, 224, 224))
  
 

文章来源: wanghao.blog.csdn.net,作者:AI浩,版权归原作者所有,如需转载,请联系作者。

原文链接:wanghao.blog.csdn.net/article/details/117441033

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。