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.

hierarchical_occlusion_encap.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright 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. """Hierarchical Occlusion encapsulator."""
  16. from mindinsight.datavisual.common.exceptions import TrainJobNotExistError
  17. from mindinsight.explainer.encapsulator.explain_data_encap import ExplanationEncap
  18. class HierarchicalOcclusionEncap(ExplanationEncap):
  19. """Hierarchical occlusion encapsulator."""
  20. def query_hierarchical_occlusion(self,
  21. train_id,
  22. labels,
  23. limit,
  24. offset,
  25. sorted_name,
  26. sorted_type,
  27. prediction_types=None
  28. ):
  29. """
  30. Query hierarchical occlusion results.
  31. Args:
  32. train_id (str): Job ID.
  33. labels (list[str]): Label filter.
  34. limit (int): Maximum number of items to be returned.
  35. offset (int): Page offset.
  36. sorted_name (str): Field to be sorted.
  37. sorted_type (str): Sorting order, 'ascending' or 'descending'.
  38. prediction_types (list[str]): Prediction types filter.
  39. Returns:
  40. tuple[int, list[dict]], total number of samples after filtering and list of sample results.
  41. """
  42. job = self.job_manager.get_job(train_id)
  43. if job is None:
  44. raise TrainJobNotExistError(train_id)
  45. samples = self._query_samples(job, labels, sorted_name, sorted_type, prediction_types,
  46. query_type="hoc_layers")
  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))
  56. return count, sample_infos
  57. def _touch_sample(self, sample, job):
  58. """
  59. Final edit on single sample info.
  60. Args:
  61. sample (dict): Sample info.
  62. job (ExplainManager): Explain job.
  63. Returns:
  64. dict, the edited sample info.
  65. """
  66. sample_cp = sample.copy()
  67. sample_cp["image"] = self._get_image_url(job.train_id, sample["image"], "original")
  68. for inference_item in sample_cp["inferences"]:
  69. new_list = []
  70. for idx, hoc_layer in enumerate(inference_item["hoc_layers"]):
  71. hoc_layer["outcome"] = self._get_image_url(job.train_id,
  72. f"{sample['id']}_{inference_item['label']}_{idx}.jpg",
  73. "outcome")
  74. new_list.append(hoc_layer)
  75. inference_item["hoc_layers"] = new_list
  76. return sample_cp