interpretation.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 .interpretation_algorithms import CAM, LIME, NormLIME
  15. from .normlime_base import precompute_normlime_weights
  16. class Interpretation(object):
  17. """
  18. Base class for all interpretation algorithms.
  19. """
  20. def __init__(self, interpretation_algorithm_name, predict_fn, label_names,
  21. **kwargs):
  22. supported_algorithms = {'cam': CAM, 'lime': LIME, 'normlime': NormLIME}
  23. self.algorithm_name = interpretation_algorithm_name.lower()
  24. assert self.algorithm_name in supported_algorithms.keys()
  25. self.predict_fn = predict_fn
  26. # initialization for the interpretation algorithm.
  27. self.algorithm = supported_algorithms[self.algorithm_name](
  28. self.predict_fn, label_names, **kwargs)
  29. def interpret(self, data_, visualization=True, save_dir='./'):
  30. """
  31. Args:
  32. data_: data_ can be a path or numpy.ndarray.
  33. visualization: whether to show using matplotlib.
  34. save_dir: dir to save figure if save_to_disk is True.
  35. Returns:
  36. """
  37. return self.algorithm.interpret(data_, visualization, save_dir)