mr2.nn.attention.ShiftedWindowAttention

class mr2.nn.attention.ShiftedWindowAttention[source]

Bases: Module

Shifted Window Attention.

(Shifted) Window Attention calculates attention over windows of the input. It was introduced in Swin Transformer [SWIN] and is used in Uformer.

References

[SWIN]

Liu, Ze, et al. “Swin transformer: Hierarchical vision transformer using shifted windows.” ICCV 2021.

__init__(n_dim: int, n_channels_in: int, n_channels_out: int, n_heads: int, window_size: int = 7, shifted: bool = True, features_last: bool = False)[source]

Initialize the ShiftedWindowAttention module.

Parameters:
  • n_dim (int) – The dimension of the input.

  • n_channels_in (int) – The number of channels in the input tensor.

  • n_channels_out (int) – The number of channels in the output tensor.

  • n_heads (int) – The number of attention heads. The number if channels per head is channels // n_heads.

  • window_size (int, default: 7) – The size of the window.

  • shifted (bool, default: True) – Whether to shift the window.

  • features_last (bool, default: False) – Whether the features are last in the input tensor or in the second dimension.

__call__(x: Tensor) → Tensor[source]

Apply the ShiftedWindowAttention.

Parameters:

x (Tensor) – The input tensor.

Returns:

The output tensor.

forward(x: Tensor) → Tensor[source]

Apply the ShiftedWindowAttention.