prune.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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, division, print_function
  15. import paddle
  16. from paddlex.ppcls.utils import logger
  17. def get_pruner(config, model):
  18. if config.get("Slim", False) and config["Slim"].get("prune", False):
  19. import paddleslim
  20. prune_method_name = config["Slim"]["prune"]["name"].lower()
  21. assert prune_method_name in [
  22. "fpgm", "l1_norm"
  23. ], "The prune methods only support 'fpgm' and 'l1_norm'"
  24. if prune_method_name == "fpgm":
  25. pruner = paddleslim.dygraph.FPGMFilterPruner(
  26. model, [1] + config["Global"]["image_shape"])
  27. else:
  28. pruner = paddleslim.dygraph.L1NormFilterPruner(
  29. model, [1] + config["Global"]["image_shape"])
  30. # prune model
  31. _prune_model(pruner, config, model)
  32. else:
  33. pruner = None
  34. return pruner
  35. def _prune_model(pruner, config, model):
  36. from paddleslim.analysis import dygraph_flops as flops
  37. logger.info("FLOPs before pruning: {}GFLOPs".format(
  38. flops(model, [1] + config["Global"]["image_shape"]) / 1e9))
  39. model.eval()
  40. params = []
  41. for sublayer in model.sublayers():
  42. for param in sublayer.parameters(include_sublayers=False):
  43. if isinstance(sublayer, paddle.nn.Conv2D):
  44. params.append(param.name)
  45. ratios = {}
  46. for param in params:
  47. ratios[param] = config["Slim"]["prune"]["pruned_ratio"]
  48. plan = pruner.prune_vars(ratios, [0])
  49. logger.info("FLOPs after pruning: {}GFLOPs; pruned ratio: {}".format(
  50. flops(model, [1] + config["Global"]["image_shape"]) / 1e9,
  51. plan.pruned_flops))
  52. for param in model.parameters():
  53. if "conv2d" in param.name:
  54. logger.info("{}\t{}".format(param.name, param.shape))
  55. model.train()