Add baseline_replacement_by_indices_excluding: a placement-controlled perturbation - #380
Open
SubhagSharma wants to merge 1 commit into
Conversation
… perturbation Every perturbation function in Quantus answers what value to substitute or what transform to apply. None constructs a region subject to a placement constraint. That matters when a random perturbation is used as a control for the masking artefact rather than as a random-importance baseline. A control drawn uniformly from the whole frame removes, in expectation, a fraction gamma of the object it is meant to be a control for, where gamma is the object's frame coverage. At high coverage this is enough to reverse the sign of the measured object reliance. baseline_replacement_by_indices_excluding takes the same arguments as baseline_replacement_by_indices plus an optional exclude_mask and seed. It draws the same number of indices, still scattered, only from outside the mask. It raises ValueError when the region cannot be placed: gamma * W * H distinct positions cannot be drawn from the (1 - gamma) * W * H outside the object, so the control exists only while gamma <= 0.5. Falling back to an unconstrained draw would return a number that looks like a measurement and is not one. With exclude_mask=None it is bit-identical to baseline_replacement_by_indices, so it is a drop-in and cannot change existing results. Tests cover the feasibility bound, non-overlap across seeds, drop-in equivalence when no mask is given, and seeded reproducibility. Discussed in understandable-machine-intelligence-lab#379.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
baseline_replacement_by_indices_excluding— see #379 for the motivation.Same signature as
baseline_replacement_by_indices, plus an optionalexclude_maskand
seed. It draws the same number of indices, still scattered, but only fromoutside the mask. With
exclude_mask=Noneit's identical to the stock function, soit can't affect existing results.
It raises
ValueErrorwhen the region won't fit — if the excluded area is more thanhalf the frame there aren't enough positions left. I made it raise rather than fall
back to an unconstrained draw, because a silent fallback gives you a number that
looks fine and isn't.
Four tests: the raise/no-raise boundary, non-overlap across seeds, equivalence with
the stock function when no mask is given, and seeded reproducibility.
A few notes:
blackformatted,flake8clean. TheF405s in the test file come from theexisting
import *and hit every test in there.blackoff the surrounding code so the diff stays readable.toxlocally (tests/conftest.pyimports keras, which I don'thave), so the four tests were run directly against the function in the package.
CI will be the real check.
ValueErrorrather than a new exception class, to keep this to one function.Happy to move the placement logic into a
utils.pyhelper instead if you'd preferthat split.