Using agents to train AI: curated and simulated data

Part 5: Scaling up

Introduction to agentic AI

About this lecture

Deep learning needs many labelled examples. In research, labels are the scarce part: a pathologist’s afternoon, a rare disease, a 3D volume that takes hours to outline. Agents cannot label for you, but they can attack this bottleneck from two sides.

  • Curate: the agent writes scripts that audit the labelled data you have, and builds a gallery for a human expert to check.
  • Simulate: the agent writes a simulator that makes images with exact labels for free, to train a network on.
  • Test on real data: a small, hand-labelled real test set decides whether any of it works.
  • A case study: the course author’s ongoing project, where synthetic scores looked good and the real predictions were rejected.

A concept unit with a light exercise. It builds on How LLMs and agents work, and how they fail and Privacy, ethics and safety.

Ten thousand images are easy to get; labelling them is not

A stack of 20 000 microscopy images from a scanner. One expert outlines each in about 5 minutes. 20 000 times 5 minutes is about 1 700 hours, about a year of full-time work, and a second expert disagrees.

Labels in research are rare, expensive, slow, inconsistent and sensitive

Five panels. Rare: a few dozen cases of a rare disease. Expensive: expert time of pathologists and clinicians. Slow: 3D volumes and videos take hours per item. Inconsistent: two experts draw two different outlines of the same nucleus. Sensitive: patient data, not for any cloud service.

A network learns from labelled examples and is judged on new ones

Labelled example tiles, intact or fragmented nuclei, feed into training a network. A held-out test set never seen in training gives a score of 91 percent correct. The score means something only if the test examples are new, correct and like the real use.

Before you label or simulate anything, try a pretrained model

  1. Pretrained, as is: does an existing model already do the job on 20 of your images? For nuclei and cells: Cellpose (now Cellpose-SAM) or StarDist.
  2. Fine-tune with few labels: start from a model trained on many images (a domain model, or a general ImageNet-trained network) and adjust it with tens of your labels.
  3. Only then curate or simulate to get more training data.

Check each model’s licence: Cellpose’s models are trained on data licensed CC BY-NC.

If you only need positions, a detector is cheaper than outlines

  • Boxes are much faster to label, and detectors such as YOLO* are fast to train and run. A simulator gives boxes as easily as outlines.

* The common YOLO software (Ultralytics) is AGPL-3.0: the licence matters for commercial or closed-source use.

Agents help in two ways: curate what you have, simulate what you lack

Centre: few, messy labelled examples. Left route, Curate: the agent writes audit scripts for duplicates, leakage, odd labels, broken files and units, builds a review gallery, the expert decides and every decision is logged. Right route, Simulate: the agent writes a simulator giving unlimited images with exact labels, trains the network on them, and tests on a small set of real hand-labelled images.

Most labelled datasets have problems nobody has looked for

  • Duplicates and near-duplicates: the same image twice, or mirrored, re-saved or cropped.
  • Leakage: the same patient, animal or slide in both training and test data.
  • Wrong labels: even famous benchmarks average about 3% wrong test labels (Northcutt et al. 2021).
  • Outliers and broken files: a blank image, a truncated file, a different stain.
  • Metadata: a pixel size in nanometres in a micrometre column; “Intact” and “intact”.
  • Imbalance: 95% of one class, so “always say normal” scores 95%.

Leakage makes a model look better than it is

Left: images split at random into train and test; squares of the same colour, the same patient, appear on both sides, so the test checks memory, not skill. Right: split by patient; the test patients are new and the score means what you think. Colour equals patient, or animal, slide, batch, plate or site.

The agent writes the audit; it reports and changes nothing

A manifest table with columns file, sample, split, label, pixel size and audit flag. Flags: exact copy of another image, patient also in train, looks fragmented, file cannot be opened, pixel size in nm, label spelled differently. Each flag is a question for a person, not a deletion. Illustrative.
Audit dataset/ before anyone trains on it. Report only; change nothing.

Flag, do not delete: the expert decides and every decision is logged

A script flags 41 suspect images. An expert looks at each one. 29 are relabelled because the label was wrong; 12 are kept as unusual but correct. Decisions go into decisions.csv with file, decision, reason, who and date. A cleaner that deletes everything unusual would have removed 12 correct, hard examples, exactly the cases the network most needs to learn.

A simulator gives perfect labels for free

Three panels from a simple nucleus simulator: the simulated fluorescence image, its exact mask with one colour per nucleus, and a density map that sums to the exact count of nuclei.

Randomise everything that should not change the answer

Ten simulated images with their exact counts, from 0 to 28 nuclei, varying in nucleus size, shape, brightness, crowding, blur, noise, background and debris.

The network learns your simulator, not the world

Top row: four simulated nucleus images. Bottom row: four real images from BBBC039. The real nuclei are larger, smoother and more elongated than the simulated ones.

Randomisation aims to make real images one more variation

Left: a narrow simulator; simulated images form a tight cloud and real images lie outside it, so the network guesses. Right: a randomised simulator; simulated images form a wide cloud and real images fall at its edge, one more variation. Each point is one image, placed by how it looks.

Label a small real test set first, and lock it away

Five steps. 1, label 20 to 50 real images by hand and lock them away. 2, the agent writes the simulator. 3, train on simulated images. 4, test on the locked real set. 5, fine-tune on a few other real labels and test again. If the real score is poor, improve the simulator, not the test set. Step 1 comes first, before anyone has seen what the model does.

Case study: putting cut tissue pieces back together

Schematic. 1, a cleared organ with vessels is cut by hand into pieces. 2, each piece is imaged in 3D separately; the knife-cut faces carry vessel openings. 3, find the cut faces and put the pieces back together; knife cut, natural surface and vessel opening look different. No one has labelled which surface is which, in 3D.

The course author’s ongoing project (unpublished; human tissue, so only schematics here).

The agent built a 3D tissue simulator and audited its labels

Pipeline: random organ shape and vessel tree in 3D, virtual knife cuts and handling deformation, rendering like the microscope with blur, texture and noise, exact labels for every voxel: cut, rim, normal. What the agent did to make the labels trustworthy: assigned whole specimens to train or validation before rendering; froze the generator before training; re-checked every label voxel with an independent script and test shapes; kept ambiguous places as unknown; measured real background noise and found the simulation about ten times too noisy.

Synthetic scores rose; the author looked at real predictions and said no

Left: on simulated validation specimens the F1 score of an improved model is about 0.8 to 0.94, against about 0.34 to 0.45 for a simpler control model. Right: on real tissue, the author looks at the predictions and calls them almost random; they are rejected as unusable and kept only as a baseline. No real labels existed to compute a score.

Finding the gap: what the simulator got wrong

Three diagnoses. Background noise: simulated about 10 times too noisy, found then fixed. Brightness towards a cut face: simulated drops at a sharp edge, real fades over about 0.5 mm, so the network learned a cue that real tissue does not show. Real or simulated: a classifier trained to tell real patches from simulated ones reaches AUC 0.87, where 0.5 means it cannot tell; summary numbers matched, the patches still did not, for one artefact type.

Lessons from the case

  • Synthetic metrics measure the simulator. Careful reports and good scores did not predict real usefulness.
  • A human looking at real outputs was the decisive test. Plan it; do not wait for it.
  • Lock a small real test set early, even for 3D data where labelling is slow.
  • Measure the gap: compare simple statistics of real and simulated data, and ask a classifier to tell them apart.
  • Design reviews so that the expert is not led: random choice set in advance, model answers hidden, “uncertain” allowed.
  • Keep failures as baselines. The negative result is part of the record.

You learn what you simulated

A loop: your assumptions about what nuclei, tissue and noise look like go into a simulator written by the agent, which trains a network, which says it works on simulated test images, which confirms your assumptions. You learn what you simulated. Break the loop with a real hand-labelled test set.

Where this goes wrong

  • Circularity: simulated tests confirm your own assumptions.
  • Cleaning removes hard cases: label-noise tools flag rare, correct examples too.
  • Hidden bias: a simulator of “typical” cells ignores the atypical patients.
  • Compute: training needs time and sometimes a GPU; start small.
  • Licences: real data used for fine-tuning must allow it; record the licence.
  • Privacy: auditing patient data with a cloud agent sends it to the provider.

What the agent does and does not do here

It does well

  • writes audit scripts and review galleries
  • writes simulators from your description
  • trains and evaluates small networks
  • documents splits, settings and decisions

It does not

  • know which flagged cases are truly wrong
  • know whether its simulator looks real
  • choose a fair real test set for you
  • see that real predictions are nonsense

The exercise: audit a dataset, then train a counter on simulated nuclei

Part A, Curate: 157 small labelled images; the agent writes an audit script and a review gallery; you decide file by file; result decisions.csv and a cleaned manifest. Part B, Simulate: the agent writes a nucleus simulator, trains a small counting CNN, tests it on real BBBC039 images, and optionally fine-tunes on a few real images. A laptop is enough.

References

  • Tobin J et al. (2017). Domain randomization for transferring deep neural networks from simulation to the real world. IROS 2017. arXiv:1703.06907
  • Northcutt CG, Jiang L, Chuang IL (2021). Confident learning: estimating uncertainty in dataset labels. J Artif Intell Res 70. arXiv:1911.00068
  • Northcutt CG, Athalye A, Mueller J (2021). Pervasive label errors in test sets destabilize machine learning benchmarks. NeurIPS Datasets and Benchmarks. arXiv:2103.14749
  • Kapoor S, Narayanan A (2023). Leakage and the reproducibility crisis in machine-learning-based science. Patterns 4:100804. doi:10.1016/j.patter.2023.100804
  • Lehmussola A et al. (2007). Computational framework for simulating fluorescence microscope images with cell populations. IEEE Trans Med Imaging 26:1010.
  • Rajaram S et al. (2012). SimuCell: a flexible framework for creating synthetic microscopy images. Nat Methods 9:634. doi:10.1038/nmeth.2096
  • Nehme E et al. (2018). Deep-STORM: super-resolution single-molecule microscopy by deep learning. Optica 5:458. doi:10.1364/OPTICA.5.000458

Summary

Concept Key fact
Label bottleneck labels are rare, expensive, slow, inconsistent and sensitive; images are cheap
Pretrained first try an existing model (Cellpose, StarDist), then fine-tune; boxes (YOLO) if positions suffice
Curation the agent writes audit scripts and a review gallery; flag, do not delete: hard cases look like errors
Leakage split by patient, animal, slide or batch; duplicates must not cross train and test
Simulation the agent writes a generator; every image comes with exact labels

Summary (continued)

Concept Key fact
Domain randomisation vary everything that should not change the answer (Tobin et al. 2017)
Sim-to-real gap the network learns the simulator; error on real images is larger
Real test set label a few dozen real images first, lock them away, judge everything on them
Case study good synthetic scores, careful reports; real predictions rejected on visual review
Circularity simulated tests confirm your own assumptions; only real data breaks the loop

Next (Choosing a model: families, sizes and benchmarks): which language model to run for which job, how to read model names and benchmark graphs, and why a small test on your own task beats a leaderboard.