normlime.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. # 选择使用0号卡
  3. os.environ['CUDA_VISIBLE_DEVICES'] = '0'
  4. import os.path as osp
  5. import paddlex as pdx
  6. # 下载和解压Imagenet果蔬分类数据集
  7. veg_dataset = 'https://bj.bcebos.com/paddlex/interpret/mini_imagenet_veg.tar.gz'
  8. pdx.utils.download_and_decompress(veg_dataset, path='./')
  9. # 下载和解压已训练好的MobileNetV2模型
  10. model_file = 'https://bj.bcebos.com/paddlex/interpret/mini_imagenet_veg_mobilenetv2.tar.gz'
  11. pdx.utils.download_and_decompress(model_file, path='./')
  12. # 加载模型
  13. model_file = 'mini_imagenet_veg_mobilenetv2'
  14. model = pdx.load_model(model_file)
  15. # 定义测试所用的数据集
  16. dataset = 'mini_imagenet_veg'
  17. test_dataset = pdx.datasets.ImageNet(
  18. data_dir=dataset,
  19. file_list=osp.join(dataset, 'test_list.txt'),
  20. label_list=osp.join(dataset, 'labels.txt'),
  21. transforms=model.test_transforms)
  22. import numpy as np
  23. np.random.seed(5)
  24. perm = np.random.permutation(len(test_dataset.file_list))
  25. for i in range(len(test_dataset.file_list)):
  26. # 可解释性可视化
  27. pdx.interpret.normlime(
  28. test_dataset.file_list[perm[i]][0],
  29. model,
  30. test_dataset,
  31. save_dir='./',
  32. normlime_weights_file='{}_{}.npy'.format(
  33. dataset.split('/')[-1], model.model_name))
  34. if i == 1:
  35. # first iter will have an initialization process, followed by the interpretation.
  36. # second iter will directly load the initialization process, followed by the interpretation.
  37. break