Flow fields to objects: Cellpose

Image analysis

About this lecture

The previous day ended with four maps a network can predict to separate touching cells. Today the fourth one in full: flow fields, the representation of the Cellpose family of networks (Stringer et al., 2021).

  • The training target: heat spread inside each cell, and arrows that point uphill.
  • What the network predicts: two arrow components and a cell probability per pixel.
  • Decoding: every pixel follows the arrows; pixels that meet at the same sink form one cell.
  • The flow check: drop masks whose arrows do not look like the arrows of a cell.
  • The cell diameter: why the image is rescaled, and what a wrong diameter does.
  • What goes wrong, on simulated arrows and on the real network in the browser.

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

Recall: arrows that lead to one point

From the day on modern deep learning (route 4 of “instances from a pixel map”):

  • For every cell pixel the network predicts a small arrow.
  • Inside each cell the arrows lead, along a path inside the cell, to one point.
  • Follow the arrows from every pixel: pixels that end at the same point belong to the same cell.
  • This follows bends: a C-shaped or bent cell is no problem.

Today: where the arrows come from, how they are followed, and what decides how many cells come out.

A C-shaped cell with its flow target: blue arrows along the gradient of heat diffused from a source in the cell (red star). Five particle paths (orange) start at the two ends and the middle of the C, follow the arrows around the bend inside the cell, and all end at the star.

Try it: flows on many cells (simulated)

The training target, step by step

Put heat at the cell’s centre and let it spread inside the cell only, as on the heat-equation day: repeatedly replace every pixel by the average of itself and its neighbours that belong to the cell.

Bar charts of the heat in a cell of 5 pixels with the source at pixel 3, after one, two and three steps of 'add 1 at the source, then average with the neighbours inside the cell': 0, 0.33, 0.33, 0.33, 0; then 0.17, 0.56, 0.67, 0.56, 0.17; then 0.36, 0.80, 0.93, 0.80, 0.36. Above the last chart the arrows: right, right, sink, left, left.

  • 5 pixels, source at pixel 3. Each step: add 1 at the source, then average over the in-cell neighbours (the end pixels have only one neighbour inside).
  • After 3 steps: (0.36, 0.80, 0.93, 0.80, 0.36). The arrows point uphill: →, →, sink, ←, ←.
  • Arrow = direction of the heat difference between the two neighbours, length 1: only the direction matters.

Heat in a C-shaped cell

A C-shaped cell of 528 pixels, source (red star) on the back of the C. Heat on a log colour scale after 10 steps: only a small spot around the source is warm. After 50 steps the warm region has crept about halfway along both arms. After 200 steps the whole C is warm, hottest at the source and coolest at the two tips. Right: the arrows (blue) and three paths (orange) from the two tips and the back, all following the arc to the star.
  • The source: the cell pixel nearest the median of the cell’s x and y coordinates (for a C, on its back, not in the hole).
  • The heat creeps along the arms; it needs about as many steps as the longest path in the cell to reach the tips (here about 23). The training target uses 2 × (width + height) of the cell’s box: 128 steps here.
  • The arrows follow the arc, because heat only travels inside the cell.

Why heat, not straight lines to the centre?

Why not let every arrow point straight at the centre?

  • In a C-shape the straight line from a tip to the centre crosses the hole: following it would leave the cell (74 of 101 points on that line lie outside).
  • That is the star-convex limitation of the previous unit in another guise: straight rays need a star-shaped cell.
  • Heat spreads only through the cell, so “uphill” bends around corners: the path from the tip follows the arc and arrives in 24 steps.

One word of warning: this “diffusion” is the heat equation (a PDE), not the generative “diffusion models” of a later day.

The same C-shaped cell twice. Left: arrows pointing straight at the source; the dashed line from the upper tip to the source runs through the hole of the C, outside the cell. Right: arrows uphill in the heat; the path from the same tip follows the upper arm and the back of the C to the source.

The math: the heat target

For a cell O with source c, the heat h solves the heat equation with a source, and no flow across the border:

\partial_t h = \Delta h + \delta_c \ \text{ in } O, \qquad \partial_n h = 0 \ \text{ on } \partial O .

  • Discretely (recall the heat-equation day): h(p) \leftarrow \operatorname{mean}_{q \in N(p) \cap O} h(q), after adding 1 at c; N(p) is the 3 \times 3 neighbourhood. Averaging only over in-cell neighbours is the no-flux condition.
  • The arrows are the normalised gradient \mathbf v = \nabla h / \|\nabla h\|, with central differences \partial_x h \approx h(x+1, y) - h(x-1, y) (0 outside the cell).
  • While heat is fed in at c, a pixel away from the source is at most the mean of its in-cell neighbourhood (discrete subharmonic; the 8 neighbours of c are the exception, but c itself is warmer). So (maximum principle) every pixel except c has a warmer neighbour: one maximum, one sink per cell (exercise B8).
  • Practical versions use \log(1 + h) before the gradient: same direction, better-scaled differences far from the source.

Try it: heat and arrows

What the network predicts

A U-Net (the U-Net unit of the neural networks day) with three output channels per pixel:

  • two arrow components (left–right, up–down): shown as a colour (hue = direction, brightness = length);
  • a cell probability (as a logit; above the threshold = “inside a cell”).

Trained on images with hand-drawn masks: the targets are the heat arrows of each mask, so the network learns to predict them from the image alone.

Every cell becomes a small colour wheel: each colour points to the cell’s centre.

Left: a fluorescence micrograph of about 100 cultured cells, cytoplasm in green and nuclei in blue. Right: the network's predicted arrows for the same image as colours: every cell is a small colour wheel whose colours run around its centre, with white arrows pointing inwards; the background is black.

Image: Cellpose demo images (cellpose.org), BSD-3-Clause; the real network running in the browser.

Decoding: follow the arrows

  1. Every pixel with cell probability above the threshold is a particle.
  2. Each particle moves one pixel along the arrow at its position (interpolated between pixels), again and again: a fixed number of steps (default 200 for cells of 30 pixels, scaled with the diameter).
  3. Particles gather at sinks. Count the end points per pixel (a histogram); its local peaks with more than 10 particles are the seeds.
  4. Each seed region collects the pixels whose particle arrived there: one mask per sink.

No threshold on a distance, no markers to tune: the arrows do the grouping.

The same micrograph darkened, with the paths of a sample of pixels drawn as thin coloured lines; they converge like the spokes of a wheel on red dots (the sinks, larger where more particles arrived) with white plus signs (the seeds), one per cell.

Following the flow as pseudocode

for every pixel p above the probability threshold:
    x = p
    repeat N times:  x = x + arrow at (x rounded to a pixel)
    end(p) = x
count the end points in every pixel
sinks = touching groups of pixels with at least 3 end points
mask of sink s = all pixels p whose end(p) lies in s
  • No markers and no distance threshold: the arrows do the grouping.
  • Too few steps N: particles stop on the way and miss the sink.

By hand on a grid

A 5 \times 5 field, one cell per step (· = no movement):

  • From (4, 0): → (3, 1) → (2, 1) → (2, 2), and stays: the sink in the middle.
  • From (3, 3): → (4, 4), and stays: a second sink.
  • From (0, 0): → (0, 1) → (0, 0) → …: two arrows point at each other, the particle goes back and forth: a spurious sink.

Group all 25 pixels by where they end: 19 at (2, 2), 4 at (4, 4), 2 stuck in the corner. Three objects: which ones are errors? (Exercise B2.)

A 5 by 5 grid of arrows. Three paths are drawn: orange from the bottom-left cell up to the centre cell, which has no arrow; green from cell (3, 3) diagonally to the bottom-right cell, which has no arrow; red between the two top-left cells, whose arrows point at each other.

The math: following the flow

Following the arrows is the Euler method for the ODE \dot{\mathbf x} = \mathbf v(\mathbf x):

\mathbf x_{j+1} = \mathbf x_j + \eta\,\mathbf v(\mathbf x_j), \qquad \eta = 1 \text{ pixel},

with \mathbf v bilinearly interpolated between pixel centres.

  • Along an exact path, \frac{d}{dt}h(\mathbf x(t)) = \nabla h \cdot \mathbf v = \|\nabla h\| > 0: h only increases, so a path can only end at a maximum of h, the source (exercise B7).
  • A sink is a point where nearby arrows point inwards: \mathbf v = 0 and \nabla\!\cdot\mathbf v < 0.
  • Noise breaks the gradient structure: where noisy arrows point at each other, a new zero of \mathbf v appears, a spurious sink. With step 1 a particle between two opposite arrows jumps back and forth; with interpolation and shorter steps it settles at the zero between them (exercise B9).
  • The number of steps must exceed the longest path in a cell: about the radius for a disc, half the arc length for a C (exercise B10).

Try it: following the arrows

The flow check

A mask can come out of the decoding even where the arrows are nonsense (debris, noise, a piece of a split cell). Test each mask:

  1. Compute the arrows the mask should have: its own heat target.
  2. Compare with the predicted arrows on its pixels: the mean squared difference, the flow error.
  3. Above the flow threshold (default 0.4): drop the mask as implausible.

Four pixels, predicted (1, 0), (0.8, 0.6), (0, 1), (−1, 0), the mask’s own (1, 0), (1, 0), (0, 1), (1, 0): squared differences 0, 0.4, 0, 4; mean 1.1 > 0.4: dropped.

The cells micrograph darkened, each mask outlined: green for kept masks, red for masks whose flow error is above the threshold; the flow error of each mask is printed on it, most values below 0.2.

The math: loss and flow check

Training loss per image, with predicted arrows \hat{\mathbf v}, target arrows \mathbf v (scaled by 5) and cell probability \hat p (logit \hat z):

L = \frac{1}{|\Omega|}\sum_{p \in \Omega} \Big( \lambda\,\|\hat{\mathbf v}(p) - 5\,\mathbf v(p)\|^2 \;+\; \operatorname{BCE}\big(\sigma(\hat z(p)),\, [p \in \text{cell}]\big) \Big),

with a fixed weight \lambda: the arrows are learned by regression (squared error, \mathbf v = 0 outside cells), the cell probability by classification (binary cross-entropy, the neural-networks day).

Flow check of a mask M, with \mathbf v_M the arrows of the heat target of M alone:

\operatorname{err}(M) = \frac{1}{|M|}\sum_{p \in M} \big\|\hat{\mathbf v}(p)/5 - \mathbf v_M(p)\big\|^2, \qquad \text{keep } M \text{ if } \operatorname{err}(M) \le \tau \ (= 0.4).

  • \operatorname{err} = 0 for exact arrows; two unit arrows at angle \theta differ by 2 - 2\cos\theta: 0.4 means about 37° on average.
  • Pixels only: the check needs no ground truth, so it works on new images.

Cell diameter and rescaling

The network was trained on cells of one size: about 30 pixels across for the cell network, 17 for the nuclei network. So the image is rescaled until its cells have that size: cells of 45 px → factor 30/45 = 0.667, and a 600 × 450 image becomes 400 × 300.

  • Too small a diameter: the image is enlarged too much, each cell looks like a cluster → splits; and 4× the pixels for half the diameter.
  • Too large: cells shrink to a few pixels → misses.
  • The diameter can be estimated: a small regression from the network’s internal summary of the image (its “style” vector) to the cell size.

Masks of the nuclei network on a fluorescence image of about 95 nuclei, run with three diameters. Diameter 8: most nuclei found, some split, and several bright nuclei missed; F1 0.84. Diameter 29, the right value: almost every nucleus has one mask; F1 0.92. Diameter 100: only seven masks; F1 0.14.

Image: BBBC039 (U2OS nuclei), Broad Bioimage Benchmark Collection, CC0.

The math: why the scale matters

  • Convolutions are translation-equivariant (shift the image, the output shifts) but not scale-equivariant: a filter of fixed size sees a different pattern when the cell is twice as large.
  • Rescaling by s = d_\text{train} / d maps cells of diameter d to d_\text{train}, the size range the filters were trained on. Cost: the number of pixels scales with s^2.
  • The predicted arrows are resized back to the original image, and the particles move there; the longest path in a disc is about d/2, so the number of steps is scaled with the diameter: n = 200\, d / d_\text{train}.
  • Size model: the network’s style vector \mathbf s (a global average of its deepest features) predicts \log d linearly: \log \hat d = \mathbf a^\top(\mathbf s - \bar{\mathbf s}) + b, fitted on the training images.

One network for many kinds of images

Cellpose’s networks are generalists: trained on many kinds of cells and microscopes at once.

  • Each image is normalised by percentiles (1st → 0, 99th → 1), so brightness and exposure do not matter.
  • Up to two channels: the one to segment (cytoplasm, membrane) and an optional nuclear channel.
  • Why it generalises: the flow representation is the same for every cell shape, and many different images teach the network what “a cell” is beyond one dataset.
  • Where it still fails: images unlike any in training. Stained tissue has dark nuclei on a bright background; the nuclei network expects bright nuclei: inverted, F1 0.87; not inverted, F1 0.

Four sample images: cultured cells with green cytoplasm and blue nuclei; a phase image of cells with bright ring-shaped membranes; grey fluorescent nuclei; a purple-stained tissue section with dark nuclei on a light background.

The tissue section with the nuclei network's masks. Left, image inverted: one mask on almost every dark nucleus. Right, not inverted: the masks cover the light gaps between the nuclei instead.

Images: Cellpose demo images (BSD-3-Clause); BBBC039 and the 2018 Data Science Bowl (BBBC038), CC0.

Failures by construction

Cause Result
noisy arrows extra sinks: splits (often dropped by the flow check)
touching cells with one smooth field one sink: merge
too few steps particles not yet arrived: masks shrink or break up
dim cells below the probability threshold misses
wrong diameter splits (too small) or misses (too large)

The cells micrograph segmented twice. Left, default 200 steps: one mask per cell, including the long protrusions. Right, only 10 steps, flow-check view: the kept masks (green) cover only the cell bodies around the nuclei, and many small pieces of protrusions are outlined in red as dropped.

Only 10 steps on the cells: 164 sinks instead of 105, the flow check drops 59 of 160 masks, and the 101 kept are smaller (median diameter 24.5 instead of 29.1 px).

Try it: Cellpose in the browser

Newer versions

  • The flow representation and the decoding stay the same in later versions; the network around them changes.
  • Image restoration in front (denoising, deblurring) for noisy images.
  • The latest version replaces the U-Net encoder by the large pretrained image encoder of promptable segmentation (the SAM unit, two units on) and no longer needs a diameter: it was trained to handle cells of very different sizes.
  • Too large for this site: about 620 MB of weights even at half precision, against 13 MB for the U-Net.

The cells image and its predicted arrows as colour wheels, as on the earlier slide.

Summary

  • Target: heat spread inside each cell from a source; the arrows point uphill and bend with the cell.
  • Network: a U-Net that predicts two arrow components and a cell probability per pixel.
  • Decoding: every cell pixel follows the arrows for a fixed number of steps; pixels that meet at the same sink are one cell.
  • Flow check: a mask whose arrows differ from its own target arrows (mean squared difference above 0.4) is dropped.
  • Diameter: the image is rescaled so that cells have the training size; a factor of about 2 off is tolerated, more is not.
  • Failures: noisy arrows split, shared fields merge, too few steps shrink, wrong polarity or size fails.