base_model.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. import paddle
  18. from paddle import nn
  19. from ppocr.modeling.transforms import build_transform
  20. from ppocr.modeling.backbones import build_backbone
  21. from ppocr.modeling.necks import build_neck
  22. from ppocr.modeling.heads import build_head
  23. __all__ = ['BaseModel']
  24. class BaseModel(nn.Layer):
  25. def __init__(self, config):
  26. """
  27. the module for OCR.
  28. args:
  29. config (dict): the super parameters for module.
  30. """
  31. super(BaseModel, self).__init__()
  32. # 输入的通道数
  33. in_channels = config.get('in_channels', 3)
  34. model_type = config['model_type']
  35. # build transfrom,
  36. # for rec, transfrom can be TPS,None
  37. # for det and cls, transfrom shoule to be None,
  38. # if you make model differently, you can use transfrom in det and cls
  39. if 'Transform' not in config or config['Transform'] is None:
  40. self.use_transform = False
  41. else:
  42. self.use_transform = True
  43. config['Transform']['in_channels'] = in_channels
  44. self.transform = build_transform(config['Transform'])
  45. in_channels = self.transform.out_channels
  46. # build backbone, backbone is need for del, rec and cls
  47. # 读取backbone配置,返回对应backbone class
  48. config["Backbone"]['in_channels'] = in_channels
  49. self.backbone = build_backbone(config["Backbone"], model_type)
  50. in_channels = self.backbone.out_channels
  51. # build neck
  52. # for rec, neck can be cnn,rnn or reshape(None)
  53. # for det, neck can be FPN, BIFPN and so on.
  54. # for cls, neck should be none
  55. if 'Neck' not in config or config['Neck'] is None:
  56. self.use_neck = False
  57. else:
  58. self.use_neck = True
  59. config['Neck']['in_channels'] = in_channels
  60. self.neck = build_neck(config['Neck'])
  61. in_channels = self.neck.out_channels
  62. # # build head, head is need for det, rec and cls
  63. config["Head"]['in_channels'] = in_channels
  64. self.head = build_head(config["Head"])
  65. @paddle.jit.to_static
  66. def forward(self, x, data=None):
  67. if self.use_transform:
  68. x = self.transform(x)
  69. x = self.backbone(x)
  70. if self.use_neck:
  71. x = self.neck(x)
  72. if data is None:
  73. x = self.head(x)
  74. else:
  75. x = self.head(x, data)
  76. return x