You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

saliency_encap.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright 2020-2021 Huawei Technologies Co., Ltd
  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. # ============================================================================
  15. """Saliency map encapsulator."""
  16. from mindinsight.datavisual.common.exceptions import TrainJobNotExistError
  17. from mindinsight.explainer.encapsulator.explain_data_encap import ExplanationEncap, ExplanationKeys
  18. class SaliencyEncap(ExplanationEncap):
  19. """Saliency map encapsulator."""
  20. def query_saliency_maps(self,
  21. train_id,
  22. labels,
  23. explainers,
  24. limit,
  25. offset,
  26. sorted_name,
  27. sorted_type,
  28. prediction_types=None):
  29. """
  30. Query saliency maps.
  31. Args:
  32. train_id (str): Job ID.
  33. labels (list[str]): Label filter.
  34. explainers (list[str]): Explainers of saliency maps to be shown.
  35. limit (int): Maximum number of items to be returned.
  36. offset (int): Page offset.
  37. sorted_name (str): Field to be sorted.
  38. sorted_type (str): Sorting order, 'ascending' or 'descending'.
  39. prediction_types (list[str]): Prediction types filter. Default: None.
  40. Returns:
  41. tuple[int, list[dict]], total number of samples after filtering and list of sample result.
  42. """
  43. job = self.job_manager.get_job(train_id)
  44. if job is None:
  45. raise TrainJobNotExistError(train_id)
  46. samples = self._query_samples(job, labels, sorted_name, sorted_type, prediction_types)
  47. sample_infos = []
  48. obj_offset = offset * limit
  49. count = len(samples)
  50. end = count
  51. if obj_offset + limit < end:
  52. end = obj_offset + limit
  53. for i in range(obj_offset, end):
  54. sample = samples[i]
  55. sample_infos.append(self._touch_sample(sample, job, explainers))
  56. return count, sample_infos
  57. def _touch_sample(self, sample, job, explainers):
  58. """
  59. Final editing the sample info.
  60. Args:
  61. sample (dict): Sample info.
  62. job (ExplainJob): Explain job.
  63. explainers (list[str]): Explainer names.
  64. Returns:
  65. dict, the edited sample info.
  66. """
  67. sample_cp = sample.copy()
  68. sample_cp["image"] = self._get_image_url(job.train_id, sample['image'], "original")
  69. for inference in sample_cp["inferences"]:
  70. new_list = []
  71. for saliency_map in inference[ExplanationKeys.SALIENCY.value]:
  72. if explainers and saliency_map["explainer"] not in explainers:
  73. continue
  74. saliency_map["overlay"] = self._get_image_url(job.train_id, saliency_map['overlay'], "overlay")
  75. new_list.append(saliency_map)
  76. inference[ExplanationKeys.SALIENCY.value] = new_list
  77. return sample_cp