Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs with passing images_background to AnchorImage #542

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions alibi/explainers/anchor_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,12 @@ def perturbation(
segments_mask[:, anchor] = 1

# for each sample, need to sample one of the background images if provided
if self.images_background:
if self.images_background is not None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.images_background is either None or np.ndarray so we must check explicitly for None as np.ndarray doesn't have a working __bool__ method.

backgrounds = np.random.choice(
range(len(self.images_background)),
segments_mask.shape[0],
replace=True,
)
segments_mask = np.hstack((segments_mask, backgrounds.reshape(-1, 1)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line appears to be completely random and just doesn't work.

else:
backgrounds = [None] * segments_mask.shape[0]
# create fudged image where the pixel value in each superpixel is set to the
Expand All @@ -247,13 +246,11 @@ def perturbation(
mask = np.zeros(segments.shape).astype(bool)
for superpixel in to_perturb:
mask[segments == superpixel] = True
if background_idx:
if background_idx is not None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main bug. Here background_idx can be 0 but in the previous version it would cause it to go down this (wrong) branch and end up using fudged_image which is not defined. The fix is to explicitly check for None.

# replace values with those of background image
# TODO: Could images_background be None herre?
temp[mask] = self.images_background[background_idx][mask]
else:
# ... or with the averaged superpixel value
# TODO: Where is fudged_image defined?
temp[mask] = fudged_image[mask]
pert_imgs.append(temp)

Expand Down
5 changes: 4 additions & 1 deletion alibi/explainers/tests/test_anchor_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,22 @@ def test_sampler(predict_fn, models, mnist_data):
indirect=True,
ids='models={}'.format
)
def test_anchor_image(predict_fn, models, mnist_data):
@pytest.mark.parametrize('images_background', [True, False], ids='images_background={}'.format)
def test_anchor_image(predict_fn, models, mnist_data, images_background):
x_train = mnist_data["X_train"]
image = x_train[0]

segmentation_fn = "slic"
segmentation_kwargs = {"n_segments": 10, "compactness": 10, "sigma": 0.5}
image_shape = (28, 28, 1)
images_background = x_train[:10] if images_background else None

explainer = AnchorImage(
predict_fn,
image_shape,
segmentation_fn=segmentation_fn,
segmentation_kwargs=segmentation_kwargs,
images_background=images_background
)

p_sample = 0.5 # probability of perturbing a superpixel
Expand Down