base_model.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (c) 2020 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. config["Backbone"]['in_channels'] = in_channels
  46. self.backbone = build_backbone(config["Backbone"], model_type)
  47. in_channels = self.backbone.out_channels
  48. # build neck
  49. # for rec, neck can be cnn,rnn or reshape(None)
  50. # for det, neck can be FPN, BIFPN and so on.
  51. # for cls, neck should be none
  52. if 'Neck' not in config or config['Neck'] is None:
  53. self.use_neck = False
  54. else:
  55. self.use_neck = True
  56. config['Neck']['in_channels'] = in_channels
  57. self.neck = build_neck(config['Neck'])
  58. in_channels = self.neck.out_channels
  59. # # build head, head is need for det, rec and cls
  60. config["Head"]['in_channels'] = in_channels
  61. self.head = build_head(config["Head"])
  62. def forward(self, x, data=None):
  63. if self.use_transform:
  64. x = self.transform(x)
  65. x = self.backbone(x)
  66. if self.use_neck:
  67. x = self.neck(x)
  68. if data is None:
  69. x = self.head(x)
  70. else:
  71. x = self.head(x, data)
  72. return x