det_fce_head.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
  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. """
  15. This code is refer from:
  16. https://github.com/open-mmlab/mmocr/blob/main/mmocr/models/textdet/dense_heads/fce_head.py
  17. """
  18. from paddle import nn
  19. from paddle import ParamAttr
  20. import paddle.nn.functional as F
  21. from paddle.nn.initializer import Normal
  22. import paddle
  23. from functools import partial
  24. def multi_apply(func, *args, **kwargs):
  25. pfunc = partial(func, **kwargs) if kwargs else func
  26. map_results = map(pfunc, *args)
  27. return tuple(map(list, zip(*map_results)))
  28. class FCEHead(nn.Layer):
  29. """The class for implementing FCENet head.
  30. FCENet(CVPR2021): Fourier Contour Embedding for Arbitrary-shaped Text
  31. Detection.
  32. [https://arxiv.org/abs/2104.10442]
  33. Args:
  34. in_channels (int): The number of input channels.
  35. scales (list[int]) : The scale of each layer.
  36. fourier_degree (int) : The maximum Fourier transform degree k.
  37. """
  38. def __init__(self, in_channels, fourier_degree=5):
  39. super().__init__()
  40. assert isinstance(in_channels, int)
  41. self.downsample_ratio = 1.0
  42. self.in_channels = in_channels
  43. self.fourier_degree = fourier_degree
  44. self.out_channels_cls = 4
  45. self.out_channels_reg = (2 * self.fourier_degree + 1) * 2
  46. self.out_conv_cls = nn.Conv2D(
  47. in_channels=self.in_channels,
  48. out_channels=self.out_channels_cls,
  49. kernel_size=3,
  50. stride=1,
  51. padding=1,
  52. groups=1,
  53. weight_attr=ParamAttr(
  54. name='cls_weights',
  55. initializer=Normal(
  56. mean=0., std=0.01)),
  57. bias_attr=True)
  58. self.out_conv_reg = nn.Conv2D(
  59. in_channels=self.in_channels,
  60. out_channels=self.out_channels_reg,
  61. kernel_size=3,
  62. stride=1,
  63. padding=1,
  64. groups=1,
  65. weight_attr=ParamAttr(
  66. name='reg_weights',
  67. initializer=Normal(
  68. mean=0., std=0.01)),
  69. bias_attr=True)
  70. def forward(self, feats, targets=None):
  71. cls_res, reg_res = multi_apply(self.forward_single, feats)
  72. level_num = len(cls_res)
  73. outs = {}
  74. if not self.training:
  75. for i in range(level_num):
  76. tr_pred = F.softmax(cls_res[i][:, 0:2, :, :], axis=1)
  77. tcl_pred = F.softmax(cls_res[i][:, 2:, :, :], axis=1)
  78. outs['level_{}'.format(i)] = paddle.concat(
  79. [tr_pred, tcl_pred, reg_res[i]], axis=1)
  80. else:
  81. preds = [[cls_res[i], reg_res[i]] for i in range(level_num)]
  82. outs['levels'] = preds
  83. return outs
  84. def forward_single(self, x):
  85. cls_predict = self.out_conv_cls(x)
  86. reg_predict = self.out_conv_reg(x)
  87. return cls_predict, reg_predict