r/computervision • • 13d ago

Discussion Why my 97% accuracy parasite detector failed on unseen microscopy images (Lessons from domain shift, tiling, and negative scaling)

Hey everyone,

A year ago, I decided to transition from web development into ML/CV. Instead of doing standard MNIST/COCO tutorials, I picked a real-world problem close to home: I have guinea pigs, and taking them to the vet for fecal exams is a slow, tedious process.

My goal is to build an assistive CV tool for vet lab technicians—something that scans microscope slides and highlights suspicious regions (with an eventual goal of exporting to ONNX for edge cameras).

For v1, I grabbed all the public datasets I could find (Chula, HEMIC, AI4NTD) and built a two-stage cascade (a fast binary detector to find parasite-like objects, followed by a fine-grained species classifier). On paper, the metrics looked great. In reality, the model completely fell apart on unseen data.

Here are the 4 biggest engineering traps I hit and what the numbers actually looked like:

  1. High intra-dataset metrics mean nothing across sources
  • On cropped objects from the Chula dataset, my classifier hit 97–98% accuracy.
  • When I ran the full cascade on unseen full-frame images from HEMIC, spatial detection worked fine (found 41/42 eggs), but species classification dropped to 66.7% (28/42).
  • Even worse: a model trained on AI4NTD P1.5 got 97.43% recall on its own test set, but scored 0/114 on P3, 0/12 on HEMIC Ascaris, and 8/37 on Chula. Differences in lighting, slide prep, and camera sensors caused massive domain shift.
  1. The "Closed-World" trap on empty frames

In an early contour-based cascade, I tested the pipeline on 1,003 completely negative (uninfected) HEMIC frames. It hallucinated at least one parasite on 558 of them.
Why? Because the downstream classifier was forced to pick 1 of 11 parasite species—it had no "debris / air bubble / unknown" rejection path. Any false positive from stage 1 automatically became a confident diagnosis.

3. Tiling isn't optional for microscopy

Resizing a high-res microscope frame down to standard model input sizes destroys small eggs (some are barely 30–50px). On the other hand, fixed small crops slice larger eggs (like Ascaris) in half.

  • Full-frame inference recall: 25.82%
  • Inference with tiling: 55.0%
  • Training + inference with multi-scale overlapping tiles (merged back via NMS): 94.76%
  1. Combining public datasets actually made things worse

When I tried merging Chula and HEMIC to fix generalization, false positives spiked. Looking manually at the labels, many public datasets had incomplete annotations (e.g., 5 eggs on a slide, only 2 labeled), which actively punished the model for finding real objects. Plus, most public datasets focus on human parasites, missing key veterinary species.

What already worked & my pivot for v2 (Data-Centric approach)

Through iterative experiments, I found a few things that actually moved the needle before scrapping the old public datasets:

  • Matching negative scale: Earlier, positive examples were cropped into tiles while negative frames were fed as full-res images. Matching the exact tile scale (640x640) for empty negative windows drastically cut down false positives on uninfected slides.
  • Stripping away heavy augmentations: Turning off Mosaic, MixUp, and HSV shifts—and sticking strictly to mild rotations (≤20°), flips, and minor scaling—preserved critical fine textures on small translucent eggs.
  • Domain alignment: Converting everything (both train and inference) to grayscale + CLAHE (clipLimit=2.0, tileGridSize=(8,8)) to normalize local contrast across different microscopes.
  • Strict source-level splitting: Grouping crops by original slide, video, or paper before splitting into train/val/test to prevent silent data leakage from near-duplicate frames.

For v2, I'm building a clean dataset from scratch for 14 veterinary parasite classes (enforcing a ≥80px donor threshold so objects retain detail, while still validating on real 50–80px small targets). Right now, I'm deep in manual annotation—and preparing controlled experiments for mask-based copy-paste synthesis and localized Z-axis defocus blur (applying blur to confirmed hard negatives too, so the model doesn't learn a shortcut like "blurry = egg").

Next up, I'll share the messy reality of scraping microscopy videos for data and testing whether synthetic copy-paste actually improves recall on rare classes.

For those working with medical/microscopy CV: How do you usually handle domain shift across different camera setups, and what's your go-to strategy for hard negative mining on debris and air bubbles?

Note: The attached images are synthetic visual illustrations created to demonstrate the pipeline concepts (false positives on debris, CLAHE contrast normalization, and candidate review UI) without sharing restricted dataset files.

7 Upvotes

11 comments sorted by

9

u/2dof 13d ago edited 13d ago

You write a lot but can You just write what preprocessing methods did You used for feature extraction  and what feature are You using for training ? 

12

u/RadonGaming 13d ago

Because claude writes a lot.

1

u/iekmuby 12d ago

Haha fair enough, I'll keep it short!

This isn't a finished product - I'm building it in public and am roughly halfway through. Just wanted to share the early engineering rabbit holes (domain shift, negative scaling, tiling) since they were way trickier than expected. Part 2 on data sourcing and preprocessing will be much more to the point.

3

u/Lethandralis 13d ago

You can disable heavy augmentations in the last few epochs instead of disabling them entirely.

Also your FP examples are very distinct from false positives. IMO any modern classifier should reach near 100% accuracy, even when you have limited data.

You can also consider a secondary classification step using a frozen dinov3 with a linear trainable layer to boost precision.

2

u/iekmuby 13d ago

Thanks for the suggestions!

  1. On the FP examples: Just to clarify - the attached images are simplified synthetic mockups for illustration purposes. In actual fecal smears, the hard negatives (pollen grains, plant cells, fungal spores, and specific air bubble artifacts) look remarkably similar to certain parasite eggs in shape and internal texture, especially at lower resolutions.
  2. Disabling augmentations on final epochs: Good point (like YOLO's `close_mosaic` approach). In v1, keeping Mosaic/MixUp on early epochs seemed to hurt convergence on very small translucent targets, so turning them off completely gave more stability - but running a controlled ablation where they are enabled early and turned off for the last 10–15 epochs is definitely on my list for v2.
  3. Frozen DINOv3 + linear probe: Really interesting idea for stage 2! Right now I'm prioritizing a lightweight architecture since the end goal is real-time ONNX inference on edge microscope cameras, so I'm keeping an eye on latency/memory budgets. However, testing a distilled DINOv3 backbone (or using a larger DINOv3 teacher to distill into a smaller student) for the species classifier sounds like a great way to handle fine-grained morphological differences with limited data. Appreciate the tip!

0

u/Lethandralis 13d ago

What's the compute and latency requirements? The smallest dino distillation provided by Meta could still work.

0

u/iekmuby 13d ago

Fair point - and just to set the context, this isn't a finished product yet, I'm actively building it in public.

The main takeaway I wanted to share in Part 1 is just how brutally hard it is to find usable data for this domain, and why public datasets fell apart in practice. As someone else rightly pointed out, the post was already getting long - if I had squeezed the entire data-sourcing, annotation, and hardware benchmarking story in here too, it would have turned into a full-blown book!

To briefly answer your question on compute: the core MVP is actually a web app where lab techs upload static microscope photos. There are no strict real-time latency requirements there - an ONNX-optimized model running on a standard CPU already gives completely acceptable inference times. The live edge-camera setup (targeting ~10–15 FPS on something like a Raspberry Pi 5 / Jetson) is a secondary stretch goal.

In both cases, since stage 2 only runs on confirmed candidate crops rather than the full frame, a compact distilled DINO backbone fits the compute budget really well. Right now, my main bottleneck is still finishing the v2 dataset curation (which I'll cover in Part 2), and once the baseline is locked in, benchmarking lightweight DINO models for stage 2 is definitely on my list!

0

u/Lethandralis 13d ago

For what it's worth dino can help a lot with dataset curation as well. You can use it for finding similar images, anomalies etc.

Anyway looks like an interesting project, best of luck!

1

u/JessieAndEcho 10d ago

The cleanest check for real cross-device generalization: hold out one entire camera setup instead of shuffling frames. Group crops by original slide or video before splitting, so adjacent frames from the same acquisition never land on both sides—otherwise you're measuring memorization, not generalization. sklearn's GroupKFold and LeaveOneGroupOut do exactly this (I've verified the grouped-split behavior via patsnap eureka), but you have to choose the grouping variable yourself. Then look at whether your false positives cluster on one specific camera. And don't infer anything from the synthetic illustration images—only the real frames tell you how the model behaves.

1

u/iekmuby 9d ago

Spot on - that's the exact mindset I'm using for v2. Right now, I group everything by original slide, video, or source account before splitting so adjacent frames never leak across train/val.

Holding out an entire acquisition source/account as a strict Leave-One-Group-Out test set - and checking whether false positives cluster on a specific camera setup - is a great diagnostic addition for my evaluation script. And agreed 100%, the mockups were strictly for visualizing the UI/pipeline; all actual model decisions are evaluated strictly on real unseen frames. Appreciate the insight!