det_db_head.py 4.4 KB

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