MCRISEGrid
- class xaitk_saliency.impls.perturb_image.mc_rise.MCRISEGrid(n: int, s: int, p1: float, k: int, seed: int | None = None, threads: int | None = 4)
Based on Hatakeyama et. al: https://openaccess.thecvf.com/content/ACCV2020/papers/Hatakeyama_Visualizing_Color-wise_Saliency_of_Black-Box_Image_Classification_Models_ACCV_2020_paper.pdf
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.
Warning: this implementation returns a different shape than typically expected by this interface.
- __init__(n: int, s: int, p1: float, k: int, seed: int | None = None, threads: int | None = 4) None
Generate a set of random binary masks
- Parameters:
n – int Number of random masks used in the algorithm. E.g. 1000.
s – int Spatial resolution of the small masking grid. E.g. 8. Assumes square grid.
p1 – float 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.
k – int Number of colors to use.
seed – Optional[int] A seed to pass into the constructed random number generator to allow for reproducibility
threads – int 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.
- Raises:
ValueError If p1 not in [0, 1].
- Raises:
ValueError If k < 1.
- 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
Warning: this implementation returns a different shape than typically expected by this interface. Instead of [nMasks x Height x Width], masks of shape [kColors x nMasks x Height x Width] are returned.
- Parameters:
ref_image – np.ndarray Reference image to generate perturbations from.
- Returns:
np.ndarray Mask matrix with shape [kColors x nMasks x Height x Width].