det_db_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. def get_bias_attr(k, name):
  23. stdv = 1.0 / math.sqrt(k * 1.0)
  24. initializer = paddle.nn.initializer.Uniform(-stdv, stdv)
  25. bias_attr = ParamAttr(initializer=initializer, name=name + "_b_attr")
  26. return bias_attr
  27. class Head(nn.Layer):
  28. def __init__(self, in_channels, name_list):
  29. super(Head, self).__init__()
  30. self.conv1 = nn.Conv2D(
  31. in_channels=in_channels,
  32. out_channels=in_channels // 4,
  33. kernel_size=3,
  34. padding=1,
  35. weight_attr=ParamAttr(name=name_list[0] + '.w_0'),
  36. bias_attr=False)
  37. self.conv_bn1 = nn.BatchNorm(
  38. num_channels=in_channels // 4,
  39. param_attr=ParamAttr(
  40. name=name_list[1] + '.w_0',
  41. initializer=paddle.nn.initializer.Constant(value=1.0)),
  42. bias_attr=ParamAttr(
  43. name=name_list[1] + '.b_0',
  44. initializer=paddle.nn.initializer.Constant(value=1e-4)),
  45. moving_mean_name=name_list[1] + '.w_1',
  46. moving_variance_name=name_list[1] + '.w_2',
  47. act='relu')
  48. self.conv2 = nn.Conv2DTranspose(
  49. in_channels=in_channels // 4,
  50. out_channels=in_channels // 4,
  51. kernel_size=2,
  52. stride=2,
  53. weight_attr=ParamAttr(
  54. name=name_list[2] + '.w_0',
  55. initializer=paddle.nn.initializer.KaimingUniform()),
  56. bias_attr=get_bias_attr(in_channels // 4, name_list[-1] + "conv2"))
  57. self.conv_bn2 = nn.BatchNorm(
  58. num_channels=in_channels // 4,
  59. param_attr=ParamAttr(
  60. name=name_list[3] + '.w_0',
  61. initializer=paddle.nn.initializer.Constant(value=1.0)),
  62. bias_attr=ParamAttr(
  63. name=name_list[3] + '.b_0',
  64. initializer=paddle.nn.initializer.Constant(value=1e-4)),
  65. moving_mean_name=name_list[3] + '.w_1',
  66. moving_variance_name=name_list[3] + '.w_2',
  67. act="relu")
  68. self.conv3 = nn.Conv2DTranspose(
  69. in_channels=in_channels // 4,
  70. out_channels=1,
  71. kernel_size=2,
  72. stride=2,
  73. weight_attr=ParamAttr(
  74. name=name_list[4] + '.w_0',
  75. initializer=paddle.nn.initializer.KaimingUniform()),
  76. bias_attr=get_bias_attr(in_channels // 4, name_list[-1] + "conv3"),
  77. )
  78. def forward(self, x):
  79. x = self.conv1(x)
  80. x = self.conv_bn1(x)
  81. x = self.conv2(x)
  82. x = self.conv_bn2(x)
  83. x = self.conv3(x)
  84. x = F.sigmoid(x)
  85. return x
  86. class DBHead(nn.Layer):
  87. """
  88. Differentiable Binarization (DB) for text detection:
  89. see https://arxiv.org/abs/1911.08947
  90. args:
  91. params(dict): super parameters for build DB network
  92. """
  93. def __init__(self, in_channels, k=50, **kwargs):
  94. super(DBHead, self).__init__()
  95. self.k = k
  96. binarize_name_list = [
  97. 'conv2d_56', 'batch_norm_47', 'conv2d_transpose_0', 'batch_norm_48',
  98. 'conv2d_transpose_1', 'binarize'
  99. ]
  100. thresh_name_list = [
  101. 'conv2d_57', 'batch_norm_49', 'conv2d_transpose_2', 'batch_norm_50',
  102. 'conv2d_transpose_3', 'thresh'
  103. ]
  104. self.binarize = Head(in_channels, binarize_name_list)
  105. self.thresh = Head(in_channels, thresh_name_list)
  106. def step_function(self, x, y):
  107. return paddle.reciprocal(1 + paddle.exp(-self.k * (x - y)))
  108. def forward(self, x):
  109. shrink_maps = self.binarize(x)
  110. if not self.training:
  111. return {'maps': shrink_maps}
  112. threshold_maps = self.thresh(x)
  113. binary_maps = self.step_function(shrink_maps, threshold_maps)
  114. y = paddle.concat([shrink_maps, threshold_maps, binary_maps], axis=1)
  115. return {'maps': y}