fcos_head.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # Copyright (c) 2020 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. import paddle
  19. import paddle.nn as nn
  20. import paddle.nn.functional as F
  21. from paddle import ParamAttr
  22. from paddle.nn.initializer import Normal, Constant
  23. from ppdet.core.workspace import register
  24. from ppdet.modeling.layers import ConvNormLayer
  25. class ScaleReg(nn.Layer):
  26. """
  27. Parameter for scaling the regression outputs.
  28. """
  29. def __init__(self):
  30. super(ScaleReg, self).__init__()
  31. self.scale_reg = self.create_parameter(
  32. shape=[1],
  33. attr=ParamAttr(initializer=Constant(value=1.)),
  34. dtype="float32")
  35. def forward(self, inputs):
  36. out = inputs * self.scale_reg
  37. return out
  38. @register
  39. class FCOSFeat(nn.Layer):
  40. """
  41. FCOSFeat of FCOS
  42. Args:
  43. feat_in (int): The channel number of input Tensor.
  44. feat_out (int): The channel number of output Tensor.
  45. num_convs (int): The convolution number of the FCOSFeat.
  46. norm_type (str): Normalization type, 'bn'/'sync_bn'/'gn'.
  47. use_dcn (bool): Whether to use dcn in tower or not.
  48. """
  49. def __init__(self,
  50. feat_in=256,
  51. feat_out=256,
  52. num_convs=4,
  53. norm_type='bn',
  54. use_dcn=False):
  55. super(FCOSFeat, self).__init__()
  56. self.feat_in = feat_in
  57. self.feat_out = feat_out
  58. self.num_convs = num_convs
  59. self.norm_type = norm_type
  60. self.cls_subnet_convs = []
  61. self.reg_subnet_convs = []
  62. for i in range(self.num_convs):
  63. in_c = feat_in if i == 0 else feat_out
  64. cls_conv_name = 'fcos_head_cls_tower_conv_{}'.format(i)
  65. cls_conv = self.add_sublayer(
  66. cls_conv_name,
  67. ConvNormLayer(
  68. ch_in=in_c,
  69. ch_out=feat_out,
  70. filter_size=3,
  71. stride=1,
  72. norm_type=norm_type,
  73. use_dcn=use_dcn,
  74. bias_on=True,
  75. lr_scale=2.))
  76. self.cls_subnet_convs.append(cls_conv)
  77. reg_conv_name = 'fcos_head_reg_tower_conv_{}'.format(i)
  78. reg_conv = self.add_sublayer(
  79. reg_conv_name,
  80. ConvNormLayer(
  81. ch_in=in_c,
  82. ch_out=feat_out,
  83. filter_size=3,
  84. stride=1,
  85. norm_type=norm_type,
  86. use_dcn=use_dcn,
  87. bias_on=True,
  88. lr_scale=2.))
  89. self.reg_subnet_convs.append(reg_conv)
  90. def forward(self, fpn_feat):
  91. cls_feat = fpn_feat
  92. reg_feat = fpn_feat
  93. for i in range(self.num_convs):
  94. cls_feat = F.relu(self.cls_subnet_convs[i](cls_feat))
  95. reg_feat = F.relu(self.reg_subnet_convs[i](reg_feat))
  96. return cls_feat, reg_feat
  97. @register
  98. class FCOSHead(nn.Layer):
  99. """
  100. FCOSHead
  101. Args:
  102. fcos_feat (object): Instance of 'FCOSFeat'
  103. num_classes (int): Number of classes
  104. fpn_stride (list): The stride of each FPN Layer
  105. prior_prob (float): Used to set the bias init for the class prediction layer
  106. fcos_loss (object): Instance of 'FCOSLoss'
  107. norm_reg_targets (bool): Normalization the regression target if true
  108. centerness_on_reg (bool): The prediction of centerness on regression or clssification branch
  109. """
  110. __inject__ = ['fcos_feat', 'fcos_loss']
  111. __shared__ = ['num_classes']
  112. def __init__(self,
  113. fcos_feat,
  114. num_classes=80,
  115. fpn_stride=[8, 16, 32, 64, 128],
  116. prior_prob=0.01,
  117. fcos_loss='FCOSLoss',
  118. norm_reg_targets=True,
  119. centerness_on_reg=True):
  120. super(FCOSHead, self).__init__()
  121. self.fcos_feat = fcos_feat
  122. self.num_classes = num_classes
  123. self.fpn_stride = fpn_stride
  124. self.prior_prob = prior_prob
  125. self.fcos_loss = fcos_loss
  126. self.norm_reg_targets = norm_reg_targets
  127. self.centerness_on_reg = centerness_on_reg
  128. conv_cls_name = "fcos_head_cls"
  129. bias_init_value = -math.log((1 - self.prior_prob) / self.prior_prob)
  130. self.fcos_head_cls = self.add_sublayer(
  131. conv_cls_name,
  132. nn.Conv2D(
  133. in_channels=256,
  134. out_channels=self.num_classes,
  135. kernel_size=3,
  136. stride=1,
  137. padding=1,
  138. weight_attr=ParamAttr(initializer=Normal(
  139. mean=0., std=0.01)),
  140. bias_attr=ParamAttr(
  141. initializer=Constant(value=bias_init_value))))
  142. conv_reg_name = "fcos_head_reg"
  143. self.fcos_head_reg = self.add_sublayer(
  144. conv_reg_name,
  145. nn.Conv2D(
  146. in_channels=256,
  147. out_channels=4,
  148. kernel_size=3,
  149. stride=1,
  150. padding=1,
  151. weight_attr=ParamAttr(initializer=Normal(
  152. mean=0., std=0.01)),
  153. bias_attr=ParamAttr(initializer=Constant(value=0))))
  154. conv_centerness_name = "fcos_head_centerness"
  155. self.fcos_head_centerness = self.add_sublayer(
  156. conv_centerness_name,
  157. nn.Conv2D(
  158. in_channels=256,
  159. out_channels=1,
  160. kernel_size=3,
  161. stride=1,
  162. padding=1,
  163. weight_attr=ParamAttr(initializer=Normal(
  164. mean=0., std=0.01)),
  165. bias_attr=ParamAttr(initializer=Constant(value=0))))
  166. self.scales_regs = []
  167. for i in range(len(self.fpn_stride)):
  168. lvl = int(math.log(int(self.fpn_stride[i]), 2))
  169. feat_name = 'p{}_feat'.format(lvl)
  170. scale_reg = self.add_sublayer(feat_name, ScaleReg())
  171. self.scales_regs.append(scale_reg)
  172. def _compute_locations_by_level(self, fpn_stride, feature):
  173. """
  174. Compute locations of anchor points of each FPN layer
  175. Args:
  176. fpn_stride (int): The stride of current FPN feature map
  177. feature (Tensor): Tensor of current FPN feature map
  178. Return:
  179. Anchor points locations of current FPN feature map
  180. """
  181. shape_fm = paddle.shape(feature)
  182. shape_fm.stop_gradient = True
  183. h, w = shape_fm[2], shape_fm[3]
  184. shift_x = paddle.arange(0, w * fpn_stride, fpn_stride)
  185. shift_y = paddle.arange(0, h * fpn_stride, fpn_stride)
  186. shift_x = paddle.unsqueeze(shift_x, axis=0)
  187. shift_y = paddle.unsqueeze(shift_y, axis=1)
  188. shift_x = paddle.expand(shift_x, shape=[h, w])
  189. shift_y = paddle.expand(shift_y, shape=[h, w])
  190. shift_x.stop_gradient = True
  191. shift_y.stop_gradient = True
  192. shift_x = paddle.reshape(shift_x, shape=[-1])
  193. shift_y = paddle.reshape(shift_y, shape=[-1])
  194. location = paddle.stack(
  195. [shift_x, shift_y], axis=-1) + float(fpn_stride) / 2
  196. location.stop_gradient = True
  197. return location
  198. def forward(self, fpn_feats, is_training):
  199. assert len(fpn_feats) == len(
  200. self.fpn_stride
  201. ), "The size of fpn_feats is not equal to size of fpn_stride"
  202. cls_logits_list = []
  203. bboxes_reg_list = []
  204. centerness_list = []
  205. for scale_reg, fpn_stride, fpn_feat in zip(self.scales_regs,
  206. self.fpn_stride, fpn_feats):
  207. fcos_cls_feat, fcos_reg_feat = self.fcos_feat(fpn_feat)
  208. cls_logits = self.fcos_head_cls(fcos_cls_feat)
  209. bbox_reg = scale_reg(self.fcos_head_reg(fcos_reg_feat))
  210. if self.centerness_on_reg:
  211. centerness = self.fcos_head_centerness(fcos_reg_feat)
  212. else:
  213. centerness = self.fcos_head_centerness(fcos_cls_feat)
  214. if self.norm_reg_targets:
  215. bbox_reg = F.relu(bbox_reg)
  216. if not is_training:
  217. bbox_reg = bbox_reg * fpn_stride
  218. else:
  219. bbox_reg = paddle.exp(bbox_reg)
  220. cls_logits_list.append(cls_logits)
  221. bboxes_reg_list.append(bbox_reg)
  222. centerness_list.append(centerness)
  223. if not is_training:
  224. locations_list = []
  225. for fpn_stride, feature in zip(self.fpn_stride, fpn_feats):
  226. location = self._compute_locations_by_level(fpn_stride, feature)
  227. locations_list.append(location)
  228. return locations_list, cls_logits_list, bboxes_reg_list, centerness_list
  229. else:
  230. return cls_logits_list, bboxes_reg_list, centerness_list
  231. def get_loss(self, fcos_head_outs, tag_labels, tag_bboxes, tag_centerness):
  232. cls_logits, bboxes_reg, centerness = fcos_head_outs
  233. return self.fcos_loss(cls_logits, bboxes_reg, centerness, tag_labels,
  234. tag_bboxes, tag_centerness)