attribench.masking.image.ConstantImageMasker

class attribench.masking.image.ConstantImageMasker(masking_level, mask_value=0.0)[source]

Bases: ImageMasker

Image masker that masks pixels or features by replacing them with a given constant value.

Parameters:
feature_levelstr

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

mask_valuefloat

The value to use for masking. Defaults to 0.0.

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