|
I built a clean, well-structured deep learning pipeline using MONAI (Medical Open Network for AI) on a public abdominal ultrasound dataset. The pipeline included:
And the model still struggled to learn. The interesting part isn't that the model underperformed. What mattered was the diagnosis: a series of simple checks that traced the problem back to the dataset, not the model. Those checks are useful far beyond medical imaging. They apply to almost any machine learning project. If you're new to ML, this is a lesson worth carrying into every project: understand your data before you tune your model. I set out to build a medical image segmentation tutorial. I ended up learning a more valuable lesson: no amount of careful engineering can rescue a model from a dataset that can't support the task. By the end of this article, you'll understand:
This is not a beginner introduction to deep learning – it assumes familiarity with concepts like UNet architectures and training loops. But the data-quality lessons apply broadly to many ML projects. What We'll Cover:
The DatasetI used the US Simulation & Segmentation dataset, a public collection of abdominal ultrasound images with organ segmentation labels from Kaggle. It contains:
At first glance, the dataset looked ideal:
Whether it actually supported the task was a different question. Step 1: Rule Out the Pipeline Before Blaming the DataGround rule: you should always rule out the pipeline before blaming the data. A model failing on buggy code looks exactly like a model failing on bad data. The engineering needs to be trustworthy. Subject-Grouped SplitsA common mistake in medical imaging is randomly splitting images into train and test sets. That approach is problematic because many frames come from the same patient. Those frames share anatomy, scanner settings, and noise patterns. If frames from the same patient appear in both the train and test sets, the model can partially memorize patient-specific patterns. Test scores look artificially good, even though the model may fail on truly unseen patients. This is called subject leakage. The fix is to split by patient instead of by image: That assertion matters.If the split logic ever breaks, the pipeline fails loudly instead of silently producing misleading metrics. Decoding Masks CorrectlyThe dataset stores labels as color-coded masks. Each organ corresponds to a different RGB color. Training requires converting those colors into integer class labels. A naïve implementation uses exact color matching, but resizing operations can slightly alter colors at mask boundaries. A more robust approach maps each pixel to its nearest palette color: Before training, it’s worth visually checking a few decoded masks against the original images. This catches issues like incorrect palettes, RGB/BGR channel swaps, or resizing artifacts that silently corrupt labels. These bugs rarely throw errors. Instead, the model simply learns poorly. And “trained on wrong labels” looks exactly like “the model can’t learn the data.” Verifying masks early removes that uncertainty. Loss Design and Class WeightingFor training, I usd standard MONAI segmentation losses. The goal wasn’t to aggressively maximize performance, but to establish a stable and trustworthy baseline. The training curves below show that the model optimized normally: the loss decreased consistently, and the validation dice stabilized rather than diverging. This helped rule out optimization instability as the primary cause of poor final performance. ![]() Three choices were deliberate:
Step 2: The Model Still StruggledThe first experiment focused on liver segmentation — the simplest single-organ task in the dataset.
Dice scores range from 0 (no overlap) to 1 (perfect overlap). Qualitatively, the predictions often captured rough liver regions but failed at boundaries and consistency across real scans. Especially important:
At this point, two explanations were possible:
Because the engineering had been carefully validated, the second possibility became worth investigating seriously. That's where the real lesson began. Step 3: Interrogating the DatasetRather than endlessly tuning the model, the productive move is to turn the diagnostic lens on the dataset. Three simple checks revealed the real problem. None required retraining or expensive experiments. Diagnostic 1: What Does the Dataset Actually Contain?The first step was simply plotting the dataset composition. ![]()
This immediately changed the interpretation of the dataset. Although the dataset contains many real ultrasound scans, almost all labeled training data is synthetic. The model is effectively trained on synthetic ultrasound and expected to generalize to real ultrasound. That's a difficult transfer problem from the start. The limitation is simple: the real images mostly don't have labels, so supervised training has very little real-world data to learn from. Lesson:Before training anything, chart the dataset composition. A headline image count can be misleading. "1,500 images" sounds large until you discover that only a tiny fraction are labeled examples from the target domain. Diagnostic 2: Do Synthetic and Real Images Look Similar?The next question was whether the synthetic and real ultrasound images actually followed similar visual distributions. Plotting intensity histograms showed a clear mismatch. ![]()
The synthetic simulator captured anatomical geometry reasonably well, but it didn't reproduce the texture and noise characteristics of real ultrasound:
This is the classic synthetic-to-real domain gap. The model learned features tuned to synthetic images and then encountered a substantially different distribution during evaluation. Poor transfer performance became expected rather than surprising. Lesson:Whenever training and deployment happen on different domains — synthetic → real, scanner A → scanner B, hospital A → hospital B — measure the distribution shift directly. Simple histogram comparisons can reveal major problems in minutes. Diagnostic 3: Can the gap be fixed by adding real data?The obvious next idea was: why not include some real labeled data during training? But before implementing that approach, it's worth checking how many distinct patients actually had labels. Only fourpatients. That result fundamentally changed the situation. Proper medical imaging evaluation requires subject-grouped train/test splits. But with only four patients, any evaluation becomes statistically unstable. Training on two or three patients and testing on one or two patients would produce highly unreliable metrics that depend heavily on which patient happened to be held out. At that point, the dataset simply couldn't support trustworthy real-world evaluation. Lesson:In medical imaging, count subjects, not images. The true size of a dataset is bounded by the number of independent patients, not the number of files. Step 4: Knowing When to StopAt this point, additional tuning no longer made sense. The bottleneck was not the architecture, optimizer, or learning rate. The bottleneck was the dataset itself. The pipeline was still valuable and reusable. But this particular dataset couldn't reliably support the intended segmentation task. That distinction matters: sometimes a problem is difficult but solvable, and sometimes the data simply can't support the conclusion you want to draw. Learning to recognize the difference is an important ML skill. A Practical Dataset Evaluation ChecklistBefore committing weeks to model development, these checks are worth running on any dataset:
These checks take minutes and can save weeks of unnecessary tuning. What I Would Try NextImproving results would likely require better data rather than a larger model. The next steps I'd prioritize:
All of these target the actual bottleneck: data quality and data diversity. The Bigger LessonIn machine learning, it's easy to focus most of our attention on architectures, hyperparameters, optimization tricks, and newer models. But the dataset quietly defines the ceiling. A sophisticated model on weak data often disappoints, while a simpler model on strong data performs surprisingly well. That was the real lesson from this project. The most valuable skill wasn't building the pipeline. It was diagnosing why the model couldn't succeed and being willing to trust what the data was saying. The workflow — checking dataset composition, counting subjects, comparing distributions, ruling out engineering bugs, and deciding when to stop — transfers to almost any ML project. In many projects, better judgment about the data matters more than a better model. The pipeline code and diagnostic notebooks are available at the MONAI Ultrasound Working Group repository. Questions, corrections, and improvements are always welcome. |



