resnext.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import math
  3. from mmcv.cnn import build_conv_layer, build_norm_layer
  4. from mmdet.registry import MODELS
  5. from ..layers import ResLayer
  6. from .resnet import Bottleneck as _Bottleneck
  7. from .resnet import ResNet
  8. class Bottleneck(_Bottleneck):
  9. expansion = 4
  10. def __init__(self,
  11. inplanes,
  12. planes,
  13. groups=1,
  14. base_width=4,
  15. base_channels=64,
  16. **kwargs):
  17. """Bottleneck block for ResNeXt.
  18. If style is "pytorch", the stride-two layer is the 3x3 conv layer, if
  19. it is "caffe", the stride-two layer is the first 1x1 conv layer.
  20. """
  21. super(Bottleneck, self).__init__(inplanes, planes, **kwargs)
  22. if groups == 1:
  23. width = self.planes
  24. else:
  25. width = math.floor(self.planes *
  26. (base_width / base_channels)) * groups
  27. self.norm1_name, norm1 = build_norm_layer(
  28. self.norm_cfg, width, postfix=1)
  29. self.norm2_name, norm2 = build_norm_layer(
  30. self.norm_cfg, width, postfix=2)
  31. self.norm3_name, norm3 = build_norm_layer(
  32. self.norm_cfg, self.planes * self.expansion, postfix=3)
  33. self.conv1 = build_conv_layer(
  34. self.conv_cfg,
  35. self.inplanes,
  36. width,
  37. kernel_size=1,
  38. stride=self.conv1_stride,
  39. bias=False)
  40. self.add_module(self.norm1_name, norm1)
  41. fallback_on_stride = False
  42. self.with_modulated_dcn = False
  43. if self.with_dcn:
  44. fallback_on_stride = self.dcn.pop('fallback_on_stride', False)
  45. if not self.with_dcn or fallback_on_stride:
  46. self.conv2 = build_conv_layer(
  47. self.conv_cfg,
  48. width,
  49. width,
  50. kernel_size=3,
  51. stride=self.conv2_stride,
  52. padding=self.dilation,
  53. dilation=self.dilation,
  54. groups=groups,
  55. bias=False)
  56. else:
  57. assert self.conv_cfg is None, 'conv_cfg must be None for DCN'
  58. self.conv2 = build_conv_layer(
  59. self.dcn,
  60. width,
  61. width,
  62. kernel_size=3,
  63. stride=self.conv2_stride,
  64. padding=self.dilation,
  65. dilation=self.dilation,
  66. groups=groups,
  67. bias=False)
  68. self.add_module(self.norm2_name, norm2)
  69. self.conv3 = build_conv_layer(
  70. self.conv_cfg,
  71. width,
  72. self.planes * self.expansion,
  73. kernel_size=1,
  74. bias=False)
  75. self.add_module(self.norm3_name, norm3)
  76. if self.with_plugins:
  77. self._del_block_plugins(self.after_conv1_plugin_names +
  78. self.after_conv2_plugin_names +
  79. self.after_conv3_plugin_names)
  80. self.after_conv1_plugin_names = self.make_block_plugins(
  81. width, self.after_conv1_plugins)
  82. self.after_conv2_plugin_names = self.make_block_plugins(
  83. width, self.after_conv2_plugins)
  84. self.after_conv3_plugin_names = self.make_block_plugins(
  85. self.planes * self.expansion, self.after_conv3_plugins)
  86. def _del_block_plugins(self, plugin_names):
  87. """delete plugins for block if exist.
  88. Args:
  89. plugin_names (list[str]): List of plugins name to delete.
  90. """
  91. assert isinstance(plugin_names, list)
  92. for plugin_name in plugin_names:
  93. del self._modules[plugin_name]
  94. @MODELS.register_module()
  95. class ResNeXt(ResNet):
  96. """ResNeXt backbone.
  97. Args:
  98. depth (int): Depth of resnet, from {18, 34, 50, 101, 152}.
  99. in_channels (int): Number of input image channels. Default: 3.
  100. num_stages (int): Resnet stages. Default: 4.
  101. groups (int): Group of resnext.
  102. base_width (int): Base width of resnext.
  103. strides (Sequence[int]): Strides of the first block of each stage.
  104. dilations (Sequence[int]): Dilation of each stage.
  105. out_indices (Sequence[int]): Output from which stages.
  106. style (str): `pytorch` or `caffe`. If set to "pytorch", the stride-two
  107. layer is the 3x3 conv layer, otherwise the stride-two layer is
  108. the first 1x1 conv layer.
  109. frozen_stages (int): Stages to be frozen (all param fixed). -1 means
  110. not freezing any parameters.
  111. norm_cfg (dict): dictionary to construct and config norm layer.
  112. norm_eval (bool): Whether to set norm layers to eval mode, namely,
  113. freeze running stats (mean and var). Note: Effect on Batch Norm
  114. and its variants only.
  115. with_cp (bool): Use checkpoint or not. Using checkpoint will save some
  116. memory while slowing down the training speed.
  117. zero_init_residual (bool): whether to use zero init for last norm layer
  118. in resblocks to let them behave as identity.
  119. """
  120. arch_settings = {
  121. 50: (Bottleneck, (3, 4, 6, 3)),
  122. 101: (Bottleneck, (3, 4, 23, 3)),
  123. 152: (Bottleneck, (3, 8, 36, 3))
  124. }
  125. def __init__(self, groups=1, base_width=4, **kwargs):
  126. self.groups = groups
  127. self.base_width = base_width
  128. super(ResNeXt, self).__init__(**kwargs)
  129. def make_res_layer(self, **kwargs):
  130. """Pack all blocks in a stage into a ``ResLayer``"""
  131. return ResLayer(
  132. groups=self.groups,
  133. base_width=self.base_width,
  134. base_channels=self.base_channels,
  135. **kwargs)