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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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
  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. query_type="saliency_maps")
  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 editing the 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. sample_cp = sample.copy()
  69. sample_cp["image"] = self._get_image_url(job.train_id, sample['image'], "original")
  70. for inference in sample_cp["inferences"]:
  71. new_list = []
  72. for saliency_map in inference["saliency_maps"]:
  73. if explainers and saliency_map["explainer"] not in explainers:
  74. continue
  75. saliency_map["overlay"] = self._get_image_url(job.train_id, saliency_map['overlay'], "overlay")
  76. new_list.append(saliency_map)
  77. inference["saliency_maps"] = new_list
  78. return sample_cp