det_east_head.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. padding,
  30. groups=1,
  31. if_act=True,
  32. act=None,
  33. name=None,
  34. ):
  35. super(ConvBNLayer, self).__init__()
  36. self.if_act = if_act
  37. self.act = act
  38. self.conv = nn.Conv2D(
  39. in_channels=in_channels,
  40. out_channels=out_channels,
  41. kernel_size=kernel_size,
  42. stride=stride,
  43. padding=padding,
  44. groups=groups,
  45. weight_attr=ParamAttr(name=name + "_weights"),
  46. bias_attr=False,
  47. )
  48. self.bn = nn.BatchNorm(
  49. num_channels=out_channels,
  50. act=act,
  51. param_attr=ParamAttr(name="bn_" + name + "_scale"),
  52. bias_attr=ParamAttr(name="bn_" + name + "_offset"),
  53. moving_mean_name="bn_" + name + "_mean",
  54. moving_variance_name="bn_" + name + "_variance",
  55. )
  56. def forward(self, x):
  57. x = self.conv(x)
  58. x = self.bn(x)
  59. return x
  60. class EASTHead(nn.Layer):
  61. """ """
  62. def __init__(self, in_channels, model_name, **kwargs):
  63. super(EASTHead, self).__init__()
  64. self.model_name = model_name
  65. if self.model_name == "large":
  66. num_outputs = [128, 64, 1, 8]
  67. else:
  68. num_outputs = [64, 32, 1, 8]
  69. self.det_conv1 = ConvBNLayer(
  70. in_channels=in_channels,
  71. out_channels=num_outputs[0],
  72. kernel_size=3,
  73. stride=1,
  74. padding=1,
  75. if_act=True,
  76. act="relu",
  77. name="det_head1",
  78. )
  79. self.det_conv2 = ConvBNLayer(
  80. in_channels=num_outputs[0],
  81. out_channels=num_outputs[1],
  82. kernel_size=3,
  83. stride=1,
  84. padding=1,
  85. if_act=True,
  86. act="relu",
  87. name="det_head2",
  88. )
  89. self.score_conv = ConvBNLayer(
  90. in_channels=num_outputs[1],
  91. out_channels=num_outputs[2],
  92. kernel_size=1,
  93. stride=1,
  94. padding=0,
  95. if_act=False,
  96. act=None,
  97. name="f_score",
  98. )
  99. self.geo_conv = ConvBNLayer(
  100. in_channels=num_outputs[1],
  101. out_channels=num_outputs[3],
  102. kernel_size=1,
  103. stride=1,
  104. padding=0,
  105. if_act=False,
  106. act=None,
  107. name="f_geo",
  108. )
  109. def forward(self, x, targets=None):
  110. f_det = self.det_conv1(x)
  111. f_det = self.det_conv2(f_det)
  112. f_score = self.score_conv(f_det)
  113. f_score = F.sigmoid(f_score)
  114. f_geo = self.geo_conv(f_det)
  115. f_geo = (F.sigmoid(f_geo) - 0.5) * 2 * 800
  116. pred = {"f_score": f_score, "f_geo": f_geo}
  117. return pred