Cortical layers from two modalities · Visium DLPFC
Spatial transcriptomics · multimodal representation learning

Reading cortical layers from gene expression and tissue at the same time

Each spot on a Visium slide gives two measurements of the same few cells: a transcriptome and a photograph. This project trains encoders on both, fuses them to predict the cortical layer a spot belongs to, and tests whether a self-supervised objective that uses only the pairing between the two modalities can replace part of the labelled data. Everything is evaluated on a donor whose tissue the models never see during training.

01 — The data

Twelve sections, three donors, two modalities per spot

The spatialLIBD experiment sampled dorsolateral prefrontal cortex from three neurotypical donors, four sections each. Every spot carries a UMI count vector over 33,538 genes and sits at a known pixel coordinate on the H&E image of the same section, which is what makes the pairing exact rather than approximate. Two neuroanatomists annotated the six cortical layers and the white matter by hand.

Sections and split

hover a row for detail

Annotated spots per layer

Why the split is by donor. Sections from one donor come from the same tissue block, and two adjacent sections are almost the same cells cut twice. Splitting at random across spots or sections gives a model neighbouring tissue in both train and test and inflates every number. All results below are on Br8100, held out completely.

What one spot looks like

02 — Section explorer

Every spot, every prediction

Pick a section and colour the spots by the manual annotation, the model's call, its confidence, how much weight the fusion gate put on each modality, or the measured level of a marker gene. Hovering a spot shows the tissue underneath it and the full probability vector.

03 — Results

What each modality contributes

All rows are the same training recipe, the same held-out donor and the same evaluation code; only the input and the initialisation change. Sort by any column, and select a row to redraw the panels underneath it.

The short version is that fusion did not pay off. The best fused model finishes a thousandth of a point ahead of expression alone, and the three fusion schemes land within 0.023 of each other. Histology on its own is far weaker, though not empty: look at its white matter score against its layer 4 and layer 5 scores and it becomes clear the images carry one thing well and the rest poorly.

Held-out donor Br8100

click a row to inspect · click a header to sort

Confusion matrix

F1 by layer

Training curves

validation macro-F1

Learned joint embedding

UMAP, held-out donor, coloured by manual layer

Where the gate looks

weight given to expression, by layer
04 — Self-supervised pretraining

Learning from the pairing instead of the labels

The pretraining stage never reads a layer annotation. It takes the expression vector and the H&E crop from the same spot as a positive pair, every other spot in the batch as a negative, and trains both encoders with a symmetric InfoNCE loss. Spots within two lattice steps of each other are excluded from the negatives, because at 100 µm spacing a spot's neighbours contain much the same tissue and treating them as negatives works against the structure the objective is meant to find.

One thing this dataset cannot show is the usual argument for self-supervision. The spatialLIBD annotators labelled nearly every spot, so there is no meaningful pool of unlabelled tissue to exploit. What is testable is whether the pairing alone teaches the encoders enough that they need fewer labels afterwards, and whether the representation stands up on its own with nothing fitted on top of it.

Contrastive objective

loss and in-batch matching rate

What the frozen embeddings know

nearest-neighbour vote, nothing fitted

Cross-modal retrieval

held-out donor, expression query against an image gallery

Label efficiency

same architecture, same schedule, only the initialisation differs

Pretraining ablations

frozen encoders with a linear head, so the representation is what is measured
05 — Cross-modal prediction

How much of the transcriptome is visible in the tissue

Training a model to regress the expression panel from the H&E crop alone puts a number on the overlap between the two modalities. Genes that follow the cortical bands should be partly recoverable, since laminar position is written into the morphology; genes that vary for reasons with no visible signature should not be.

Correlation across all modelled genes

Pearson r, held-out donor

Best-recovered genes

hover for the value

Measured against predicted

each point is one spot on the held-out donor
06 — Methods

How this was built

Inputs

Counts are library-size normalised to the median training depth and log-transformed. The 3,000 most variable genes are chosen on the training sections only, excluding mitochondrial and ribosomal genes, then standardised with training means and standard deviations. Image crops are squares of twice the spot diameter taken from the full-resolution slide and resized to 112 px, so each crop covers the same physical area on every section regardless of small differences in spot size.

Encoders

Expression goes through a two-layer MLP to a 256-dimensional embedding. Images go through a ResNet-18 with the max pool removed from its stem, which puts the first residual stage at 56×56 rather than 28×28 and keeps nuclear-scale texture in range at a 112 px input. A stride-1 stem preserves more again but measured roughly ten times slower on the GPU used here, and it discards the pretrained stem weights. The two embeddings are combined by a gate that produces per-spot weights for each modality, and the fused vector feeds a linear classifier over seven classes.

Self-supervised stage

Symmetric InfoNCE over expression–image pairs with a learned temperature, trained on every training-section spot and reading no layer annotation. Augmentation is right-angle rotations, flips and a stain jitter on the image side, gene dropout and multiplicative noise on the expression side. Spots within two lattice steps on the same section are masked out of the denominator. The pretrained encoders are then either fine-tuned with the classifier or frozen and probed, and both are compared against the same architecture trained from scratch.

Evaluation

Donor Br8100 is held out entirely. Validation for early stopping uses two sections withheld from the training donors, which is optimistic by construction and therefore never reported as a result. Layer frequencies are uneven, so macro-F1 and balanced accuracy are the headline numbers and plain accuracy is shown alongside for reference. Spatial coherence counts how often a spot's prediction matches the majority call of its six neighbours, which separates a model that produces contiguous bands from one that is speckled at the same accuracy.

Reproducing

pip install -r requirements.txt
python -m src.data.download
python -m src.data.build_dataset
python -m src.data.features
python scripts/run_all.py
python -m src.export_dashboard

Source

Data from Maynard et al., Nature Neuroscience 24, 425–436 (2021), distributed through spatialLIBD. Code and trained configurations are in the repository this page is served from.