__init__.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 . import prune
  15. from . import quant
  16. from . import distill
  17. from .prune import *
  18. from .quant import *
  19. from .distill import *
  20. import yaml
  21. from paddlex.ppdet.core.workspace import load_config
  22. from paddlex.ppdet.utils.checkpoint import load_pretrain_weight
  23. def build_slim_model(cfg, slim_cfg, mode='train'):
  24. with open(slim_cfg) as f:
  25. slim_load_cfg = yaml.load(f, Loader=yaml.Loader)
  26. if mode != 'train' and slim_load_cfg['slim'] == 'Distill':
  27. return cfg
  28. if slim_load_cfg['slim'] == 'Distill':
  29. model = DistillModel(cfg, slim_cfg)
  30. cfg['model'] = model
  31. elif slim_load_cfg['slim'] == 'DistillPrune':
  32. if mode == 'train':
  33. model = DistillModel(cfg, slim_cfg)
  34. pruner = create(cfg.pruner)
  35. pruner(model.student_model)
  36. else:
  37. model = create(cfg.architecture)
  38. weights = cfg.weights
  39. load_config(slim_cfg)
  40. pruner = create(cfg.pruner)
  41. model = pruner(model)
  42. load_pretrain_weight(model, weights)
  43. cfg['model'] = model
  44. cfg['slim_type'] = cfg.slim
  45. else:
  46. load_config(slim_cfg)
  47. model = create(cfg.architecture)
  48. if mode == 'train':
  49. load_pretrain_weight(model, cfg.pretrain_weights)
  50. slim = create(cfg.slim)
  51. cfg['slim_type'] = cfg.slim
  52. cfg['model'] = slim(model)
  53. cfg['slim'] = slim
  54. if mode != 'train':
  55. load_pretrain_weight(cfg['model'], cfg.weights)
  56. return cfg