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

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