Skip to content

SmABMIL

torchmil.models.SmABMIL

Bases: MILModel

Attention-based Multiple Instance Learning (ABMIL) model with the \(\texttt{Sm}\) operator. Proposed in Sm: enhanced localization in Multiple Instance Learning for medical imaging classification.

Given an input bag \(\mathbf{X} = \left[ \mathbf{x}_1, \ldots, \mathbf{x}_N \right]^\top \in \mathbb{R}^{N \times P}\), the model optionally applies a feature extractor, \(\text{FeatExt}(\cdot)\), to transform the instance features: \(\mathbf{X} = \text{FeatExt}(\mathbf{X}) \in \mathbb{R}^{N \times D}\).

Then, it aggregates the instance features into a bag representation \(\mathbf{z} \in \mathbb{R}^{D}\) using an attention-based pooling mechanism that incorporates the \(\texttt{Sm}\) operator,

\[ \mathbf{z}, \mathbf{f} = \operatorname{SmAttentionPool}(\mathbf{X}), \]

where \(\mathbf{f} \in \mathbb{R}^{N}\) are the attention values. See SmAttentionPool for more details on the pooling operator The bag representation \(\mathbf{z}\) is then fed into a classifier (one linear layer) to predict the bag label.

__init__(in_shape, att_dim=128, att_act='tanh', sm_mode='approx', sm_alpha='trainable', sm_layers=0, sm_steps=10, sm_pre=False, sm_post=False, sm_spectral_norm=False, feat_ext=torch.nn.Identity(), criterion=torch.nn.BCEWithLogitsLoss())

Parameters:

  • in_shape (tuple) –

    Shape of input data expected by the feature extractor (excluding batch dimension).

  • att_dim (int, default: 128 ) –

    Attention dimension.

  • att_act (str, default: 'tanh' ) –

    Activation function for attention. Possible values: 'tanh', 'relu', 'gelu'.

  • sm_mode (str, default: 'approx' ) –

    Mode for the Sm operator. Possible values: 'approx', 'exact'.

  • sm_alpha (Union[float, str], default: 'trainable' ) –

    Alpha value for the Sm operator. If 'trainable', alpha is trainable.

  • sm_layers (int, default: 0 ) –

    Number of layers that use the Sm operator.

  • sm_steps (int, default: 10 ) –

    Number of steps for the Sm operator.

  • sm_pre (bool, default: False ) –

    If True, apply Sm operator before the attention pooling.

  • sm_post (bool, default: False ) –

    If True, apply Sm operator after the attention pooling.

  • sm_spectral_norm (bool, default: False ) –

    If True, apply spectral normalization to all linear layers.

  • feat_ext (Module, default: Identity() ) –

    Feature extractor.

  • criterion (Module, default: BCEWithLogitsLoss() ) –

    Loss function. By default, Binary Cross-Entropy loss from logits for binary classification.

forward(X, adj, mask=None, return_att=False)

Forward pass.

Parameters:

  • X (Tensor) –

    Bag features of shape (batch_size, bag_size, ...).

  • adj (Tensor) –

    Adjacency matrix of shape (batch_size, bag_size, bag_size).

  • mask (Tensor, default: None ) –

    Mask of shape (batch_size, bag_size).

  • return_att (bool, default: False ) –

    If True, returns attention values (before normalization) in addition to Y_pred.

Returns:

  • Y_pred ( Tensor ) –

    Bag label logits of shape (batch_size,).

  • att ( Tensor ) –

    Only returned when return_att=True. Attention values (before normalization) of shape (batch_size, bag_size).

compute_loss(Y, X, adj, mask=None)

Compute loss given true bag labels.

Parameters:

  • Y (Tensor) –

    Bag labels of shape (batch_size,).

  • X (Tensor) –

    Bag features of shape (batch_size, bag_size, ...).

  • adj (Tensor) –

    Adjacency matrix of shape (batch_size, bag_size, bag_size).

  • mask (Tensor, default: None ) –

    Mask of shape (batch_size, bag_size).

Returns:

  • Y_pred ( Tensor ) –

    Bag label logits of shape (batch_size,).

  • loss_dict ( dict ) –

    Dictionary containing the loss value.

predict(X, adj, mask=None, return_inst_pred=True)

Predict bag and (optionally) instance labels.

Parameters:

  • X (Tensor) –

    Bag features of shape (batch_size, bag_size, ...).

  • adj (Tensor) –

    Adjacency matrix of shape (batch_size, bag_size, bag_size).

  • mask (Tensor, default: None ) –

    Mask of shape (batch_size, bag_size).

  • return_inst_pred (bool, default: True ) –

    If True, returns instance labels predictions, in addition to bag label predictions.

Returns:

  • Y_pred ( Tensor ) –

    Bag label logits of shape (batch_size,).

  • y_inst_pred ( Tensor ) –

    If return_inst_pred=True, returns instance labels predictions of shape (batch_size, bag_size).