base_model.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  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. from paddle import nn
  18. from ppocr.modeling.transforms import build_transform
  19. from ppocr.modeling.backbones import build_backbone
  20. from ppocr.modeling.necks import build_neck
  21. from ppocr.modeling.heads import build_head
  22. __all__ = ['BaseModel']
  23. class BaseModel(nn.Layer):
  24. def __init__(self, config):
  25. """
  26. the module for OCR.
  27. args:
  28. config (dict): the super parameters for module.
  29. """
  30. super(BaseModel, self).__init__()
  31. in_channels = config.get('in_channels', 3)
  32. model_type = config['model_type']
  33. # build transfrom,
  34. # for rec, transfrom can be TPS,None
  35. # for det and cls, transfrom shoule to be None,
  36. # if you make model differently, you can use transfrom in det and cls
  37. if 'Transform' not in config or config['Transform'] is None:
  38. self.use_transform = False
  39. else:
  40. self.use_transform = True
  41. config['Transform']['in_channels'] = in_channels
  42. self.transform = build_transform(config['Transform'])
  43. in_channels = self.transform.out_channels
  44. # build backbone, backbone is need for del, rec and cls
  45. if 'Backbone' not in config or config['Backbone'] is None:
  46. self.use_backbone = False
  47. else:
  48. self.use_backbone = True
  49. config["Backbone"]['in_channels'] = in_channels
  50. self.backbone = build_backbone(config["Backbone"], model_type)
  51. in_channels = self.backbone.out_channels
  52. # build neck
  53. # for rec, neck can be cnn,rnn or reshape(None)
  54. # for det, neck can be FPN, BIFPN and so on.
  55. # for cls, neck should be none
  56. if 'Neck' not in config or config['Neck'] is None:
  57. self.use_neck = False
  58. else:
  59. self.use_neck = True
  60. config['Neck']['in_channels'] = in_channels
  61. self.neck = build_neck(config['Neck'])
  62. in_channels = self.neck.out_channels
  63. # # build head, head is need for det, rec and cls
  64. if 'Head' not in config or config['Head'] is None:
  65. self.use_head = False
  66. else:
  67. self.use_head = True
  68. config["Head"]['in_channels'] = in_channels
  69. self.head = build_head(config["Head"])
  70. self.return_all_feats = config.get("return_all_feats", False)
  71. def forward(self, x, data=None):
  72. y = dict()
  73. if self.use_transform:
  74. x = self.transform(x)
  75. if self.use_backbone:
  76. x = self.backbone(x)
  77. if isinstance(x, dict):
  78. y.update(x)
  79. else:
  80. y["backbone_out"] = x
  81. final_name = "backbone_out"
  82. if self.use_neck:
  83. x = self.neck(x)
  84. if isinstance(x, dict):
  85. y.update(x)
  86. else:
  87. y["neck_out"] = x
  88. final_name = "neck_out"
  89. if self.use_head:
  90. x = self.head(x, targets=data)
  91. # for multi head, save ctc neck out for udml
  92. if isinstance(x, dict) and 'ctc_neck' in x.keys():
  93. y["neck_out"] = x["ctc_neck"]
  94. y["head_out"] = x
  95. elif isinstance(x, dict):
  96. y.update(x)
  97. else:
  98. y["head_out"] = x
  99. final_name = "head_out"
  100. if self.return_all_feats:
  101. if self.training:
  102. return y
  103. elif isinstance(x, dict):
  104. return x
  105. else:
  106. return {final_name: x}
  107. else:
  108. return x