cal_sensitivities_file.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  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. import argparse
  15. import os
  16. # 选择使用0号卡
  17. os.environ['CUDA_VISIBLE_DEVICES'] = '0'
  18. import paddlex as pdx
  19. def cal_sensitivies_file(model_dir, dataset, save_file):
  20. # 加载模型
  21. model = pdx.load_model(model_dir)
  22. # 定义验证所用的数据集
  23. eval_dataset = pdx.datasets.ImageNet(
  24. data_dir=dataset,
  25. file_list=os.path.join(dataset, 'val_list.txt'),
  26. label_list=os.path.join(dataset, 'labels.txt'),
  27. transforms=model.eval_transforms)
  28. pdx.slim.cal_params_sensitivities(
  29. model, save_file, eval_dataset, batch_size=8)
  30. if __name__ == '__main__':
  31. parser = argparse.ArgumentParser(description=__doc__)
  32. parser.add_argument(
  33. "--model_dir",
  34. default="./output/mobilenet/best_model",
  35. type=str,
  36. help="The model path.")
  37. parser.add_argument(
  38. "--dataset",
  39. default="./vegetables_cls",
  40. type=str,
  41. help="The dataset path.")
  42. parser.add_argument(
  43. "--save_file",
  44. default="./sensitivities.data",
  45. type=str,
  46. help="The sensitivities file path.")
  47. args = parser.parse_args()
  48. cal_sensitivies_file(args.model_dir, args.dataset, args.save_file)