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

Conversation

jklaise
Copy link
Contributor

@jklaise jklaise commented Nov 25, 2021

Resolves #540.

I have manually checked that the code now works passing an array of images_background and that the proposed anchor is actually superimposed on these background images. I will see if an automated test can also be written relatively easily.

@@ -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.

@@ -247,7 +246,7 @@ 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.

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.

@codecov
Copy link

codecov bot commented Nov 25, 2021

Codecov Report

Merging #542 (458a773) into master (41d348e) will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #542      +/-   ##
==========================================
+ Coverage   82.46%   82.50%   +0.03%     
==========================================
  Files          77       77              
  Lines       10416    10418       +2     
==========================================
+ Hits         8590     8595       +5     
+ Misses       1826     1823       -3     
Impacted Files Coverage Δ
alibi/explainers/anchor_image.py 93.12% <100.00%> (+1.82%) ⬆️
alibi/explainers/tests/test_anchor_image.py 97.97% <100.00%> (+0.04%) ⬆️
alibi/explainers/tests/test_shap_wrappers.py 97.40% <0.00%> (+<0.01%) ⬆️

@jklaise
Copy link
Contributor Author

jklaise commented Nov 25, 2021

Added a simple test by parametrizing images_background. PR should be ready for review.

Copy link
Collaborator

@RobertSamoilescu RobertSamoilescu left a comment

Choose a reason for hiding this comment

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

@jklaise, looks good to me!

@RobertSamoilescu RobertSamoilescu merged commit 8f0c759 into SeldonIO:master Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'UnboundLocalError: local variable 'fudged_image' referenced before assignment' in AnchorImage
2 participants