RandomGrid
- class xaitk_saliency.impls.perturb_image.random_grid.RandomGrid(n: int, s: Tuple[int, int], p1: float, seed: int | None = None, threads: int | None = None)
Generate masks using a random grid of set cell size. If the chosen cell size does not divide an image evenly, then the grid is over-sized and the resulting mask is centered and cropped. Each mask is also shifted randomly by a maximum of half the cell size in both x and y.
This method is based on RISE (http://bmvc2018.org/contents/papers/1064.pdf) but aims to address the changing cell size, given images of different sizes, aspect of that implementation. This method keeps cell size constant and instead adjusts the overall grid size for different sized images.
- Parameters:
n – Number of masks to generate.
s – Dimensions of the grid cells in pixels. E.g. (3, 4) would use a grid of 3x4 pixel cells.
p1 – Probability of a grid cell being set to 1 (not occluded). This should be a float value in the [0, 1] range.
seed – A seed to use for the random number generator, allowing for masks to be reproduced.
threads – Number of threads to use when generating masks. If this is <=0 or None, no threading is used and processing is performed in-line serially.
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: Tuple[int, int], p1: float, seed: int | None = None, threads: int | None = None)
- 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].