当前位置:   article > 正文

MobileNets V3神经网络简介与代码实战_mobilenetv3代码详解

mobilenetv3代码详解

1.介绍
    从MobileNet V3的名字,我们就知道,它是对基于MobileNet V1和 MobileNet V2而进行改进的,但它的结构不是单纯通过人工设计的,而是结合了神经架构搜索,更加详细的介绍可以参见:Searching for MobileNetV3

2.模型结构
   MobileNet V3结构中包含了深度可分离卷积MobileNets V1神经网络简介与代码实战_天竺街潜水的八角的博客-CSDN博客)和颠倒残差块MobileNets V2神经网络简介与代码实战_天竺街潜水的八角的博客-CSDN博客),可见我以前的博客,这里就重点介绍一下MnasNet 模型中SE结构(轻量级注意力结构),在下图中的黄色框框区域。

   把黄色区域展开,就是下面这张图片,SENet的核心思想在于通过网络根据loss去学习特征权重,使得有效的feature map权重大,无效或效果小的feature map权重小的方式训练模型达到更好的结果。

 

3.模型特点
    MobileNet V3相对于MobileNets V1和MobileNets V2有以下两个特点:

    1. 引入SE结构

    2. 引入h-swish激活函数(作者发现swish激活函数能够有效提高网络的精度。然而,swish的计算量太大了,所以改进出h-swish)

 

    3. 相对于MobileNets V2(Original Last Stage),减掉了中间的3x3卷积block和1x1卷积block,作用:减少计算量的同时并使得网络没有失去准确率(高维的特征空间还在)

4.代码实现 pytorch

  1. class hswish(nn.Module):
  2. def forward(self, x):
  3. out = x * F.relu6(x + 3, inplace=True) / 6
  4. return out
  5. class hsigmoid(nn.Module):
  6. def forward(self, x):
  7. out = F.relu6(x + 3, inplace=True) / 6
  8. return out
  9. class SeModule(nn.Module):
  10. def __init__(self, in_size, reduction=4):
  11. super(SeModule, self).__init__()
  12. self.se = nn.Sequential(
  13. nn.AdaptiveAvgPool2d(1),
  14. nn.Conv2d(in_size, in_size // reduction, kernel_size=1, stride=1, padding=0, bias=False),
  15. nn.BatchNorm2d(in_size // reduction),
  16. nn.ReLU(inplace=True),
  17. nn.Conv2d(in_size // reduction, in_size, kernel_size=1, stride=1, padding=0, bias=False),
  18. nn.BatchNorm2d(in_size),
  19. hsigmoid()
  20. )
  21. def forward(self, x):
  22. return x * self.se(x)
  23. class Block(nn.Module):
  24. '''expand + depthwise + pointwise'''
  25. def __init__(self, kernel_size, in_size, expand_size, out_size, nolinear, semodule, stride):
  26. super(Block, self).__init__()
  27. self.stride = stride
  28. self.se = semodule
  29. self.conv1 = nn.Conv2d(in_size, expand_size, kernel_size=1, stride=1, padding=0, bias=False)
  30. self.bn1 = nn.BatchNorm2d(expand_size)
  31. self.nolinear1 = nolinear
  32. self.conv2 = nn.Conv2d(expand_size, expand_size, kernel_size=kernel_size, stride=stride, padding=kernel_size//2, groups=expand_size, bias=False)
  33. self.bn2 = nn.BatchNorm2d(expand_size)
  34. self.nolinear2 = nolinear
  35. self.conv3 = nn.Conv2d(expand_size, out_size, kernel_size=1, stride=1, padding=0, bias=False)
  36. self.bn3 = nn.BatchNorm2d(out_size)
  37. self.shortcut = nn.Sequential()
  38. if stride == 1 and in_size != out_size:
  39. self.shortcut = nn.Sequential(
  40. nn.Conv2d(in_size, out_size, kernel_size=1, stride=1, padding=0, bias=False),
  41. nn.BatchNorm2d(out_size),
  42. )
  43. def forward(self, x):
  44. out = self.nolinear1(self.bn1(self.conv1(x)))
  45. out = self.nolinear2(self.bn2(self.conv2(out)))
  46. out = self.bn3(self.conv3(out))
  47. if self.se != None:
  48. out = self.se(out)
  49. out = out + self.shortcut(x) if self.stride==1 else out
  50. return out
  51. class MobileNetV3_Large(nn.Module):
  52. def __init__(self, num_classes=1000):
  53. super(MobileNetV3_Large, self).__init__()
  54. self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1, bias=False)
  55. self.bn1 = nn.BatchNorm2d(16)
  56. self.hs1 = hswish()
  57. self.bneck = nn.Sequential(
  58. Block(3, 16, 16, 16, nn.ReLU(inplace=True), None, 1),
  59. Block(3, 16, 64, 24, nn.ReLU(inplace=True), None, 2),
  60. Block(3, 24, 72, 24, nn.ReLU(inplace=True), None, 1),
  61. Block(5, 24, 72, 40, nn.ReLU(inplace=True), SeModule(40), 2),
  62. Block(5, 40, 120, 40, nn.ReLU(inplace=True), SeModule(40), 1),
  63. Block(5, 40, 120, 40, nn.ReLU(inplace=True), SeModule(40), 1),
  64. Block(3, 40, 240, 80, hswish(), None, 2),
  65. Block(3, 80, 200, 80, hswish(), None, 1),
  66. Block(3, 80, 184, 80, hswish(), None, 1),
  67. Block(3, 80, 184, 80, hswish(), None, 1),
  68. Block(3, 80, 480, 112, hswish(), SeModule(112), 1),
  69. Block(3, 112, 672, 112, hswish(), SeModule(112), 1),
  70. Block(5, 112, 672, 160, hswish(), SeModule(160), 1),
  71. Block(5, 160, 672, 160, hswish(), SeModule(160), 2),
  72. Block(5, 160, 960, 160, hswish(), SeModule(160), 1),
  73. )
  74. self.conv2 = nn.Conv2d(160, 960, kernel_size=1, stride=1, padding=0, bias=False)
  75. self.bn2 = nn.BatchNorm2d(960)
  76. self.hs2 = hswish()
  77. self.linear3 = nn.Linear(960, 1280)
  78. self.bn3 = nn.BatchNorm1d(1280)
  79. self.hs3 = hswish()
  80. self.linear4 = nn.Linear(1280, num_classes)
  81. self.init_params()
  82. def init_params(self):
  83. for m in self.modules():
  84. if isinstance(m, nn.Conv2d):
  85. init.kaiming_normal_(m.weight, mode='fan_out')
  86. if m.bias is not None:
  87. init.constant_(m.bias, 0)
  88. elif isinstance(m, nn.BatchNorm2d):
  89. init.constant_(m.weight, 1)
  90. init.constant_(m.bias, 0)
  91. elif isinstance(m, nn.Linear):
  92. init.normal_(m.weight, std=0.001)
  93. if m.bias is not None:
  94. init.constant_(m.bias, 0)
  95. def forward(self, x):
  96. out = self.hs1(self.bn1(self.conv1(x)))
  97. out = self.bneck(out)
  98. out = self.hs2(self.bn2(self.conv2(out)))
  99. out = F.avg_pool2d(out, 7)
  100. out = out.view(out.size(0), -1)
  101. out = self.hs3(self.bn3(self.linear3(out)))
  102. out = self.linear4(out)
  103. return out
  104. class MobileNetV3_Small(nn.Module):
  105. def __init__(self, num_classes=1000):
  106. super(MobileNetV3_Small, self).__init__()
  107. self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1, bias=False)
  108. self.bn1 = nn.BatchNorm2d(16)
  109. self.hs1 = hswish()
  110. self.bneck = nn.Sequential(
  111. Block(3, 16, 16, 16, nn.ReLU(inplace=True), SeModule(16), 2),
  112. Block(3, 16, 72, 24, nn.ReLU(inplace=True), None, 2),
  113. Block(3, 24, 88, 24, nn.ReLU(inplace=True), None, 1),
  114. Block(5, 24, 96, 40, hswish(), SeModule(40), 2),
  115. Block(5, 40, 240, 40, hswish(), SeModule(40), 1),
  116. Block(5, 40, 240, 40, hswish(), SeModule(40), 1),
  117. Block(5, 40, 120, 48, hswish(), SeModule(48), 1),
  118. Block(5, 48, 144, 48, hswish(), SeModule(48), 1),
  119. Block(5, 48, 288, 96, hswish(), SeModule(96), 2),
  120. Block(5, 96, 576, 96, hswish(), SeModule(96), 1),
  121. Block(5, 96, 576, 96, hswish(), SeModule(96), 1),
  122. )
  123. self.conv2 = nn.Conv2d(96, 576, kernel_size=1, stride=1, padding=0, bias=False)
  124. self.bn2 = nn.BatchNorm2d(576)
  125. self.hs2 = hswish()
  126. self.linear3 = nn.Linear(576, 1280)
  127. self.bn3 = nn.BatchNorm1d(1280)
  128. self.hs3 = hswish()
  129. self.linear4 = nn.Linear(1280, num_classes)
  130. self.init_params()
  131. def init_params(self):
  132. for m in self.modules():
  133. if isinstance(m, nn.Conv2d):
  134. init.kaiming_normal_(m.weight, mode='fan_out')
  135. if m.bias is not None:
  136. init.constant_(m.bias, 0)
  137. elif isinstance(m, nn.BatchNorm2d):
  138. init.constant_(m.weight, 1)
  139. init.constant_(m.bias, 0)
  140. elif isinstance(m, nn.Linear):
  141. init.normal_(m.weight, std=0.001)
  142. if m.bias is not None:
  143. init.constant_(m.bias, 0)
  144. def forward(self, x):
  145. out = self.hs1(self.bn1(self.conv1(x)))
  146. out = self.bneck(out)
  147. out = self.hs2(self.bn2(self.conv2(out)))
  148. out = F.avg_pool2d(out, 7)
  149. out = out.view(out.size(0), -1)
  150. out = self.hs3(self.bn3(self.linear3(out)))
  151. out = self.linear4(out)
  152. return out

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/161686
推荐阅读
相关标签
  

闽ICP备14008679号