rec_rfl_head.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # copyright (c) 2022 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. """
  15. This code is refer from:
  16. https://github.com/hikopensource/DAVAR-Lab-OCR/blob/main/davarocr/davar_rcg/models/sequence_heads/counting_head.py
  17. """
  18. import paddle
  19. import paddle.nn as nn
  20. from paddle.nn.initializer import TruncatedNormal, Constant, Normal, KaimingNormal
  21. from .rec_att_head import AttentionLSTM
  22. kaiming_init_ = KaimingNormal()
  23. zeros_ = Constant(value=0.)
  24. ones_ = Constant(value=1.)
  25. class CNTHead(nn.Layer):
  26. def __init__(self,
  27. embed_size=512,
  28. encode_length=26,
  29. out_channels=38,
  30. **kwargs):
  31. super(CNTHead, self).__init__()
  32. self.out_channels = out_channels
  33. self.Wv_fusion = nn.Linear(embed_size, embed_size, bias_attr=False)
  34. self.Prediction_visual = nn.Linear(encode_length * embed_size,
  35. self.out_channels)
  36. def forward(self, visual_feature):
  37. b, c, h, w = visual_feature.shape
  38. visual_feature = visual_feature.reshape([b, c, h * w]).transpose(
  39. [0, 2, 1])
  40. visual_feature_num = self.Wv_fusion(visual_feature) # batch * 26 * 512
  41. b, n, c = visual_feature_num.shape
  42. # using visual feature directly calculate the text length
  43. visual_feature_num = visual_feature_num.reshape([b, n * c])
  44. prediction_visual = self.Prediction_visual(visual_feature_num)
  45. return prediction_visual
  46. class RFLHead(nn.Layer):
  47. def __init__(self,
  48. in_channels=512,
  49. hidden_size=256,
  50. batch_max_legnth=25,
  51. out_channels=38,
  52. use_cnt=True,
  53. use_seq=True,
  54. **kwargs):
  55. super(RFLHead, self).__init__()
  56. assert use_cnt or use_seq
  57. self.use_cnt = use_cnt
  58. self.use_seq = use_seq
  59. if self.use_cnt:
  60. self.cnt_head = CNTHead(
  61. embed_size=in_channels,
  62. encode_length=batch_max_legnth + 1,
  63. out_channels=out_channels,
  64. **kwargs)
  65. if self.use_seq:
  66. self.seq_head = AttentionLSTM(
  67. in_channels=in_channels,
  68. out_channels=out_channels,
  69. hidden_size=hidden_size,
  70. **kwargs)
  71. self.batch_max_legnth = batch_max_legnth
  72. self.num_class = out_channels
  73. self.apply(self.init_weights)
  74. def init_weights(self, m):
  75. if isinstance(m, nn.Linear):
  76. kaiming_init_(m.weight)
  77. if isinstance(m, nn.Linear) and m.bias is not None:
  78. zeros_(m.bias)
  79. def forward(self, x, targets=None):
  80. cnt_inputs, seq_inputs = x
  81. if self.use_cnt:
  82. cnt_outputs = self.cnt_head(cnt_inputs)
  83. else:
  84. cnt_outputs = None
  85. if self.use_seq:
  86. if self.training:
  87. seq_outputs = self.seq_head(seq_inputs, targets[0],
  88. self.batch_max_legnth)
  89. else:
  90. seq_outputs = self.seq_head(seq_inputs, None,
  91. self.batch_max_legnth)
  92. return cnt_outputs, seq_outputs
  93. else:
  94. return cnt_outputs