__init__.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 . import unstructured_prune
  18. from .prune import *
  19. from .quant import *
  20. from .distill import *
  21. from .unstructured_prune import *
  22. import yaml
  23. from paddlex.ppdet.core.workspace import load_config
  24. from paddlex.ppdet.utils.checkpoint import load_pretrain_weight
  25. def build_slim_model(cfg, slim_cfg, mode='train'):
  26. with open(slim_cfg) as f:
  27. slim_load_cfg = yaml.load(f, Loader=yaml.Loader)
  28. if mode != 'train' and slim_load_cfg['slim'] == 'Distill':
  29. return cfg
  30. if slim_load_cfg['slim'] == 'Distill':
  31. model = DistillModel(cfg, slim_cfg)
  32. cfg['model'] = model
  33. elif slim_load_cfg['slim'] == 'DistillPrune':
  34. if mode == 'train':
  35. model = DistillModel(cfg, slim_cfg)
  36. pruner = create(cfg.pruner)
  37. pruner(model.student_model)
  38. else:
  39. model = create(cfg.architecture)
  40. weights = cfg.weights
  41. load_config(slim_cfg)
  42. pruner = create(cfg.pruner)
  43. model = pruner(model)
  44. load_pretrain_weight(model, weights)
  45. cfg['model'] = model
  46. cfg['slim_type'] = cfg.slim
  47. elif slim_load_cfg['slim'] == 'PTQ':
  48. model = create(cfg.architecture)
  49. load_config(slim_cfg)
  50. load_pretrain_weight(model, cfg.weights)
  51. slim = create(cfg.slim)
  52. cfg['slim_type'] = cfg.slim
  53. cfg['model'] = slim(model)
  54. cfg['slim'] = slim
  55. elif slim_load_cfg['slim'] == 'UnstructuredPruner':
  56. load_config(slim_cfg)
  57. slim = create(cfg.slim)
  58. cfg['slim_type'] = cfg.slim
  59. cfg['slim'] = slim
  60. cfg['unstructured_prune'] = True
  61. else:
  62. load_config(slim_cfg)
  63. model = create(cfg.architecture)
  64. if mode == 'train':
  65. load_pretrain_weight(model, cfg.pretrain_weights)
  66. slim = create(cfg.slim)
  67. cfg['slim_type'] = cfg.slim
  68. # TODO: fix quant export model in framework.
  69. if mode == 'test' and slim_load_cfg['slim'] == 'QAT':
  70. slim.quant_config['activation_preprocess_type'] = None
  71. cfg['model'] = slim(model)
  72. cfg['slim'] = slim
  73. if mode != 'train':
  74. load_pretrain_weight(cfg['model'], cfg.weights)
  75. return cfg