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.7 kB

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