Avoid zero devision, fix faithfulness deletion bug

This commit is contained in:
lixiaohui 2021-06-12 18:29:07 +08:00
parent 7f731faa9b
commit 8d2f42e244
2 changed files with 4 additions and 2 deletions

View File

@ -233,7 +233,6 @@ def calc_auc(x: _Array) -> _Array:
# take mean for multiple patches if the model is fully convolutional model # take mean for multiple patches if the model is fully convolutional model
if len(x.shape) == 4: if len(x.shape) == 4:
x = np.mean(np.mean(x, axis=2), axis=3) x = np.mean(np.mean(x, axis=2), axis=3)
auc = (x.sum() - x[0] - x[-1]) / len(x) auc = (x.sum() - x[0] - x[-1]) / len(x)
return auc return auc

View File

@ -164,7 +164,7 @@ class AblationWithSaliency(Ablation):
up_bound = low_bound + pixel_per_step up_bound = low_bound + pixel_per_step
for j in range(num_perturbations): for j in range(num_perturbations):
masks[i, j, :, ((saliency_rank[i] >= low_bound) & (saliency_rank[i] < up_bound))] = True 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 up_bound += pixel_per_step
masks = masks if has_channel else np.squeeze(masks, axis=2) masks = masks if has_channel else np.squeeze(masks, axis=2)
@ -178,6 +178,9 @@ class AblationWithSaliency(Ablation):
if self._pixel_per_step: if self._pixel_per_step:
pixel_per_step = self._pixel_per_step pixel_per_step = self._pixel_per_step
num_perturbations = math.floor(num_pixels * self._perturb_percent / 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: elif self._num_perturbations:
pixel_per_step = math.floor(num_pixels * self._perturb_percent / self._num_perturbations) pixel_per_step = math.floor(num_pixels * self._perturb_percent / self._num_perturbations)
num_perturbations = self._num_perturbations num_perturbations = self._num_perturbations