senet.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import math
  18. from paddle import fluid
  19. from paddle.fluid.param_attr import ParamAttr
  20. from ppdet.experimental import mixed_precision_global_state
  21. from ppdet.core.workspace import register, serializable
  22. from .resnext import ResNeXt
  23. __all__ = ['SENet', 'SENetC5']
  24. @register
  25. @serializable
  26. class SENet(ResNeXt):
  27. """
  28. Squeeze-and-Excitation Networks, see https://arxiv.org/abs/1709.01507
  29. Args:
  30. depth (int): SENet depth, should be 50, 101, 152
  31. groups (int): group convolution cardinality
  32. group_width (int): width of each group convolution
  33. freeze_at (int): freeze the backbone at which stage
  34. norm_type (str): normalization type, 'bn', 'sync_bn' or 'affine_channel'
  35. freeze_norm (bool): freeze normalization layers
  36. norm_decay (float): weight decay for normalization layer weights
  37. variant (str): ResNet variant, supports 'a', 'b', 'c', 'd' currently
  38. feature_maps (list): index of the stages whose feature maps are returned
  39. dcn_v2_stages (list): index of stages who select deformable conv v2
  40. """
  41. def __init__(self,
  42. depth=50,
  43. groups=64,
  44. group_width=4,
  45. freeze_at=2,
  46. norm_type='affine_channel',
  47. freeze_norm=True,
  48. norm_decay=0.,
  49. variant='d',
  50. feature_maps=[2, 3, 4, 5],
  51. dcn_v2_stages=[],
  52. std_senet=False,
  53. weight_prefix_name=''):
  54. super(SENet, self).__init__(depth, groups, group_width, freeze_at,
  55. norm_type, freeze_norm, norm_decay, variant,
  56. feature_maps)
  57. if depth < 152:
  58. self.stage_filters = [128, 256, 512, 1024]
  59. else:
  60. self.stage_filters = [256, 512, 1024, 2048]
  61. self.reduction_ratio = 16
  62. self.std_senet = std_senet
  63. self._c1_out_chan_num = 128
  64. self._model_type = 'SEResNeXt'
  65. self.dcn_v2_stages = dcn_v2_stages
  66. def _squeeze_excitation(self, input, num_channels, name=None):
  67. mixed_precision_enabled = mixed_precision_global_state() is not None
  68. pool = fluid.layers.pool2d(
  69. input=input,
  70. pool_size=0,
  71. pool_type='avg',
  72. global_pooling=True,
  73. use_cudnn=mixed_precision_enabled)
  74. stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0)
  75. squeeze = fluid.layers.fc(
  76. input=pool,
  77. size=int(num_channels / self.reduction_ratio),
  78. act='relu',
  79. param_attr=fluid.param_attr.ParamAttr(
  80. initializer=fluid.initializer.Uniform(-stdv, stdv),
  81. name=name + '_sqz_weights'),
  82. bias_attr=ParamAttr(name=name + '_sqz_offset'))
  83. stdv = 1.0 / math.sqrt(squeeze.shape[1] * 1.0)
  84. excitation = fluid.layers.fc(
  85. input=squeeze,
  86. size=num_channels,
  87. act='sigmoid',
  88. param_attr=fluid.param_attr.ParamAttr(
  89. initializer=fluid.initializer.Uniform(-stdv, stdv),
  90. name=name + '_exc_weights'),
  91. bias_attr=ParamAttr(name=name + '_exc_offset'))
  92. scale = fluid.layers.elementwise_mul(x=input, y=excitation, axis=0)
  93. return scale
  94. @register
  95. @serializable
  96. class SENetC5(SENet):
  97. __doc__ = SENet.__doc__
  98. def __init__(self,
  99. depth=50,
  100. groups=64,
  101. group_width=4,
  102. freeze_at=2,
  103. norm_type='affine_channel',
  104. freeze_norm=True,
  105. norm_decay=0.,
  106. variant='d',
  107. feature_maps=[5],
  108. weight_prefix_name=''):
  109. super(SENetC5, self).__init__(depth, groups, group_width, freeze_at,
  110. norm_type, freeze_norm, norm_decay,
  111. variant, feature_maps)
  112. self.severed_head = True