---
title: 'MAD-MIL: Multi-head Attention MIL'
url: https://www.emergentmind.com/topics/multi-head-attention-mil-mad-mil
type: topic
---

# MAD-MIL: Multi-head Attention MIL

Multi-head Attention MIL (MAD-MIL) is a multiple instance learning (MIL) framework designed for weakly supervised classification tasks in digital pathology, particularly for whole slide images (WSIs). MAD-MIL generalizes the single-head gated attention mechanism of Attention-based Deep MIL (ABMIL) to a multi-head formulation inspired by Transformer architectures. The model emphasizes efficient, interpretable, and accurate aggregation of information from large sets of image patches, reducing computational footprint and increasing representational diversity relative to prior state-of-the-art MIL approaches [2404.05362].

## 1. Model Architecture and Computational Flow

MAD-MIL replaces the single gated-attention module of ABMIL with an M-headed attention block, introducing architectural parallels to Transformer-style multi-head attention. The model comprises four primary components:

1. **Instance Feature Extraction**: The WSI is partitioned into $N$ tiles $\{p_1, \ldots, p_N\}$. Each tile $p_i$ is processed by a pretrained CNN (e.g., ResNet-50) to yield high-dimensional features $h_i \in \mathbb{R}^{D_0}$, which are compressed via a learnable fully connected (FC) layer to $f_i \in \mathbb{R}^D$.
2. **Multi-head Attention Module**: The feature vector $f_i$ is split evenly into $M$ sub-vectors along the feature dimension: $f_i = [f_{i,1}; f_{i,2}; \ldots; f_{i,M}]$, with $f_{i,m} \in \mathbb{R}^{D/M}$. Each sub-vector is processed by a distinct gated attention head, yielding per-head attention weights $a_{i,m}$ and aggregated vectors $z_m$.
3. **Aggregation Layer (Bag-level Embedding)**: For each head $m$, the representation is aggregated as $z_m = \sum_{i=1}^N a_{i,m} f_{i,m}$, where $a_{i,m} \geq 0$ and $\sum_{i} a_{i,m} = 1$. The outputs from $M$ heads are concatenated to form the slide-level embedding $Z = [z_1; z_2; \ldots; z_M] \in \mathbb{R}^D$.
4. **Classifier**: A final FC layer $L$ computes predictions $\hat{y} = L(Z) \in \mathbb{R}^C$, using sigmoid activation for binary and softmax for multiclass tasks.

## 2. Multi-head Attention Mechanisms

MAD-MIL supports two conceptualizations for multi-head aggregation: its practical implementation and a Transformer-style formulation.

**A. Transformer-style Multi-head (for context):**
- Each embedding $f_i$ is used to compute queries ($Q_h$), keys ($K_h$), and values ($V_h$) via linear projections.
- For each head $h$:

    $$
    \begin{aligned}
    Q_h &= X W^Q_h \in \mathbb{R}^{N \times d_k} \\
    K_h &= X W^K_h \in \mathbb{R}^{N \times d_k} \\
    V_h &= X W^V_h \in \mathbb{R}^{N \times d_v}
    \end{aligned}
    $$

- Dot-product attention weights and per-head outputs are computed, concatenated, and pooled for the final bag embedding.

**B. Gated Multi-head Attention (MAD-MIL implementation):**
- Each split feature $f_{i,m}$ is processed by a gated attention module:

    $$
    a_{i,m} =
    \frac{
        \exp\left(w_m^{T} \left[\tanh(V_m f_{i,m}^T) \circ \sigma(U_m f_{i,m}^T)\right]\right)
    }{
        \sum_{j=1}^N \exp\left(w_m^{T} \left[\tanh(V_m f_{j,m}^T) \circ \sigma(U_m f_{j,m}^T)\right]\right)
    }
    $$

    where $U_m, V_m \in \mathbb{R}^{L \times (D/M)}$, $w_m \in \mathbb{R}^L$, and $\sigma$ is the sigmoid function.

- Per-head bag embedding: $z_m = \sum_{i=1}^N a_{i,m} \, f_{i,m}$.

## 3. Model Complexity and Efficiency

MAD-MIL is designed to reduce both trainable parameters and floating point operations per bag relative to existing deep MIL architectures such as ABMIL and DS-MIL, without loss of accuracy. Parameter and computational requirements across representative tasks are summarized below.

| Dataset      | Method         | Params    | FLOPs   |
|--------------|---------------|-----------|---------|
| MNIST-BAGS   | ABMIL         | 167.1 K   | 19.9 M  |
|              | MAD-MIL/6     | 107.1 K   | 12.7 M  |
| TUPAC16      | ABMIL         | 788.7 K   | 94.4 M  |
|              | MAD-MIL/3     | 614.8 K   | 73.5 M  |
|              | DS-MIL        | 1.186 M   | 142.0 M |
| TCGA BRCA    | ABMIL         | 788.7 K   | 94.4 M  |
|              | MAD-MIL/2     | 657.6 K   | 78.6 M  |
| TCGA LUNG    | MAD-MIL/8     | 559.3 K   | 66.8 M  |
| TCGA KIDNEY  | MAD-MIL/5     | 582.7 K   | 69.6 M  |

Across datasets, MAD-MIL achieves $20-30\%$ reductions in trainable parameters and FLOPs relative to ABMIL, and over $40\%$ reduction versus DS-MIL [2404.05362].

## 4. Experimental Protocol and Evaluation

Empirical validation covered both synthetic (MNIST-BAGS) and real-world WSI datasets:

- **Datasets**:
    - MNIST-BAGS: 20-instance bags, binary classification of digit ‘8’ under controlled positive/negative instance ratios.
    - TUPAC16: 821 WSIs (H&E), binary proliferation grading.
    - TCGA BRCA: 1,038 slides, subtype classification (IDC vs ILC).
    - TCGA LUNG: 1,046 slides, LUAD vs LUSC.
    - TCGA KIDNEY: 918 slides, three-class subtyping.
- **Feature Extraction**:
    - MNIST: Flatten $28 \times 28$ images, project to $D=128$.
    - WSIs: Patch extraction ($256 \times 256$ at $20\times$), ResNet-50 to 1,024-d, then FC to $D=512$.
- **Training**:
    - Adam optimizer.
    - Task-specific epochs (MNIST: 20; TUPAC16/TCGA: 50).
    - Hyperparameters: Validation-based selection, 10-fold cross-validation (TCGA).
    - Head count $M$ optimized via validation loss.
- **Performance Metrics**: AUC (ROC), F1-score (binary), macro-F1 (multi-class).

## 5. Comparative Performance Analysis

Experimental results demonstrate that MAD-MIL consistently surpasses ABMIL, and in most cases matches or narrowly trails the highest-performing, but more complex, methods such as DS-MIL and ACMIL.

| Dataset/Task   | Method          | AUC           | F1         |
|----------------|-----------------|---------------|------------|
| MNIST-BAGS     | ABMIL           | $0.800 \pm 0.057$ | $0.668 \pm 0.104$ |
|                | MAD-MIL/7       | $0.835 \pm 0.026$ | $0.753 \pm 0.028$ |
| TUPAC16        | ABMIL           | $0.79 \pm 0.013$  | $0.725 \pm 0.013$ |
|                | MAD-MIL/3       | $0.802 \pm 0.006$ | $0.735 \pm 0.009$ |
|                | CLAM-MB         | $0.803 \pm 0.008$ | $0.725 \pm 0.010$ |
| TCGA BRCA      | ABMIL           | $0.882 \pm 0.046$ | $0.783 \pm 0.061$ |
|                | MAD-MIL/2       | $0.897 \pm 0.058$ | $0.791 \pm 0.064$ |
|                | DS-MIL          | $0.903 \pm 0.053$ | $0.788 \pm 0.056$ |
| TCGA LUNG      | ABMIL           | $0.931 \pm 0.020$ | $0.853 \pm 0.034$ |
|                | MAD-MIL/8       | $0.940 \pm 0.015$ | $0.872 \pm 0.027$ |
| TCGA KIDNEY    | ABMIL           | $0.983 \pm 0.010$ | $0.894 \pm 0.037$ |
|                | MAD-MIL/5       | $0.985 \pm 0.007$ | $0.898 \pm 0.034$ |
|                | DS-MIL          | $0.983 \pm 0.009$ | $0.908 \pm 0.037$ |

A consistent AUC and F1-score improvement is observed over ABMIL, with competitive ranking alongside other transformer-inspired methods, but at a lower computational and parameter budget [2404.05362].

## 6. Interpretability Features

MAD-MIL generates per-head attention heatmaps, enhancing transparency of slide-level predictions:

- Each attention head $m$ produces an attention score map $\{a_{i,m}\}$, which can be spatially registered to patch locations.
- Heatmaps derived from these scores can be up-scaled and superimposed on original WSIs.
- Empirical visualization (e.g., on LUAD slides) shows that MAD-MIL’s eight attention heads yield complementary highlight regions: tumor, stroma, necrosis, and lymphocyte infiltration.
- *A plausible implication is that* the diversity of M-heads offers greater opportunity for fine-grained, multi-faceted clinical interpretability and pathologist trust, compared to single-head models or those producing only a single map.

## 7. Implementation and Prospective Extensions

The published implementation includes modular code (PyTorch) with data preprocessing, model modules, and visualization tools (heatmap overlay) [GitHub: https://github.com/tueimage/MAD-MIL]:

- Feature extraction and tiling can be decoupled (offline), enabling low-latency batch inference.
- Moderate memory footprint due to reduced multilayer perceptron (MLP) sizes.
- Multi-head outputs support integration into graphical user interfaces for interactive slide review.
- Potential extensions identified in the original source include replacement of gated attention with dot-product multi-head attention, self-supervised pretraining of the feature encoder, and algorithmic head pruning or regularization to maximize information diversity for a given model size [2404.05362].

Source: https://www.emergentmind.com/topics/multi-head-attention-mil-mad-mil