From 5ed0766a8015b8beea130114f17e375c21898a2d Mon Sep 17 00:00:00 2001 From: lixiaohui Date: Sat, 31 Oct 2020 09:51:55 +0800 Subject: [PATCH] Fixbug: Numpy warning during correlation calculation when all zero saliency map encountered --- mindspore/explainer/benchmark/_attribution/faithfulness.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mindspore/explainer/benchmark/_attribution/faithfulness.py b/mindspore/explainer/benchmark/_attribution/faithfulness.py index ff946ebc3b..8233728bd2 100644 --- a/mindspore/explainer/benchmark/_attribution/faithfulness.py +++ b/mindspore/explainer/benchmark/_attribution/faithfulness.py @@ -19,6 +19,7 @@ from typing import Callable, Optional, Union, Tuple import numpy as np from scipy.ndimage.filters import gaussian_filter +from mindspore import log import mindspore as ms import mindspore.nn as nn import mindspore.ops.operations as op @@ -332,6 +333,11 @@ class NaiveFaithfulness(_FaithfulnessHelper): - faithfulness (np.ndarray): faithfulness score """ + if not np.count_nonzero(saliency): + log.warning("The saliency map is zero everywhere. The correlation will be set to zero.") + correlation = 0 + normalized_faithfulness = (correlation + 1) / 2 + return np.array([normalized_faithfulness], np.float) reference = self._get_reference(inputs) perturbations, masks = self._perturb( inputs, saliency, reference, return_mask=True)