explanation.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. from .explanation_algorithms import CAM, LIME, NormLIME
  15. from .normlime_base import precompute_normlime_weights
  16. class Explanation(object):
  17. """
  18. Base class for all explanation algorithms.
  19. """
  20. def __init__(self, explanation_algorithm_name, predict_fn, **kwargs):
  21. supported_algorithms = {
  22. 'cam': CAM,
  23. 'lime': LIME,
  24. 'normlime': NormLIME
  25. }
  26. self.algorithm_name = explanation_algorithm_name.lower()
  27. assert self.algorithm_name in supported_algorithms.keys()
  28. self.predict_fn = predict_fn
  29. # initialization for the explanation algorithm.
  30. self.explain_algorithm = supported_algorithms[self.algorithm_name](
  31. self.predict_fn, **kwargs
  32. )#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  33. #
  34. #Licensed under the Apache License, Version 2.0 (the "License");
  35. #you may not use this file except in compliance with the License.
  36. #You may obtain a copy of the License at
  37. #
  38. # http://www.apache.org/licenses/LICENSE-2.0
  39. #
  40. #Unless required by applicable law or agreed to in writing, software
  41. #distributed under the License is distributed on an "AS IS" BASIS,
  42. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  43. #See the License for the specific language governing permissions and
  44. #limitations under the License.
  45. from .explanation_algorithms import CAM, LIME, NormLIME
  46. from .normlime_base import precompute_normlime_weights
  47. class Explanation(object):
  48. """
  49. Base class for all explanation algorithms.
  50. """
  51. def __init__(self, explanation_algorithm_name, predict_fn, label_names, **kwargs):
  52. supported_algorithms = {
  53. 'cam': CAM,
  54. 'lime': LIME,
  55. 'normlime': NormLIME
  56. }
  57. self.algorithm_name = explanation_algorithm_name.lower()
  58. assert self.algorithm_name in supported_algorithms.keys()
  59. self.predict_fn = predict_fn
  60. # initialization for the explanation algorithm.
  61. self.explain_algorithm = supported_algorithms[self.algorithm_name](
  62. self.predict_fn, label_names, **kwargs
  63. )
  64. def explain(self, data_, visualization=True, save_to_disk=True, save_dir='./tmp'):
  65. """
  66. Args:
  67. data_: data_ can be a path or numpy.ndarray.
  68. visualization: whether to show using matplotlib.
  69. save_to_disk: whether to save the figure in local disk.
  70. save_dir: dir to save figure if save_to_disk is True.
  71. Returns:
  72. """
  73. return self.explain_algorithm.explain(data_, visualization, save_to_disk, save_dir)
  74. def explain(self, data_, visualization=True, save_to_disk=True, save_dir='./tmp'):
  75. """
  76. Args:
  77. data_: data_ can be a path or numpy.ndarray.
  78. visualization: whether to show using matplotlib.
  79. save_to_disk: whether to save the figure in local disk.
  80. save_dir: dir to save figure if save_to_disk is True.
  81. Returns:
  82. """
  83. return self.explain_algorithm.explain(data_, visualization, save_to_disk, save_dir)