mobilenet_v2.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import warnings
  3. import torch.nn as nn
  4. from mmcv.cnn import ConvModule
  5. from mmengine.model import BaseModule
  6. from torch.nn.modules.batchnorm import _BatchNorm
  7. from mmdet.registry import MODELS
  8. from ..layers import InvertedResidual
  9. from ..utils import make_divisible
  10. @MODELS.register_module()
  11. class MobileNetV2(BaseModule):
  12. """MobileNetV2 backbone.
  13. Args:
  14. widen_factor (float): Width multiplier, multiply number of
  15. channels in each layer by this amount. Default: 1.0.
  16. out_indices (Sequence[int], optional): Output from which stages.
  17. Default: (1, 2, 4, 7).
  18. frozen_stages (int): Stages to be frozen (all param fixed).
  19. Default: -1, which means not freezing any parameters.
  20. conv_cfg (dict, optional): Config dict for convolution layer.
  21. Default: None, which means using conv2d.
  22. norm_cfg (dict): Config dict for normalization layer.
  23. Default: dict(type='BN').
  24. act_cfg (dict): Config dict for activation layer.
  25. Default: dict(type='ReLU6').
  26. norm_eval (bool): Whether to set norm layers to eval mode, namely,
  27. freeze running stats (mean and var). Note: Effect on Batch Norm
  28. and its variants only. Default: False.
  29. with_cp (bool): Use checkpoint or not. Using checkpoint will save some
  30. memory while slowing down the training speed. Default: False.
  31. pretrained (str, optional): model pretrained path. Default: None
  32. init_cfg (dict or list[dict], optional): Initialization config dict.
  33. Default: None
  34. """
  35. # Parameters to build layers. 4 parameters are needed to construct a
  36. # layer, from left to right: expand_ratio, channel, num_blocks, stride.
  37. arch_settings = [[1, 16, 1, 1], [6, 24, 2, 2], [6, 32, 3, 2],
  38. [6, 64, 4, 2], [6, 96, 3, 1], [6, 160, 3, 2],
  39. [6, 320, 1, 1]]
  40. def __init__(self,
  41. widen_factor=1.,
  42. out_indices=(1, 2, 4, 7),
  43. frozen_stages=-1,
  44. conv_cfg=None,
  45. norm_cfg=dict(type='BN'),
  46. act_cfg=dict(type='ReLU6'),
  47. norm_eval=False,
  48. with_cp=False,
  49. pretrained=None,
  50. init_cfg=None):
  51. super(MobileNetV2, self).__init__(init_cfg)
  52. self.pretrained = pretrained
  53. assert not (init_cfg and pretrained), \
  54. 'init_cfg and pretrained cannot be specified at the same time'
  55. if isinstance(pretrained, str):
  56. warnings.warn('DeprecationWarning: pretrained is deprecated, '
  57. 'please use "init_cfg" instead')
  58. self.init_cfg = dict(type='Pretrained', checkpoint=pretrained)
  59. elif pretrained is None:
  60. if init_cfg is None:
  61. self.init_cfg = [
  62. dict(type='Kaiming', layer='Conv2d'),
  63. dict(
  64. type='Constant',
  65. val=1,
  66. layer=['_BatchNorm', 'GroupNorm'])
  67. ]
  68. else:
  69. raise TypeError('pretrained must be a str or None')
  70. self.widen_factor = widen_factor
  71. self.out_indices = out_indices
  72. if not set(out_indices).issubset(set(range(0, 8))):
  73. raise ValueError('out_indices must be a subset of range'
  74. f'(0, 8). But received {out_indices}')
  75. if frozen_stages not in range(-1, 8):
  76. raise ValueError('frozen_stages must be in range(-1, 8). '
  77. f'But received {frozen_stages}')
  78. self.out_indices = out_indices
  79. self.frozen_stages = frozen_stages
  80. self.conv_cfg = conv_cfg
  81. self.norm_cfg = norm_cfg
  82. self.act_cfg = act_cfg
  83. self.norm_eval = norm_eval
  84. self.with_cp = with_cp
  85. self.in_channels = make_divisible(32 * widen_factor, 8)
  86. self.conv1 = ConvModule(
  87. in_channels=3,
  88. out_channels=self.in_channels,
  89. kernel_size=3,
  90. stride=2,
  91. padding=1,
  92. conv_cfg=self.conv_cfg,
  93. norm_cfg=self.norm_cfg,
  94. act_cfg=self.act_cfg)
  95. self.layers = []
  96. for i, layer_cfg in enumerate(self.arch_settings):
  97. expand_ratio, channel, num_blocks, stride = layer_cfg
  98. out_channels = make_divisible(channel * widen_factor, 8)
  99. inverted_res_layer = self.make_layer(
  100. out_channels=out_channels,
  101. num_blocks=num_blocks,
  102. stride=stride,
  103. expand_ratio=expand_ratio)
  104. layer_name = f'layer{i + 1}'
  105. self.add_module(layer_name, inverted_res_layer)
  106. self.layers.append(layer_name)
  107. if widen_factor > 1.0:
  108. self.out_channel = int(1280 * widen_factor)
  109. else:
  110. self.out_channel = 1280
  111. layer = ConvModule(
  112. in_channels=self.in_channels,
  113. out_channels=self.out_channel,
  114. kernel_size=1,
  115. stride=1,
  116. padding=0,
  117. conv_cfg=self.conv_cfg,
  118. norm_cfg=self.norm_cfg,
  119. act_cfg=self.act_cfg)
  120. self.add_module('conv2', layer)
  121. self.layers.append('conv2')
  122. def make_layer(self, out_channels, num_blocks, stride, expand_ratio):
  123. """Stack InvertedResidual blocks to build a layer for MobileNetV2.
  124. Args:
  125. out_channels (int): out_channels of block.
  126. num_blocks (int): number of blocks.
  127. stride (int): stride of the first block. Default: 1
  128. expand_ratio (int): Expand the number of channels of the
  129. hidden layer in InvertedResidual by this ratio. Default: 6.
  130. """
  131. layers = []
  132. for i in range(num_blocks):
  133. if i >= 1:
  134. stride = 1
  135. layers.append(
  136. InvertedResidual(
  137. self.in_channels,
  138. out_channels,
  139. mid_channels=int(round(self.in_channels * expand_ratio)),
  140. stride=stride,
  141. with_expand_conv=expand_ratio != 1,
  142. conv_cfg=self.conv_cfg,
  143. norm_cfg=self.norm_cfg,
  144. act_cfg=self.act_cfg,
  145. with_cp=self.with_cp))
  146. self.in_channels = out_channels
  147. return nn.Sequential(*layers)
  148. def _freeze_stages(self):
  149. if self.frozen_stages >= 0:
  150. for param in self.conv1.parameters():
  151. param.requires_grad = False
  152. for i in range(1, self.frozen_stages + 1):
  153. layer = getattr(self, f'layer{i}')
  154. layer.eval()
  155. for param in layer.parameters():
  156. param.requires_grad = False
  157. def forward(self, x):
  158. """Forward function."""
  159. x = self.conv1(x)
  160. outs = []
  161. for i, layer_name in enumerate(self.layers):
  162. layer = getattr(self, layer_name)
  163. x = layer(x)
  164. if i in self.out_indices:
  165. outs.append(x)
  166. return tuple(outs)
  167. def train(self, mode=True):
  168. """Convert the model into training mode while keep normalization layer
  169. frozen."""
  170. super(MobileNetV2, self).train(mode)
  171. self._freeze_stages()
  172. if mode and self.norm_eval:
  173. for m in self.modules():
  174. # trick: eval have effect on BatchNorm only
  175. if isinstance(m, _BatchNorm):
  176. m.eval()