attribench.masking.image.ImageMasker
- class attribench.masking.image.ImageMasker(masking_level)[source]
Bases:
MaskerAbstract base class for all image maskers.
Image maskers are specific maskers for image data. They can be used to mask features on the feature level or the pixel level. If the masker is used on the feature level, the attributions must have the same shape as the samples. If the masker is used on the pixel level, the attributions must have the same shape as the samples, except the channel dimension must be 1.
If the masker is used on the feature level, masking a feature means masking a specific input feature for all samples (i.e. one color value for one pixel). If the masker is used on the pixel level, masking a feature means masking a specific pixel for all samples (i.e. all color values for one pixel).
If segmented samples are provided, the masker will use the segments to mask features. This means that masking a feature will mask all pixels belonging to the same segment. The attribution value of a segment is defined as the average attribution value of all features in that segment.
- Parameters:
- masking_levelstr
Either
"feature"or"pixel". If"feature", the masker will mask features. If"pixel", the masker will mask pixels.
- Raises:
- ValueError
If
masking_levelis not"feature"or"pixel".
Methods
Return the number of features in the samples.
Mask the
kleast important features, according to the attributions.Mask
krandom features.Mask the
kmost important features, according to the attributions.Set the batch of samples and attributions to use for masking.
- get_num_features()[source]
Return the number of features in the samples.
- Returns:
- int
Number of features in the samples.
- mask_bot(k)[source]
Mask the
kleast 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)[source]
Mask
krandom 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)[source]
Mask the
kmost 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)[source]
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()andmask_bot()methods will not be available.- segmented_samplestorch.Tensor, optional
Segmented samples to use for masking, by default None