distillation_model.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. from .base_model import BaseModel
  23. from ppocr.utils.save_load import load_pretrained_params
  24. __all__ = ['DistillationModel']
  25. class DistillationModel(nn.Layer):
  26. def __init__(self, config):
  27. """
  28. the module for OCR distillation.
  29. args:
  30. config (dict): the super parameters for module.
  31. """
  32. super().__init__()
  33. self.model_list = []
  34. self.model_name_list = []
  35. for key in config["Models"]:
  36. model_config = config["Models"][key]
  37. freeze_params = False
  38. pretrained = None
  39. if "freeze_params" in model_config:
  40. freeze_params = model_config.pop("freeze_params")
  41. if "pretrained" in model_config:
  42. pretrained = model_config.pop("pretrained")
  43. model = BaseModel(model_config)
  44. if pretrained is not None:
  45. load_pretrained_params(model, pretrained)
  46. if freeze_params:
  47. for param in model.parameters():
  48. param.trainable = False
  49. self.model_list.append(self.add_sublayer(key, model))
  50. self.model_name_list.append(key)
  51. def forward(self, x, data=None):
  52. result_dict = dict()
  53. for idx, model_name in enumerate(self.model_name_list):
  54. result_dict[model_name] = self.model_list[idx](x, data)
  55. return result_dict