Skip to content

maskers

BatchwiseChannelwisePatchwisePercentMasker

Bases: PercentMasker

Constructs a mask based on a percentage of the highest values in the input tensor per batch element, per color channel, per patch.

This class is designed to be used with 2D convolutional layers that output tensors of shape (batch_size, color_channels * patch_area, patches_high, patches_wide).

__init__

__init__(percent_to_mask: float | Tensor, color_channels: int, patch_size: int | tuple[int, int] | Sequence[int]) -> None

Construct a BatchwiseChannelwisePatchwisePercentMasker from the given parameters.

Parameters:

Name Type Description Default
percent_to_mask float | Tensor

A scalar value between 0 and 1 inclusive, indicating the percentage of the largest input values to mask.

required
color_channels int

The number of color channels in the input tensor.

required
patch_size int | tuple[int, int] | Sequence[int]

The dimension of the non-overlapping rectangular patches the image is tiled with.

required

forward

forward(input: Tensor, selector_mask: Tensor | None = None) -> torch.Tensor

Construct a mask over the input tensor.

Parameters:

Name Type Description Default
input Tensor

The tensor over which to compute the thresholds.

required
selector_mask Tensor | None

Unsupported. In the parent class, a mask tensor to use to select a subset of the elements of input when computing the threshold. torch.Tensor.masked_select results in a flattened tensor, and because BatchwiseChannelwisePatchwisePercentMasker operates per patch, per color channel, it cannot accept a flattened input.

None

Returns:

Type Description
torch.Tensor

A mask tensor of the same shape as input.

Raises:

Type Description
ValueError

If selector_mask is not None.

BatchwisePercentMasker

Bases: PercentMasker

Constructs a mask based on a percentage of the highest values in the input tensor per batch element.

__init__

__init__(percent_to_mask: float | Tensor) -> None

Construct a layer to mask a percentage of input values.

Parameters:

Name Type Description Default
percent_to_mask float | Tensor

A scalar value between 0 and 1 inclusive, indicating the percentage of the largest input values to mask.

required

Raises:

Type Description
ValueError

If percent_to_mask is not a scalar.

ValueError

If percent_to_mask is not between 0 and 1 inclusive.

forward

forward(input: Tensor, selector_mask: Tensor | None = None) -> torch.Tensor

Construct a mask over the input tensor.

Parameters:

Name Type Description Default
input Tensor

The tensor over which to compute the threshold(s).

required
selector_mask Tensor | None

A mask tensor to use to select a subset of the elements of input when computing the threshold. If None, all elements of input are used.

None

Returns:

Type Description
torch.Tensor

A mask tensor of the same shape as input.

Masker

Bases: Module, ABC

Defines an interface for masking input values.

Maskers should return a boolean tensor of the same shape as the input tensor.

Added in version 0.30.0.

PercentMasker

Bases: ThresholdMasker

Constructs a mask based on a percentage of the highest values in the input tensor.

__init__

__init__(percent_to_mask: float | Tensor) -> None

Construct a layer to mask a percentage of input values.

Parameters:

Name Type Description Default
percent_to_mask float | Tensor

A scalar value between 0 and 1 inclusive, indicating the percentage of the largest input values to mask.

required

Raises:

Type Description
ValueError

If percent_to_mask is not a scalar.

ValueError

If percent_to_mask is not between 0 and 1 inclusive.

forward

forward(input: Tensor, selector_mask: Tensor | None = None) -> torch.Tensor

Construct a mask over the input tensor.

Parameters:

Name Type Description Default
input Tensor

The tensor over which to compute the threshold(s).

required
selector_mask Tensor | None

A mask tensor to use to select a subset of the elements of input when computing the threshold. If None, all elements of input are used.

None

Returns:

Type Description
torch.Tensor

A mask tensor of the same shape as input.

ThresholdMasker

Bases: Masker

Defines an interface for constructing a mask from a threshold computed over an input tensor.

forward

forward(input: Tensor, selector_mask: Tensor | None = None) -> torch.Tensor

Construct a mask over the input tensor.

Parameters:

Name Type Description Default
input Tensor

The tensor over which to compute the threshold(s).

required
selector_mask Tensor | None

A mask tensor to use to select a subset of the elements of input when computing the threshold. If None, all elements of input are used.

None

Returns:

Type Description
torch.Tensor

A mask tensor of the same shape as input.