det_east_head.py 3.6 KB

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