EncoderMixin¶

class EncoderMixin[source]¶

Mixin class adding encoder-specific functionality.

Provides methods for: - Managing output channels for encoder feature maps. - Patching the first convolution for arbitrary input channels. - Converting encoder to dilated version for segmentation tasks.

_depth¶

Encoder depth (number of stages).

Type:

int

_in_channels¶

Number of input channels.

Type:

int

_output_stride¶

Output stride of the encoder.

Type:

int

_out_channels¶

List of output channel dimensions for each depth level.

Type:

list[int]

Example

>>> encoder = EncoderMixin()
>>> encoder.set_in_channels(1)

Initialize EncoderMixin with default parameters.

Sets default values for encoder depth, input channels, output stride, and output channel list.

Methods

get_stages

Return encoder stages for dilation modification.

make_dilated

Convert encoder to a dilated version for segmentation.

set_in_channels

Update the encoder to accept a different number of input channels.

Attributes

out_channels

Return output channel dimensions for encoder feature maps.

output_stride

Return the effective output stride of the encoder.

get_stages()[source]¶

Return encoder stages for dilation modification.

This method should be overridden by subclasses to provide stage mappings for converting strides to dilations.

Returns:

Dictionary mapping output stride to corresponding module sequences.

Return type:

dict[int, Sequence[torch.nn.Module]]

Raises:

NotImplementedError – If the method is not implemented by the subclass.

Example

>>> stages = encoder.get_stages()
make_dilated(output_stride)[source]¶

Convert encoder to a dilated version for segmentation.

Parameters:

output_stride (int) – Target output stride (must be 8 or 16).

Raises:

ValueError – If output_stride is not 8 or 16.

Return type:

None

Example

>>> encoder.make_dilated(output_stride=16)
property out_channels: list[int]¶

Return output channel dimensions for encoder feature maps.

Returns:

List of output channel dimensions for each depth level.

Return type:

list[int]

Example

>>> encoder.out_channels
... [3, 32, 64, 128, 256, 512]
property output_stride: int¶

Return the effective output stride of the encoder.

The output stride is the minimum of the configured stride and 2^depth.

Returns:

Effective output stride.

Return type:

int

Example

>>> encoder.output_stride
... 32
set_in_channels(in_channels, *, pretrained=True)[source]¶

Update the encoder to accept a different number of input channels.

Parameters:
  • in_channels (int) – Number of input channels.

  • pretrained (bool) – Whether to use pretrained weights. Defaults to True.

Return type:

None

Example

>>> encoder.set_in_channels(1, pretrained=False)