123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- from __future__ import absolute_import
- from __future__ import division
- from __future__ import print_function
- from paddle import nn
- from ppocr.modeling.transforms import build_transform
- from ppocr.modeling.backbones import build_backbone
- from ppocr.modeling.necks import build_neck
- from ppocr.modeling.heads import build_head
- __all__ = ['BaseModel']
- class BaseModel(nn.Layer):
- def __init__(self, config):
- """
- the module for OCR.
- args:
- config (dict): the super parameters for module.
- """
- super(BaseModel, self).__init__()
- in_channels = config.get('in_channels', 3)
- model_type = config['model_type']
-
-
-
-
- if 'Transform' not in config or config['Transform'] is None:
- self.use_transform = False
- else:
- self.use_transform = True
- config['Transform']['in_channels'] = in_channels
- self.transform = build_transform(config['Transform'])
- in_channels = self.transform.out_channels
-
- config["Backbone"]['in_channels'] = in_channels
- self.backbone = build_backbone(config["Backbone"], model_type)
- in_channels = self.backbone.out_channels
-
-
-
-
- if 'Neck' not in config or config['Neck'] is None:
- self.use_neck = False
- else:
- self.use_neck = True
- config['Neck']['in_channels'] = in_channels
- self.neck = build_neck(config['Neck'])
- in_channels = self.neck.out_channels
-
- config["Head"]['in_channels'] = in_channels
- self.head = build_head(config["Head"])
- def forward(self, x, data=None):
- if self.use_transform:
- x = self.transform(x)
- x = self.backbone(x)
- if self.use_neck:
- x = self.neck(x)
- if data is None:
- x = self.head(x)
- else:
- x = self.head(x, data)
- return x
|