det_sast_head.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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__(
  24. self,
  25. in_channels,
  26. out_channels,
  27. kernel_size,
  28. stride,
  29. groups=1,
  30. if_act=True,
  31. act=None,
  32. name=None,
  33. ):
  34. super(ConvBNLayer, self).__init__()
  35. self.if_act = if_act
  36. self.act = act
  37. self.conv = nn.Conv2D(
  38. in_channels=in_channels,
  39. out_channels=out_channels,
  40. kernel_size=kernel_size,
  41. stride=stride,
  42. padding=(kernel_size - 1) // 2,
  43. groups=groups,
  44. weight_attr=ParamAttr(name=name + "_weights"),
  45. bias_attr=False,
  46. )
  47. self.bn = nn.BatchNorm(
  48. num_channels=out_channels,
  49. act=act,
  50. param_attr=ParamAttr(name="bn_" + name + "_scale"),
  51. bias_attr=ParamAttr(name="bn_" + name + "_offset"),
  52. moving_mean_name="bn_" + name + "_mean",
  53. moving_variance_name="bn_" + name + "_variance",
  54. )
  55. def forward(self, x):
  56. x = self.conv(x)
  57. x = self.bn(x)
  58. return x
  59. class SAST_Header1(nn.Layer):
  60. def __init__(self, in_channels, **kwargs):
  61. super(SAST_Header1, self).__init__()
  62. out_channels = [64, 64, 128]
  63. self.score_conv = nn.Sequential(
  64. ConvBNLayer(
  65. in_channels, out_channels[0], 1, 1, act="relu", name="f_score1"
  66. ),
  67. ConvBNLayer(
  68. out_channels[0], out_channels[1], 3, 1, act="relu", name="f_score2"
  69. ),
  70. ConvBNLayer(
  71. out_channels[1], out_channels[2], 1, 1, act="relu", name="f_score3"
  72. ),
  73. ConvBNLayer(out_channels[2], 1, 3, 1, act=None, name="f_score4"),
  74. )
  75. self.border_conv = nn.Sequential(
  76. ConvBNLayer(
  77. in_channels, out_channels[0], 1, 1, act="relu", name="f_border1"
  78. ),
  79. ConvBNLayer(
  80. out_channels[0], out_channels[1], 3, 1, act="relu", name="f_border2"
  81. ),
  82. ConvBNLayer(
  83. out_channels[1], out_channels[2], 1, 1, act="relu", name="f_border3"
  84. ),
  85. ConvBNLayer(out_channels[2], 4, 3, 1, act=None, name="f_border4"),
  86. )
  87. def forward(self, x):
  88. f_score = self.score_conv(x)
  89. f_score = F.sigmoid(f_score)
  90. f_border = self.border_conv(x)
  91. return f_score, f_border
  92. class SAST_Header2(nn.Layer):
  93. def __init__(self, in_channels, **kwargs):
  94. super(SAST_Header2, self).__init__()
  95. out_channels = [64, 64, 128]
  96. self.tvo_conv = nn.Sequential(
  97. ConvBNLayer(in_channels, out_channels[0], 1, 1, act="relu", name="f_tvo1"),
  98. ConvBNLayer(
  99. out_channels[0], out_channels[1], 3, 1, act="relu", name="f_tvo2"
  100. ),
  101. ConvBNLayer(
  102. out_channels[1], out_channels[2], 1, 1, act="relu", name="f_tvo3"
  103. ),
  104. ConvBNLayer(out_channels[2], 8, 3, 1, act=None, name="f_tvo4"),
  105. )
  106. self.tco_conv = nn.Sequential(
  107. ConvBNLayer(in_channels, out_channels[0], 1, 1, act="relu", name="f_tco1"),
  108. ConvBNLayer(
  109. out_channels[0], out_channels[1], 3, 1, act="relu", name="f_tco2"
  110. ),
  111. ConvBNLayer(
  112. out_channels[1], out_channels[2], 1, 1, act="relu", name="f_tco3"
  113. ),
  114. ConvBNLayer(out_channels[2], 2, 3, 1, act=None, name="f_tco4"),
  115. )
  116. def forward(self, x):
  117. f_tvo = self.tvo_conv(x)
  118. f_tco = self.tco_conv(x)
  119. return f_tvo, f_tco
  120. class SASTHead(nn.Layer):
  121. """ """
  122. def __init__(self, in_channels, **kwargs):
  123. super(SASTHead, self).__init__()
  124. self.head1 = SAST_Header1(in_channels)
  125. self.head2 = SAST_Header2(in_channels)
  126. def forward(self, x, targets=None):
  127. f_score, f_border = self.head1(x)
  128. f_tvo, f_tco = self.head2(x)
  129. predicts = {}
  130. predicts["f_score"] = f_score
  131. predicts["f_border"] = f_border
  132. predicts["f_tvo"] = f_tvo
  133. predicts["f_tco"] = f_tco
  134. return predicts