From 8d2f42e244d64f79a619e91e0af731fbcedbecec Mon Sep 17 00:00:00 2001 From: lixiaohui Date: Sat, 12 Jun 2021 18:29:07 +0800 Subject: [PATCH] Avoid zero devision, fix faithfulness deletion bug --- mindspore/explainer/_utils.py | 1 - .../explanation/_attribution/_perturbation/ablation.py | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mindspore/explainer/_utils.py b/mindspore/explainer/_utils.py index f0b4f1d38d..6458abbe6d 100644 --- a/mindspore/explainer/_utils.py +++ b/mindspore/explainer/_utils.py @@ -233,7 +233,6 @@ def calc_auc(x: _Array) -> _Array: # take mean for multiple patches if the model is fully convolutional model if len(x.shape) == 4: x = np.mean(np.mean(x, axis=2), axis=3) - auc = (x.sum() - x[0] - x[-1]) / len(x) return auc diff --git a/mindspore/explainer/explanation/_attribution/_perturbation/ablation.py b/mindspore/explainer/explanation/_attribution/_perturbation/ablation.py index 531079aecb..e7f8346879 100644 --- a/mindspore/explainer/explanation/_attribution/_perturbation/ablation.py +++ b/mindspore/explainer/explanation/_attribution/_perturbation/ablation.py @@ -164,7 +164,7 @@ class AblationWithSaliency(Ablation): up_bound = low_bound + pixel_per_step for j in range(num_perturbations): masks[i, j, :, ((saliency_rank[i] >= low_bound) & (saliency_rank[i] < up_bound))] = True - low_bound = up_bound + factor + low_bound = up_bound * factor up_bound += pixel_per_step masks = masks if has_channel else np.squeeze(masks, axis=2) @@ -178,6 +178,9 @@ class AblationWithSaliency(Ablation): if self._pixel_per_step: pixel_per_step = self._pixel_per_step num_perturbations = math.floor(num_pixels * self._perturb_percent / self._pixel_per_step) + if not num_perturbations: + raise ValueError("Number of perturbations is not valid. Please enlarge the value of perturb_percent or " + "reduce the value of pixel_per_step when instantiating AblationWithSaliency.") elif self._num_perturbations: pixel_per_step = math.floor(num_pixels * self._perturb_percent / self._num_perturbations) num_perturbations = self._num_perturbations