You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ga_retina_head.py 3.9 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import torch.nn as nn
  3. from mmcv.cnn import ConvModule
  4. from mmcv.ops import MaskedConv2d
  5. from ..builder import HEADS
  6. from .guided_anchor_head import FeatureAdaption, GuidedAnchorHead
  7. @HEADS.register_module()
  8. class GARetinaHead(GuidedAnchorHead):
  9. """Guided-Anchor-based RetinaNet head."""
  10. def __init__(self,
  11. num_classes,
  12. in_channels,
  13. stacked_convs=4,
  14. conv_cfg=None,
  15. norm_cfg=None,
  16. init_cfg=None,
  17. **kwargs):
  18. if init_cfg is None:
  19. init_cfg = dict(
  20. type='Normal',
  21. layer='Conv2d',
  22. std=0.01,
  23. override=[
  24. dict(
  25. type='Normal',
  26. name='conv_loc',
  27. std=0.01,
  28. bias_prob=0.01),
  29. dict(
  30. type='Normal',
  31. name='retina_cls',
  32. std=0.01,
  33. bias_prob=0.01)
  34. ])
  35. self.stacked_convs = stacked_convs
  36. self.conv_cfg = conv_cfg
  37. self.norm_cfg = norm_cfg
  38. super(GARetinaHead, self).__init__(
  39. num_classes, in_channels, init_cfg=init_cfg, **kwargs)
  40. def _init_layers(self):
  41. """Initialize layers of the head."""
  42. self.relu = nn.ReLU(inplace=True)
  43. self.cls_convs = nn.ModuleList()
  44. self.reg_convs = nn.ModuleList()
  45. for i in range(self.stacked_convs):
  46. chn = self.in_channels if i == 0 else self.feat_channels
  47. self.cls_convs.append(
  48. ConvModule(
  49. chn,
  50. self.feat_channels,
  51. 3,
  52. stride=1,
  53. padding=1,
  54. conv_cfg=self.conv_cfg,
  55. norm_cfg=self.norm_cfg))
  56. self.reg_convs.append(
  57. ConvModule(
  58. chn,
  59. self.feat_channels,
  60. 3,
  61. stride=1,
  62. padding=1,
  63. conv_cfg=self.conv_cfg,
  64. norm_cfg=self.norm_cfg))
  65. self.conv_loc = nn.Conv2d(self.feat_channels, 1, 1)
  66. self.conv_shape = nn.Conv2d(self.feat_channels, self.num_anchors * 2,
  67. 1)
  68. self.feature_adaption_cls = FeatureAdaption(
  69. self.feat_channels,
  70. self.feat_channels,
  71. kernel_size=3,
  72. deform_groups=self.deform_groups)
  73. self.feature_adaption_reg = FeatureAdaption(
  74. self.feat_channels,
  75. self.feat_channels,
  76. kernel_size=3,
  77. deform_groups=self.deform_groups)
  78. self.retina_cls = MaskedConv2d(
  79. self.feat_channels,
  80. self.num_base_priors * self.cls_out_channels,
  81. 3,
  82. padding=1)
  83. self.retina_reg = MaskedConv2d(
  84. self.feat_channels, self.num_base_priors * 4, 3, padding=1)
  85. def forward_single(self, x):
  86. """Forward feature map of a single scale level."""
  87. cls_feat = x
  88. reg_feat = x
  89. for cls_conv in self.cls_convs:
  90. cls_feat = cls_conv(cls_feat)
  91. for reg_conv in self.reg_convs:
  92. reg_feat = reg_conv(reg_feat)
  93. loc_pred = self.conv_loc(cls_feat)
  94. shape_pred = self.conv_shape(reg_feat)
  95. cls_feat = self.feature_adaption_cls(cls_feat, shape_pred)
  96. reg_feat = self.feature_adaption_reg(reg_feat, shape_pred)
  97. if not self.training:
  98. mask = loc_pred.sigmoid()[0] >= self.loc_filter_thr
  99. else:
  100. mask = None
  101. cls_score = self.retina_cls(cls_feat, mask)
  102. bbox_pred = self.retina_reg(reg_feat, mask)
  103. return cls_score, bbox_pred, shape_pred, loc_pred

No Description

Contributors (3)