det_db_head.py 3.7 KB

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