Attention, transformers and pretrained models

Image analysis

About this lecture

A 3 × 3 kernel only sees its neighbours. How can a network relate a pixel to the whole image, and where do networks come from that nobody trained on my images?

  • Self-attention: a learned, content-dependent weighted average over all patches; next to a convolution’s fixed, position-only weights. Transformers: what they gain and what they pay.
  • Transfer learning: frozen features + a small classifier, or fine-tuning; self-supervised pretraining; promptable segmentation.
  • Calibration: is a network that says 0.95 right 95 % of the time?
  • Practical judgement: data, domain shift, and when a classical method is better.

Slides titled The math give the formulas and proofs; the ideas are complete without them.

Recall: what a convolution sees

  • A 3 \times 3 kernel combines a pixel with its 8 neighbours. A stack of L such layers sees (2L + 1) \times (2L + 1) pixels (the receptive field, the neural networks day): 3 layers see 7 \times 7.
  • Seeing a structure 200 pixels long takes about 100 layers, or pooling that throws away resolution.
  • The kernel weights are the same at every position and do not depend on the content: a filament pixel and a background pixel are combined with their neighbours in the same way.

A pixel grid with one black output pixel and the input pixels it depends on: 3 × 3 after one 3 × 3 convolution layer, 5 × 5 after two, 7 × 7 after three.

Patches as tokens

  • Cut the image into patches, e.g. 16 \times 16 pixels; a 512 \times 512 image gives 32 \times 32 = 1024 patches.
  • Flatten each patch into a vector, map it linearly to an embedding (a vector of, say, 384 numbers).
  • Add a position code: otherwise the network cannot tell where a patch came from.
  • The image is now a set of N vectors (“tokens”). Everything that follows works on such a set.

A 256 × 256 micrograph cut into 8 × 8 = 64 patches of 32 × 32 pixels. Four patches (orange) are shown with their embedding vectors (colour bars) and position codes (0,0), (0,1), (3,4), (7,7).

Self-attention: a learned weighted average

Each patch i has three vectors computed from its embedding: a query (“what am I looking for?”), a key (“what do I offer?”) and a value (“my content”).

  • Weight of patch j for patch i: how well i’s query matches j’s key (a dot product), turned into positive weights that sum to 1 (softmax).
  • Output for patch i: the weighted average of all values.

By hand, three tokens: query (1, 0); keys (1, 0), (0, 1), (1, 1); values 10, 0, 4.

  • Scores (dot products): 1, 0, 1. Weights: e^1, e^0, e^1 normalised: 0.42, 0.16, 0.42.
  • Output: 0.42 \cdot 10 + 0.16 \cdot 0 + 0.42 \cdot 4 = 5.9.
  • With temperature \tfrac12 (scores doubled): weights 0.47, 0.06, 0.47, output 6.6: sharper.

The three-token example as bar charts of the attention weights: at temperature 1 the keys (1,0), (0,1), (1,1) with values 10, 0, 4 get weights 0.42, 0.16, 0.42 and the output is 5.9; at temperature 0.5 the weights are 0.47, 0.06, 0.47 and the output is 6.6.

Self-attention as pseudocode

for every token i:
    for every token j:
        score_j = (query_i · key_j) / T
    weight_j = exp(score_j) / Σ_l exp(score_l)       # positive, sum 1
    output_i = Σ_j weight_j · value_j
  • N tokens: N^2 scores per layer. A convolution uses fixed weights for a few neighbours instead.
  • Everything after the scores is a weighted average: the output never leaves the range of the values.

The math: scaled dot-product attention

Tokens as rows of X \in \mathbb R^{N \times d_x}; learned matrices W_Q, W_K \in \mathbb R^{d_x \times d}, W_V \in \mathbb R^{d_x \times d_v}:

Q = XW_Q,\quad K = XW_K,\quad V = XW_V,\qquad \mathrm{Attn}(X) = \mathrm{softmax}\!\Big(\frac{QK^\top}{\sqrt d}\Big)V,

softmax along each row: a_{ij} = e^{s_{ij}}/\sum_l e^{s_{il}}, s_{ij} = \mathbf q_i^\top\mathbf k_j/\sqrt d.

  • Why \sqrt d: for independent entries with mean 0 and variance 1, \mathrm{Var}(\mathbf q^\top\mathbf k) = \sum_{m=1}^d \mathrm{Var}(q_m k_m) = d. Dividing by \sqrt d keeps the scores at variance 1, so the softmax does not saturate (all weight on one token) as d grows.
  • A temperature T: \mathrm{softmax}(s/T); T \to 0 picks the best match, T \to \infty averages uniformly.
  • Cost: QK^\top has N^2 entries, each a d-dimensional dot product: O(N^2 d) per layer, against O(N k^2 d) for a k \times k convolution on N positions.

Convolution and attention side by side

Convolution Attention
weights depend on relative position only the content (query–key match)
reach per layer k \times k neighbourhood all N tokens
same everywhere? yes (translation equivariant) the rule is; the weights are not
cost per layer \propto N \propto N^2
  • Attention whose scores depend only on the relative position j - i is a convolution (math slide).
  • Attention with fixed queries and keys = the patch itself, similarity by distance, is non-local means (introduced with the deconvolution day’s denoisers, in depth on the edge-preserving smoothing day): average the patches that look like mine, wherever they are.

The attention demo with content-only weights: the query patch (white square) on a diagonal filament puts its weight (orange) on the other filament patches anywhere in the image, far outside the 3 × 3 footprint of a convolution (blue dashed). Right: the image after one attention layer.

The math: equivariance, convolution, non-local means

  • Permutation equivariance. For a permutation matrix P: Q' = PQ, K' = PK, V' = PV, so Q'K'^\top = P\,QK^\top P^\top; a row-wise softmax commutes with permuting rows and columns: \mathrm{softmax}(PSP^\top) = P\,\mathrm{softmax}(S)P^\top. Hence \mathrm{Attn}(PX) = P\,\mathrm{softmax}(S)P^\top PV = P\,\mathrm{Attn}(X). Shuffling the patches only shuffles the output: without position codes, the network cannot know the arrangement.
  • Relative position. If s_{ij} = b(j - i) (a learned function of the offset, e.g. -\|j - i\|^2/\rho^2), then a_{ij} = w(j - i) is the same at every i (away from the border), and \mathrm{Attn} = \sum_j w(j - i)\mathbf v_j: a convolution with kernel w.
  • A squared distance is a dot product of extended vectors: -\|\mathbf x_i - \mathbf x_j\|^2 = \tilde{\mathbf q}_i^\top\tilde{\mathbf k}_j with \tilde{\mathbf q}_i = (2\mathbf x_i, -\|\mathbf x_i\|^2, 1), \tilde{\mathbf k}_j = (\mathbf x_j, 1, -\|\mathbf x_j\|^2). The demo uses such scores.
  • Non-local means: \hat I(i) = \sum_j w_{ij} I(j), w_{ij} \propto \exp(-\|P_i - P_j\|^2/h^2) for patches P_i: attention with \mathbf q_i = \mathbf k_i = P_i (as extended vectors), temperature h^2, values = pixels.

Try it: attention on patches

Transformers

  • A transformer block = attention + a small network applied to every token separately, each wrapped in a residual connection, with normalisation (the normalisation unit). Blocks are stacked (12, 24, …).
  • Several attention heads in parallel: several different weightings (one may look at nearby patches, another at similar texture).
  • Gains: every layer is global: a pixel can relate to the whole image in one step; the weighting adapts to the content.
  • Pays: N^2 weights per layer and head (64 \times 64 patches of a 1024^2 image: 16.8 million); no built-in locality or translation equivariance, so these must be learned from data: transformers need much more data or pretraining. Hybrids (convolutions early, attention late) are common.

A transformer block: N tokens, normalise, attention with several heads, add the skip connection; normalise, a small network applied to each token, add the skip connection; N tokens out. Blocks are stacked 12, 24 or more times.

Transfer learning

Early layers of any image network learn edges, blobs and textures (the neural networks day). Reuse a network trained on a large dataset:

  • Frozen features: run the pretrained network, take its internal vectors (embeddings) as features, and train only a small classifier on top (a linear probe: logistic regression). Few labels, fast, cannot overfit much.
  • Fine-tuning: continue training all or the last layers on your data. Better with enough labels; with few it overfits and can forget what it knew.
  • The random-forest day’s loop (paint, train, look) works unchanged on frozen embeddings: the features are learned instead of the filter stack.

Two ways to reuse a pretrained network. Frozen features: image, pretrained network (not trained), embedding, a linear probe, nucleus or not. Fine-tuning: the same chain, but the network is trained further together with a new head.

Self-supervised pretraining

Learn features without labels, from a task the images themselves define:

  • Contrastive: two augmented views of the same crop (rotated, blurred, brightened, noisy) should have similar embeddings; views of different crops dissimilar. The network learns what stays the same under the augmentations, so the augmentations decide what it ignores: augment colour and it can no longer tell stains apart.
  • Masked autoencoding: hide 75 % of the patches, predict them from the rest. To fill them in, the network must learn what cells look like.

A foundation model: a very large network pretrained like this on millions of images, used through frozen features or light fine-tuning. In depth: the day learning without labels (masked modelling, contrastive learning, self-distillation).

Self-supervised tasks on crops of the micrograph. Contrastive: view 1 (rotated, blurred) and view 2 (brighter, noisy) of the same crop should get close embeddings; another crop should get a distant one. Masked autoencoding: 75 % of the 16 × 16 patches hidden (orange); the network predicts them from the rest.

The math: contrastive loss and linear probe

Batch of B crops; embeddings of the two views \mathbf z_i, \mathbf z_i', normalised to length 1, temperature \tau:

\mathcal L_i = -\log\frac{\exp(\mathbf z_i^\top\mathbf z_i'/\tau)}{\sum_{j=1}^B\exp(\mathbf z_i^\top\mathbf z_j'/\tau)}:

a softmax classification of “which of the B views is my partner”, with cross-entropy (InfoNCE). The demo’s encoder used it per pixel position.

Masked autoencoding: \mathcal L = \sum_{\text{hidden } j}\|\hat P_j - P_j\|^2, the error on the hidden patches only.

Linear probe: logistic regression on frozen embeddings \mathbf z(p): P(\text{class 1} \mid p) = \sigma(\mathbf w^\top\mathbf z(p) + b), \mathbf w, b by minimising the cross-entropy on the labelled pixels. Only \dim\mathbf z + 1 parameters.

Promptable segmentation

Input: the image plus a prompt (a click, a box); output: a mask for “the thing at the click”. In miniature: compare every pixel’s embedding with the clicked pixel’s, threshold the similarity, keep the connected piece containing the click.

  • Trained mostly on photographs: it may not know what a cell boundary is in a phase-contrast image.
  • A click gives a mask, not a definition of an object that is consistent over 1000 images; for measurements you still need a rule, checked against annotations.

The probe demo in click-prompt mode on the synthetic image (nuclei of any brightness, debris, fibres): a click on one bright nucleus (red circle) and the resulting mask (yellow outline) from thresholding the embedding similarity at 0.5 and keeping the connected piece: it covers that nucleus (Dice 0.91).

Try it: frozen features and a probe

Calibration: can you trust 0.95?

A network outputs a probability. It is calibrated if, among all predictions made with confidence 0.95, 95 % are correct.

  • Reliability diagram: bin the predictions by confidence, plot the accuracy in each bin against the confidence. Calibrated = on the diagonal.
  • Modern networks are often overconfident: they say 0.99 and are right 90 % of the time; more so on data from a new microscope.
  • Temperature scaling: divide the network’s scores (logits) by one number T > 1, fitted on validation data. It fixes the in-domain calibration and never changes which class is predicted. It does not fix domain shift.
  • Disagreement between several networks (an ensemble), or between augmented copies of the input, flags doubtful regions.

Three reliability diagrams of the simulated classifier (bars: accuracy per confidence bin; red dots: mean confidence; dashed: the diagonal). Overconfident: accuracy 89.0 %, mean confidence 96.1 %, ECE 7.0 %. With fitted temperature T = 2.86: on the diagonal, ECE 0.4 %, accuracy unchanged. Same T on a shifted domain: accuracy 73.0 %, confidence 83.5 %, ECE 10.5 %.

The math: ECE and temperature

Bins b of the confidence, n_b predictions in bin b, accuracy \mathrm{acc}_b, mean confidence \mathrm{conf}_b:

\mathrm{ECE} = \sum_b \frac{n_b}{n}\,|\mathrm{acc}_b - \mathrm{conf}_b|.

Temperature: \hat{\mathbf p} = \mathrm{softmax}(\mathbf z/T). Since z_k/T is increasing in z_k for T > 0, \arg\max_k z_k/T = \arg\max_k z_k: the predicted class and the accuracy are unchanged. T is chosen to minimise the negative log-likelihood -\frac1n\sum_i\log\hat p_{i, y_i} on validation data.

Example: logit 2 for a two-class problem: \sigma(2) = 0.88; with T = 2: \sigma(1) = 0.73.

Try it: reliability diagrams

Practical judgement

  • Data: tens of annotated images for a U-Net on one microscope (with augmentation); a handful of strokes for a probe on good frozen features; many thousands to train a transformer from scratch. Consistent annotations matter more than many. Augment with what varies in your experiments, never with what you want to measure (no intensity augmentation if intensity is the readout).
  • Domain shift: a new microscope, stain or magnification shifts pretrained features too. Test on a new session or site (the evaluation day’s split rules).
  • When classical is better: few images; strong prior knowledge of the shape (Hough, model fitting); a measurement whose bias must be understood; no annotations and no time; results that must be explained to a reviewer; or a classical pipeline that already works (compare with the thresholding and random-forest baselines first). Often best: a network map + a classical step.

Summary

  • Self-attention: each token takes a softmax-weighted average of all tokens, weights from query–key matches: content-dependent and global; a convolution is fixed, local and position-only. Cost \propto N^2; no built-in locality: needs data or pretraining.
  • Transfer learning: frozen embeddings + a linear probe (few labels) or fine-tuning (more labels).
  • Self-supervised pretraining (contrastive, masked) learns without labels; the augmentations decide what is ignored. Promptable segmenters give a mask for a click.
  • Calibration: check the reliability diagram; temperature fixes in-domain overconfidence, not domain shift.
  • Choose classical when data, prior knowledge or explainability favour it.