det_db_head.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from ..common import Activation
  5. from ..backbones.det_mobilenet_v3 import ConvBNLayer
  6. class Head(nn.Module):
  7. def __init__(self, in_channels, **kwargs):
  8. super(Head, self).__init__()
  9. self.conv1 = nn.Conv2d(
  10. in_channels=in_channels,
  11. out_channels=in_channels // 4,
  12. kernel_size=3,
  13. padding=1,
  14. bias=False)
  15. self.conv_bn1 = nn.BatchNorm2d(
  16. in_channels // 4)
  17. self.relu1 = Activation(act_type='relu')
  18. self.conv2 = nn.ConvTranspose2d(
  19. in_channels=in_channels // 4,
  20. out_channels=in_channels // 4,
  21. kernel_size=2,
  22. stride=2)
  23. self.conv_bn2 = nn.BatchNorm2d(
  24. in_channels // 4)
  25. self.relu2 = Activation(act_type='relu')
  26. self.conv3 = nn.ConvTranspose2d(
  27. in_channels=in_channels // 4,
  28. out_channels=1,
  29. kernel_size=2,
  30. stride=2)
  31. def forward(self, x, return_f=False):
  32. x = self.conv1(x)
  33. x = self.conv_bn1(x)
  34. x = self.relu1(x)
  35. x = self.conv2(x)
  36. x = self.conv_bn2(x)
  37. x = self.relu2(x)
  38. if return_f is True:
  39. f = x
  40. x = self.conv3(x)
  41. x = torch.sigmoid(x)
  42. if return_f is True:
  43. return x, f
  44. return x
  45. class DBHead(nn.Module):
  46. """
  47. Differentiable Binarization (DB) for text detection:
  48. see https://arxiv.org/abs/1911.08947
  49. args:
  50. params(dict): super parameters for build DB network
  51. """
  52. def __init__(self, in_channels, k=50, **kwargs):
  53. super(DBHead, self).__init__()
  54. self.k = k
  55. binarize_name_list = [
  56. 'conv2d_56', 'batch_norm_47', 'conv2d_transpose_0', 'batch_norm_48',
  57. 'conv2d_transpose_1', 'binarize'
  58. ]
  59. thresh_name_list = [
  60. 'conv2d_57', 'batch_norm_49', 'conv2d_transpose_2', 'batch_norm_50',
  61. 'conv2d_transpose_3', 'thresh'
  62. ]
  63. self.binarize = Head(in_channels, **kwargs)# binarize_name_list)
  64. self.thresh = Head(in_channels, **kwargs)#thresh_name_list)
  65. def step_function(self, x, y):
  66. return torch.reciprocal(1 + torch.exp(-self.k * (x - y)))
  67. def forward(self, x):
  68. shrink_maps = self.binarize(x)
  69. return {'maps': shrink_maps}
  70. class LocalModule(nn.Module):
  71. def __init__(self, in_c, mid_c, use_distance=True):
  72. super(self.__class__, self).__init__()
  73. self.last_3 = ConvBNLayer(in_c + 1, mid_c, 3, 1, 1, act='relu')
  74. self.last_1 = nn.Conv2d(mid_c, 1, 1, 1, 0)
  75. def forward(self, x, init_map, distance_map):
  76. outf = torch.cat([init_map, x], dim=1)
  77. # last Conv
  78. out = self.last_1(self.last_3(outf))
  79. return out
  80. class PFHeadLocal(DBHead):
  81. def __init__(self, in_channels, k=50, mode='small', **kwargs):
  82. super(PFHeadLocal, self).__init__(in_channels, k, **kwargs)
  83. self.mode = mode
  84. self.up_conv = nn.Upsample(scale_factor=2, mode="nearest")
  85. if self.mode == 'large':
  86. self.cbn_layer = LocalModule(in_channels // 4, in_channels // 4)
  87. elif self.mode == 'small':
  88. self.cbn_layer = LocalModule(in_channels // 4, in_channels // 8)
  89. def forward(self, x, targets=None):
  90. shrink_maps, f = self.binarize(x, return_f=True)
  91. base_maps = shrink_maps
  92. cbn_maps = self.cbn_layer(self.up_conv(f), shrink_maps, None)
  93. cbn_maps = F.sigmoid(cbn_maps)
  94. return {'maps': 0.5 * (base_maps + cbn_maps), 'cbn_maps': cbn_maps}