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.

explain_data_encap.py 6.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. """Common explain data encapsulator base class."""
  16. import copy
  17. from enum import Enum
  18. from mindinsight.utils.exceptions import ParamValueError
  19. def _sort_key_min_confidence(sample, labels):
  20. """Samples sort key by the min. confidence."""
  21. min_confidence = float("+inf")
  22. for inference in sample["inferences"]:
  23. if labels and inference["label"] not in labels:
  24. continue
  25. if inference["confidence"] < min_confidence:
  26. min_confidence = inference["confidence"]
  27. return min_confidence
  28. def _sort_key_max_confidence(sample, labels):
  29. """Samples sort key by the max. confidence."""
  30. max_confidence = float("-inf")
  31. for inference in sample["inferences"]:
  32. if labels and inference["label"] not in labels:
  33. continue
  34. if inference["confidence"] > max_confidence:
  35. max_confidence = inference["confidence"]
  36. return max_confidence
  37. def _sort_key_min_confidence_sd(sample, labels):
  38. """Samples sort key by the min. confidence_sd."""
  39. min_confidence_sd = float("+inf")
  40. for inference in sample["inferences"]:
  41. if labels and inference["label"] not in labels:
  42. continue
  43. confidence_sd = inference.get("confidence_sd", float("+inf"))
  44. if confidence_sd < min_confidence_sd:
  45. min_confidence_sd = confidence_sd
  46. return min_confidence_sd
  47. def _sort_key_max_confidence_sd(sample, labels):
  48. """Samples sort key by the max. confidence_sd."""
  49. max_confidence_sd = float("-inf")
  50. for inference in sample["inferences"]:
  51. if labels and inference["label"] not in labels:
  52. continue
  53. confidence_sd = inference.get("confidence_sd", float("-inf"))
  54. if confidence_sd > max_confidence_sd:
  55. max_confidence_sd = confidence_sd
  56. return max_confidence_sd
  57. class ExplanationKeys(Enum):
  58. """Query type enums."""
  59. HOC = "hoc_layers" # HOC: Hierarchical Occlusion, an explanation method we propose
  60. SALIENCY = "saliency_maps"
  61. class ExplainDataEncap:
  62. """Explain data encapsulator base class."""
  63. def __init__(self, job_manager):
  64. self._job_manager = job_manager
  65. @property
  66. def job_manager(self):
  67. return self._job_manager
  68. class ExplanationEncap(ExplainDataEncap):
  69. """Base encapsulator for explanation queries."""
  70. def __init__(self, image_url_formatter, *args, **kwargs):
  71. super().__init__(*args, **kwargs)
  72. self._image_url_formatter = image_url_formatter
  73. def _query_samples(self,
  74. job,
  75. labels,
  76. sorted_name,
  77. sorted_type,
  78. prediction_types=None,
  79. drop_type=None):
  80. """
  81. Query samples.
  82. Args:
  83. job (ExplainManager): Explain job to be query from.
  84. labels (list[str]): Label filter.
  85. sorted_name (str): Field to be sorted.
  86. sorted_type (str): Sorting order, 'ascending' or 'descending'.
  87. prediction_types (list[str]): Prediction type filter.
  88. drop_type (str, None): When it is None, no filer will be applied. When it is 'hoc_layers', samples without
  89. hoc explanations will be filtered out. When it is 'saliency_maps', samples without saliency explanations
  90. will be filtered out.
  91. Returns:
  92. list[dict], samples to be queried.
  93. """
  94. samples = copy.deepcopy(job.get_all_samples())
  95. if drop_type not in (None, ExplanationKeys.SALIENCY.value, ExplanationKeys.HOC.value):
  96. raise ParamValueError(
  97. f"Argument drop_type valid options: None, {ExplanationKeys.SALIENCY.value}, "
  98. f"{ExplanationKeys.HOC.value}, but got {drop_type}.")
  99. if drop_type is not None:
  100. samples = [sample for sample in samples if any(infer[drop_type] for infer in sample['inferences'])]
  101. if labels:
  102. filtered = []
  103. for sample in samples:
  104. infer_labels = [inference["label"] for inference in sample["inferences"]]
  105. for infer_label in infer_labels:
  106. if infer_label in labels:
  107. filtered.append(sample)
  108. break
  109. samples = filtered
  110. if prediction_types and len(prediction_types) < 3:
  111. filtered = []
  112. for sample in samples:
  113. infer_types = [inference["prediction_type"] for inference in sample["inferences"]]
  114. for infer_type in infer_types:
  115. if infer_type in prediction_types:
  116. filtered.append(sample)
  117. break
  118. samples = filtered
  119. reverse = sorted_type == "descending"
  120. if sorted_name == "confidence":
  121. if reverse:
  122. samples.sort(key=lambda x: _sort_key_max_confidence(x, labels), reverse=reverse)
  123. else:
  124. samples.sort(key=lambda x: _sort_key_min_confidence(x, labels), reverse=reverse)
  125. elif sorted_name == "uncertainty":
  126. if not job.uncertainty_enabled:
  127. raise ParamValueError("Uncertainty is not enabled, sorted_name cannot be 'uncertainty'")
  128. if reverse:
  129. samples.sort(key=lambda x: _sort_key_max_confidence_sd(x, labels), reverse=reverse)
  130. else:
  131. samples.sort(key=lambda x: _sort_key_min_confidence_sd(x, labels), reverse=reverse)
  132. elif sorted_name != "":
  133. raise ParamValueError("sorted_name")
  134. return samples
  135. def _get_image_url(self, train_id, image_path, image_type):
  136. """Returns image's url."""
  137. if self._image_url_formatter is None:
  138. return image_path
  139. return self._image_url_formatter(train_id, image_path, image_type)