# R7 Reference image retrieval

Status: small, exploratory reference-image experiment ready for review. [Result page](../results/r7/). This uses ImageNet-pretrained ResNet-18 as a simple general-image baseline, not DINOv3 and not a classifier trained on the Academy slides.

The public [UDE Diatoms in the Wild 2024 dataset](https://doi.org/10.5281/zenodo.10410655) provides taxonomic annotations and grayscale image cutouts. Its accompanying [paper](https://doi.org/10.1093/gigascience/giae087) describes imaging and annotation. The Zenodo record states CC0 for the dataset. Source image credit: Kloster, Burfeid-Castellanos, Dani, Mayombo, Beszteri and Vidaković, UDE Diatoms in the Wild 2024, version 2024.

From the metadata, the script selected six source-slide filename groups with at least three candidates in each of Achnanthidium, Navicula and Nitzschia, and with the longer annotation box dimension between 80 and 500 pixels. Seed 706 selected the source groups and one cutout per genus per group. Four groups form the 12-image reference gallery and two form the six-image query set. Grouping strips `.Chunk` from image filenames. It does not prove biological-sample or site independence. The images are focus-stacked and originate from the public dataset, not the Academy scans.

An ImageNet-pretrained ResNet-18 maps each complete, grayscale cutout to a 512-dimensional feature vector. The whole image is resized to fit 224 pixels, centered on a median-intensity square, repeated into RGB and ImageNet-normalized. We rank cosine similarity without supervised training. Five of six queries' highest-ranked gallery images share the dataset-recorded genus. The other query has no same-genus image among its top three. This tiny, balanced, closed-set sample is a pipeline demonstration, not an accuracy estimate. It was selected from source slides known to contain all three genera. The gallery has no other genera or debris, so the model must always choose an available reference even when it is inappropriate.

Seven Academy practice candidates were cropped from assistant-drawn traces and searched against the same gallery. They have no expert-confirmed taxon labels. The reference names apply to UDE images and should not be transferred to these candidates as identifications. The local and UDE inputs differ in focus stacking, contrast, optical resolution, capture conditions and taxon distribution. Annotated UDE cutouts can themselves contain neighbors and background.

The [retrieval code](r7_retrieval.py) requires the 18 reference files in `results/r7/reference/`, the reference manifest and `/tmp/resnet18-f37072fd.pth` (SHA-256 `f37072fd47e89c5e827621c5baffa7500819f7896bbacec160b1a16c560e07ec`). The [subset script](r7_ude_subset.py) retrieves selected reference images by ZIP range requests using the public 26,541,461-byte annotation CSV at `/tmp/ude-metadata.csv` (SHA-256 `e7439336199518115bab6beb4b48ecf1747a020cfe11ddb2a724a2f8d5183be1`). The temporary files are not durable. Re-run with `MPLCONFIGDIR=/tmp/mpl-diatom PYTHONDONTWRITEBYTECODE=1 /tmp/diatom-research-venv/bin/python research/r7_retrieval.py`.

Next expand to a frozen, sample-separated gallery with enough taxa and images to include look-alikes and unknowns, and obtain expert-confirmed local vouchers. Compare DINOv3 with this same ResNet baseline and inspect retrieval under rotation, partial coverage, focal plane and background changes. Determine rejection thresholds on separate validation data.
# Leave-one-source-slide-out and rotation diagnostic · 2026-11-30

The 18 previously selected UDE reference cutouts cover three genera and six source slide identifiers, one cutout per genus per slide. For each query, hold out every cutout with the same source slide identifier; rank the remaining 15 using frozen ImageNet ResNet-18 cosine similarity. Top neighbor genus matches in 13/18 original orientations and 16/18 after rotating each query 90 degrees. Top-three contains the query genus in 17/18 and 18/18 respectively. The top individual neighbor changes in 14/18 queries. [Inspect every case](/results/r7/slide-rotation.html) and [download rankings](/results/r7/slide-rotation-results.json).

This is a tiny, hand-selected, balanced external subset, not an estimate of classification performance on Academy samples. Queries and galleries recur across folds, labels are UDE's reference labels, and different slide identifiers do not guarantee independent collection sites or specimens. This test demonstrates orientation sensitivity of the ranked neighbors, not a reliable gain from rotation or identification of overlapping or partial diatoms. Test rotation-averaged embeddings on a larger independently sampled slide-held-out dataset, including local expert-reviewed crops, before selecting an approach.

## Expanded slide-separated comparison in progress · 2026-12-01

An additional fixed-seed selection is prepared in `research/r7_expanded_subset.py`: 24 previously unused filename-derived source groups, one reference crop for each of three genera per group, with 16 groups allocated to the gallery and eight to queries. After retrieval, `research/r7_expanded_retrieval.py` will compare frozen ResNet-18 features from the original orientation against pooled 0°, 90°, 180° and 270° features for both gallery and query. No new results are available yet. The larger subset is still balanced and selected from slides with all three genera; source-slide identifiers are not established biological-sample independence and it is not an Academy accuracy benchmark.

The first archive-directory request returned HTTP 504; a second process using bounded byte ranges ended with status 143 before producing the expanded manifest. No expanded slide-separated result is available. A separate [R14 stress test](/results/r14/prefix/) recovered complete entries from a previously cached, incomplete archive prefix; all are from one slide and cannot replace the planned comparison.

