attribench.masking.image.RandomImageMasker

class attribench.masking.image.RandomImageMasker(masking_level, std=1)[source]

Bases: ImageMasker

Image masker that masks images with normally distributed random noise, with a given standard deviation.

Parameters:
masking_levelstr

The level at which to mask the image. Must be either "pixel" or "feature".

stdfloat

The standard deviation of the random noise to add to the image. Defaults to 1.

Methods

get_num_features

Return the number of features in the samples.

mask_bot

Mask the k least important features, according to the attributions.

mask_rand

Mask k random features.

mask_top

Mask the k most important features, according to the attributions.

set_batch

Set the batch of samples and attributions to use for masking.

get_num_features()

Return the number of features in the samples.

Returns:
int

Number of features in the samples.

mask_bot(k)

Mask the k least important features, according to the attributions.

Parameters:
kint

Number of features to mask.

Returns:
torch.Tensor

Samples with the bottom k features masked.

mask_rand(k, return_indices=False)

Mask k random features.

Parameters:
kint

Number of features to mask.

return_indicesbool, optional

Whether to return the indices of the masked features, by default False

Returns:
torch.Tensor

Samples with k random features masked.

Return type:

Union[Tensor, Tuple[Tensor, Tensor]]

mask_top(k)

Mask the k most important features, according to the attributions.

Parameters:
kint

Number of features to mask.

Returns:
torch.Tensor

Samples with the top k features masked.

set_batch(samples, attributions=None, segmented_samples=None)

Set the batch of samples and attributions to use for masking. Optionally also set the segmented samples.

Parameters:
samplestorch.Tensor

Samples to use for masking.

attributionstorch.Tensor, optional

Attributions to use for masking, by default None If None, the mask_top() and mask_bot() methods will not be available.

segmented_samplestorch.Tensor, optional

Segmented samples to use for masking, by default None