RISEGrid
- class xaitk_saliency.impls.perturb_image.rise.RISEGrid(n: int, s: int, p1: float, seed: int | None = None, threads: int | None = 4)
Based on Petsiuk et. al: http://bmvc2018.org/contents/papers/1064.pdf
Implementation is borrowed from the original authors: https://github.com/eclique/RISE/blob/master/explanations.py
Methods
from_config
Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments.
Return a JSON-compliant dictionary that could be passed to this class's
from_config
method to produce an instance with identical configuration.get_default_config
Generate and return a default configuration dictionary for this class.
get_impls
Discover and return a set of classes that implement the calling class.
is_usable
Check whether this class is available for use.
Transform an input reference image into a number of mask matrices indicating the perturbed regions.
- __init__(n: int, s: int, p1: float, seed: int | None = None, threads: int | None = 4)
Generate a set of random binary masks
- Parameters:
n – Number of random masks used in the algorithm. E.g. 1000.
s – Spatial resolution of the small masking grid. E.g. 8. Assumes square grid.
p1 – Probability of the grid cell being set to 1 (otherwise 0). This should be a float value in the [0, 1] range. E.g. 0.5.
seed – A seed to pass into the constructed random number generator to allow for reproducibility
threads – The number of threads to utilize when generating masks. If this is <=0 or None, no threading is used and processing is performed in-line serially.
- get_config() Dict[str, Any]
Return a JSON-compliant dictionary that could be passed to this class’s
from_config
method to produce an instance with identical configuration.In the most cases, this involves naming the keys of the dictionary based on the initialization argument names as if it were to be passed to the constructor via dictionary expansion. In some cases, where it doesn’t make sense to store some object constructor parameters are expected to be supplied at as configuration values (i.e. must be supplied at runtime), this method’s returned dictionary may leave those parameters out. In such cases, the object’s
from_config
class-method would also take additional positional arguments to fill in for the parameters that this returned configuration lacks.- Returns:
JSON type compliant configuration dictionary.
- Return type:
dict
- perturb(ref_image: ndarray) ndarray
Transform an input reference image into a number of mask matrices indicating the perturbed regions.
Output mask matrix should be three-dimensional with the format [nMasks x Height x Width], sharing the same height and width to the input reference image. The implementing algorithm may determine the quantity of output masks per input image. These masks should indicate the regions in the corresponding perturbed image that have been modified. Values should be in the [0, 1] range, where a value closer to 1.0 indicates areas of the image that are unperturbed. Note that output mask matrices may be of a floating-point type to allow for fractional perturbation.
- Parameters:
ref_image – Reference image to generate perturbations from.
- Returns:
Mask matrix with shape [nMasks x Height x Width].