Skip to content
class

AttentionUNetForSemanticSegmentation

extendsSemanticSegmentationModel
AttentionUNetForSemanticSegmentation(config: AttentionUNetConfig)

Attention U-Net for medical image segmentation (Oktay et al., MIDL 2018).

A U-Net variant that inserts a soft attention gate on every skip connection. Each gate fuses the encoder feature xℓx^\ell (the "input" branch) with the up-sampled decoder feature gℓg^\ell (the "gating signal") via

αℓ=σ ⁣(ψ⊤ReLU(Wxxℓ+Wggℓ)),x^ℓ=αℓ⊙xℓ,\alpha^\ell = \sigma\!\bigl(\psi^\top \mathrm{ReLU}(W_x x^\ell + W_g g^\ell)\bigr), \qquad \hat{x}^\ell = \alpha^\ell \odot x^\ell,

suppressing skip-feature activations outside the regions of interest highlighted by the decoder. This focuses the decoder's attention on relevant anatomy and removes the "noise" carried over from encoder layers — a consistent +1-3 Dice gain on medical-imaging benchmarks in the original paper.

Parameters

Frozen architecture spec. Use attention_unet for the standard 4-level configuration.

Attributes

Stored copy of the config that built this model.
encoderslist[_EncoderBlock]
config.depth 2-D encoder stages (DoubleConv + MaxPool).
bottleneck_DoubleConv
DoubleConv at the bottom of the U.
decoderslist[_DecoderBlock]
config.depth decoder stages, each applying an attention gate to the corresponding skip feature before concatenating it into the decoder DoubleConv.
1x1 convolution producing num_classes channels.

Notes

See Oktay et al., "Attention U-Net: Learning Where to Look for the Pancreas", MIDL 2018 (arXiv:1804.03999). The additive attention gate is identical to that of Bahdanau et al. (2015) generalised to feature maps:

qℓ=ψ⊤σ1 ⁣(Wxxℓ+Wggℓ+bg),αℓ=σ2(qℓ+bψ),q^\ell = \psi^\top \sigma_1\!\bigl(W_x x^\ell + W_g g^\ell + b_g\bigr), \quad \alpha^\ell = \sigma_2(q^\ell + b_\psi),

with σ1=ReLU\sigma_1 = \mathrm{ReLU}, σ2=sigmoid\sigma_2 = \mathrm{sigmoid}. The intermediate channel count is typically half the input channels.

Examples

>>> import lucid
>>> from lucid.models.vision.attention_unet import attention_unet
>>> model = attention_unet()
>>> x = lucid.randn(1, 1, 256, 256)
>>> out = model(x)
>>> out.logits.shape   # (B, num_classes, H, W)
(1, 2, 256, 256)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: AttentionUNetConfig)

Instance methods

1
forward(x: Tensor, targets: Tensor | None = None)

Run Attention U-Net.

Parameters

(B, C, H, W) images, or (B, C, D, H, W) volumes when the model was built with spatial_dims=3.
targetsTensor | None= None
Optional integer ground-truth masks with the same spatial extent as x.

Returns

SemanticSegmentationOutput

SemanticSegmentationOutput with logits shaped like x in