det_sast_head.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # copyright (c) 2019 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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import math
  18. import paddle
  19. from paddle import nn
  20. import paddle.nn.functional as F
  21. from paddle import ParamAttr
  22. class ConvBNLayer(nn.Layer):
  23. def __init__(self,
  24. in_channels,
  25. out_channels,
  26. kernel_size,
  27. stride,
  28. groups=1,
  29. if_act=True,
  30. act=None,
  31. name=None):
  32. super(ConvBNLayer, self).__init__()
  33. self.if_act = if_act
  34. self.act = act
  35. self.conv = nn.Conv2D(
  36. in_channels=in_channels,
  37. out_channels=out_channels,
  38. kernel_size=kernel_size,
  39. stride=stride,
  40. padding=(kernel_size - 1) // 2,
  41. groups=groups,
  42. weight_attr=ParamAttr(name=name + '_weights'),
  43. bias_attr=False)
  44. self.bn = nn.BatchNorm(
  45. num_channels=out_channels,
  46. act=act,
  47. param_attr=ParamAttr(name="bn_" + name + "_scale"),
  48. bias_attr=ParamAttr(name="bn_" + name + "_offset"),
  49. moving_mean_name="bn_" + name + "_mean",
  50. moving_variance_name="bn_" + name + "_variance")
  51. def forward(self, x):
  52. x = self.conv(x)
  53. x = self.bn(x)
  54. return x
  55. class SAST_Header1(nn.Layer):
  56. def __init__(self, in_channels, **kwargs):
  57. super(SAST_Header1, self).__init__()
  58. out_channels = [64, 64, 128]
  59. self.score_conv = nn.Sequential(
  60. ConvBNLayer(in_channels, out_channels[0], 1, 1, act='relu', name='f_score1'),
  61. ConvBNLayer(out_channels[0], out_channels[1], 3, 1, act='relu', name='f_score2'),
  62. ConvBNLayer(out_channels[1], out_channels[2], 1, 1, act='relu', name='f_score3'),
  63. ConvBNLayer(out_channels[2], 1, 3, 1, act=None, name='f_score4')
  64. )
  65. self.border_conv = nn.Sequential(
  66. ConvBNLayer(in_channels, out_channels[0], 1, 1, act='relu', name='f_border1'),
  67. ConvBNLayer(out_channels[0], out_channels[1], 3, 1, act='relu', name='f_border2'),
  68. ConvBNLayer(out_channels[1], out_channels[2], 1, 1, act='relu', name='f_border3'),
  69. ConvBNLayer(out_channels[2], 4, 3, 1, act=None, name='f_border4')
  70. )
  71. def forward(self, x):
  72. f_score = self.score_conv(x)
  73. f_score = F.sigmoid(f_score)
  74. f_border = self.border_conv(x)
  75. return f_score, f_border
  76. class SAST_Header2(nn.Layer):
  77. def __init__(self, in_channels, **kwargs):
  78. super(SAST_Header2, self).__init__()
  79. out_channels = [64, 64, 128]
  80. self.tvo_conv = nn.Sequential(
  81. ConvBNLayer(in_channels, out_channels[0], 1, 1, act='relu', name='f_tvo1'),
  82. ConvBNLayer(out_channels[0], out_channels[1], 3, 1, act='relu', name='f_tvo2'),
  83. ConvBNLayer(out_channels[1], out_channels[2], 1, 1, act='relu', name='f_tvo3'),
  84. ConvBNLayer(out_channels[2], 8, 3, 1, act=None, name='f_tvo4')
  85. )
  86. self.tco_conv = nn.Sequential(
  87. ConvBNLayer(in_channels, out_channels[0], 1, 1, act='relu', name='f_tco1'),
  88. ConvBNLayer(out_channels[0], out_channels[1], 3, 1, act='relu', name='f_tco2'),
  89. ConvBNLayer(out_channels[1], out_channels[2], 1, 1, act='relu', name='f_tco3'),
  90. ConvBNLayer(out_channels[2], 2, 3, 1, act=None, name='f_tco4')
  91. )
  92. def forward(self, x):
  93. f_tvo = self.tvo_conv(x)
  94. f_tco = self.tco_conv(x)
  95. return f_tvo, f_tco
  96. class SASTHead(nn.Layer):
  97. """
  98. """
  99. def __init__(self, in_channels, **kwargs):
  100. super(SASTHead, self).__init__()
  101. self.head1 = SAST_Header1(in_channels)
  102. self.head2 = SAST_Header2(in_channels)
  103. def forward(self, x):
  104. f_score, f_border = self.head1(x)
  105. f_tvo, f_tco = self.head2(x)
  106. predicts = {}
  107. predicts['f_score'] = f_score
  108. predicts['f_border'] = f_border
  109. predicts['f_tvo'] = f_tvo
  110. predicts['f_tco'] = f_tco
  111. return predicts