Skip to content

Repository files navigation

Microscopy Nuclei Segmentation & Morphological Profiling

An end-to-end biomedical computer vision pipeline for nuclei segmentation, instance separation, quantitative evaluation, failure analysis, and morphological profiling in microscopy images.

The project develops a nuclei-analysis system progressively from classical image processing to a custom compact U-Net, validation-tuned watershed instance separation, and external benchmarking against pretrained StarDist and Cellpose-SAM models.

Evaluation is performed on a frozen 102-image test set containing 3,678 annotated nuclei.


Final Benchmark

Model Comparison

Method Dice ↑ IoU ↑ Instance Micro F1 ↑ Count MAE ↓
Classical CV 0.8028 0.6998 N/A 12.90
Custom U-Net + Watershed 0.8353 0.7480 0.7350 9.72
StarDist 0.7667 0.7015 0.7405 7.54
Cellpose-SAM 0.9030 0.8424 0.8955 3.78

Instance matching threshold: IoU ≥ 0.50

Cellpose-SAM achieved the strongest overall benchmark performance.

The custom pipeline, however, was developed independently from classical preprocessing through semantic segmentation, failure analysis, robustness experimentation, validation-based instance-separation tuning, and quantitative morphology.

StarDist and Cellpose-SAM were subsequently evaluated as pretrained external benchmarks against the same frozen test set.


Project Overview

Microscopy segmentation involves more than predicting whether individual pixels belong to nuclei.

A useful biomedical image-analysis system should be able to:

  1. identify nuclear regions,
  2. separate touching or overlapping nuclei,
  3. detect individual objects,
  4. quantify segmentation quality,
  5. measure nuclear morphology,
  6. identify model failure modes,
  7. compare alternative segmentation approaches.

The project therefore follows the experimental progression:

Microscopy Images
        │
        ▼
Classical Computer Vision Baseline
        │
        ▼
Custom U-Net Semantic Segmentation
        │
        ▼
Failure Analysis
        │
        ▼
Robustness Experiment
        │
        ▼
Marker-Controlled Watershed
        │
        ▼
Validation-Based Watershed Tuning
        │
        ▼
Instance-Level Evaluation
        │
        ▼
Morphological Profiling
        │
        ▼
External Model Benchmarking
        ├── StarDist
        └── Cellpose-SAM

This progression allows pixel-level segmentation, object-level detection, counting, morphology, robustness, and external model performance to be evaluated separately.


Dataset

The project uses microscopy nuclei images from the 2018 Data Science Bowl dataset.

Each sample contains one microscopy image and an independent binary mask for every annotated nucleus.

sample_id/
├── images/
│   └── sample_id.png
└── masks/
    ├── nucleus_1.png
    ├── nucleus_2.png
    └── ...

A deterministic split was generated once and stored in:

data/splits.json
Split Images
Training 468
Validation 100
Test 102
Total 670

The 102-image test set remained frozen during model development and watershed parameter tuning.

The test set contains:

3,678 annotated nuclei

Raw microscopy images are excluded from the Git repository.


1. Classical Computer Vision Baseline

An interpretable non-learning segmentation pipeline was implemented first.

RGB Image
    │
    ▼
Grayscale
    │
    ▼
CLAHE
    │
    ▼
Gaussian Denoising
    │
    ▼
Otsu Thresholding
    │
    ▼
Morphological Cleanup
    │
    ▼
Distance Transform
    │
    ▼
Foreground Markers
    │
    ▼
Watershed

The pipeline combines:

  • CLAHE contrast enhancement
  • Gaussian filtering
  • Otsu thresholding
  • morphological opening and closing
  • distance transforms
  • connected-component markers
  • marker-controlled watershed segmentation

Classical Segmentation Pipeline

Classical Test Performance

Metric Result
Test images 102
Mean Dice 0.8028
Mean IoU 0.6998
Median Dice 0.8551
Median IoU 0.7469
Mean absolute instance-count error 12.90
Mean processing time 0.0031 s/image

The classical pipeline remained competitive on images whose intensity distributions matched its thresholding assumptions but was less reliable across the heterogeneous microscopy domains present in the dataset.


2. Custom U-Net Semantic Segmentation

A compact U-Net was implemented from scratch using PyTorch.

The network contains:

  • convolutional encoder blocks,
  • max-pooling downsampling,
  • bottleneck features,
  • transposed-convolution upsampling,
  • encoder-decoder skip connections,
  • single-channel segmentation logits.

Approximate model characteristics:

Trainable parameters: 483,441
Checkpoint size:      ~1.88 MB

The compact architecture was intentionally selected to make experimentation inexpensive while retaining the core U-Net encoder-decoder design.


Training Configuration

Input resolution:   128 × 128
Optimizer:          Adam
Loss:               BCEWithLogitsLoss + Dice Loss
Batch size:         8
Training images:    468
Validation images:  100

Best validation Dice:

0.8582

Selected checkpoint:

models/best_unet.pth

3. Classical CV vs Custom U-Net

Both approaches were evaluated using the same frozen 102-image test split.

Method Mean Dice Mean IoU
Classical CV 0.8028 0.6998
Custom U-Net 0.8353 0.7480

Absolute improvement:

Dice: +0.0325
IoU:  +0.0482

The U-Net achieved higher Dice on:

76 / 102 images

while:

Classical CV better: 25 images
Equal:                 1 image

The learned model therefore improved average segmentation quality without uniformly dominating the handcrafted baseline.


4. Failure Analysis

Dataset-level averages can hide severe individual failures.

The dataset contains substantial visual heterogeneity, including:

  • dark fluorescence images,
  • bright-background microscopy,
  • dense nuclear populations,
  • elongated structures,
  • unusual staining,
  • substantial contrast and intensity variation.

A dedicated failure-analysis pipeline was implemented to compare classical CV and U-Net predictions at the image level.

Failure Analysis

Five severe U-Net failures were identified using:

Dice < 0.50

Image Characteristics

Strong U-Net cases:

Mean image intensity: 30.67
Mean contrast:        16.26

Severe failure cases:

Mean image intensity: 138.21
Mean contrast:         25.50

This suggests sensitivity to appearance and domain variation.

Largest U-Net Advantage

Classical Dice: 0.067
U-Net Dice:     0.718
Gain:          +0.651

Largest Classical Advantage

Classical Dice: 0.839
U-Net Dice:     0.472
Difference:    -0.367

The experiment demonstrates why biomedical computer-vision systems should be examined at both aggregate and individual-image levels.


5. Robustness Experiment

A second U-Net training experiment introduced stronger appearance augmentation to investigate whether the severe domain-shift failures could be reduced.

The experiment partially succeeded.

Mean Dice across the severe baseline failure cases increased from:

0.0952 → 0.1668

Several previously failed samples recovered partially.

However, overall test performance decreased:

Baseline U-Net Dice: 0.8353
Robust U-Net Dice:   0.8278

Baseline IoU:        0.7480
Robust IoU:          0.7305

Across the test set:

Improved images: 19
Degraded images: 82
Unchanged:        1

The augmented checkpoint was therefore not selected as the final model.

This negative result is intentionally retained because it demonstrates an important trade-off: improving selected domain-shift failures does not necessarily improve overall generalization.


6. Semantic Segmentation to Individual Nuclei

Semantic segmentation answers:

Which pixels belong to nuclei?

It does not necessarily answer:

How many individual nuclei are present?

Touching nuclei can form one connected foreground region.

The U-Net semantic prediction was therefore converted into individual instances using marker-controlled watershed segmentation.

U-Net Probability Map
        │
        ▼
Binary Semantic Mask
        │
        ▼
Distance Transform
        │
        ▼
Local-Maxima Detection
        │
        ▼
Watershed Markers
        │
        ▼
Marker-Controlled Watershed
        │
        ▼
Individual Nuclear Instances

7. Validation-Only Watershed Tuning

Watershed performance depends strongly on marker-generation parameters.

Three parameters were optimized:

min_distance
peak_rel_threshold
min_object_size

A grid search containing 45 configurations was performed exclusively on the 100-image validation split.

The test set was not used for parameter selection.

Selected Configuration

min_distance        = 9
peak_rel_threshold  = 0.20
min_object_size     = 15

Validation performance:

Micro Precision:  0.8448
Micro Recall:     0.7191
Micro F1:         0.7769

Macro F1:         0.8145
Matched IoU:      0.8040
Count MAE:        7.11

These parameters were frozen before final test evaluation.


8. Instance-Level Evaluation

Predicted nuclei were matched one-to-one against ground-truth nuclear masks.

A predicted object is considered correctly detected when:

IoU(prediction, ground truth) ≥ 0.50

Final Custom Pipeline Results

Ground-truth nuclei:  3,678
Predicted instances:  3,013

True positives:       2,459
False positives:        554
False negatives:      1,219
Metric Score
Micro Precision 0.8161
Micro Recall 0.6686
Micro F1 0.7350
Macro Precision 0.8067
Macro Recall 0.7739
Macro F1 0.7797
Mean matched IoU 0.8104
Count MAE 9.72

Median per-image detection rate:

0.8377

9. Effect of Watershed Tuning

Before tuning:

Instance Micro F1: 0.6308
Count MAE:         13.34

After validation-based tuning:

Instance Micro F1: 0.7350
Count MAE:          9.72

Improvement:

F1:        +0.1042
Count MAE: -3.62 nuclei/image

This experiment demonstrates that post-processing is not merely cosmetic.

Carefully optimized instance-separation parameters substantially changed the object-level performance of an otherwise unchanged semantic segmentation model.


10. Morphological Profiling

After instance separation, each detected nucleus can be converted into quantitative measurements.

The pipeline extracts:

Area
Perimeter
Circularity
Eccentricity
Solidity
Mean intensity
Centroid coordinates

Morphological profiling was performed on a 30-image cohort.

Nuclei profiled:   1,016
Mean area:         512.08 px²
Median area:       284.00 px²
Mean circularity:  0.887
Mean eccentricity: 0.650
Mean solidity:     0.955
Mean intensity:    68.93

Morphology Analysis

This converts segmentation output into structured quantitative measurements suitable for downstream biomedical image analysis.


11. End-to-End Qualitative Analysis

The final pipeline visualization deliberately includes:

  • a best-performing case,
  • a representative case,
  • a failure case.

For each case the figure displays:

Original microscopy image
Ground truth
U-Net probability map
Semantic prediction
Watershed instance segmentation
Morphological centroids

Final Pipeline Results

The failure case is intentionally retained rather than removed from the presentation.

This makes the limitations of the custom model visible instead of reporting only favorable examples.


12. StarDist Benchmark

The project was subsequently benchmarked against the pretrained:

StarDist 2D_versatile_fluo

model.

StarDist directly predicts star-convex nuclear instances and therefore provides an external instance-segmentation baseline fundamentally different from the custom semantic-U-Net-plus-watershed pipeline.

The same frozen 102-image test set was used.

StarDist Results

Semantic Evaluation

Mean Dice:   0.7667
Median Dice: 0.9142

Mean IoU:    0.7015
Median IoU:  0.8419

Instance Evaluation @ IoU ≥ 0.50

Ground-truth nuclei: 3,678
Predicted instances: 3,279

True positives:      2,576
False positives:       703
False negatives:     1,102
Metric Result
Micro Precision 0.7856
Micro Recall 0.7004
Micro F1 0.7405
Macro Precision 0.7848
Macro Recall 0.7436
Macro F1 0.7580
Mean matched IoU 0.8453
Count MAE 7.54

StarDist produced slightly higher instance F1 than the custom U-Net + watershed system:

Custom pipeline: 0.7350
StarDist:        0.7405

while achieving substantially lower counting error:

Custom pipeline: 9.72
StarDist:        7.54

Its mean semantic Dice was lower because the pretrained model exhibited severe failures on several heterogeneous microscopy domains.


13. Cellpose-SAM Benchmark

A second external benchmark was performed using:

Cellpose-SAM cpsam_v2

The model was evaluated on the same frozen 102-image test set.

Inference was performed using an NVIDIA GPU.

Cellpose-SAM Results

Semantic Segmentation

Mean Dice: 0.9030
Mean IoU:  0.8424

Instance Segmentation @ IoU ≥ 0.50

Micro Precision: 0.9111
Micro Recall:    0.8804
Micro F1:        0.8955

Mean matched IoU: 0.8517
Count MAE:        3.78

Cellpose-SAM achieved the strongest overall benchmark performance.

It substantially improved both instance detection and counting accuracy compared with the custom pipeline and StarDist.

Importantly, this benchmark is presented as an external pretrained-model comparison, not as a component trained as part of the custom pipeline.


14. Final Model Comparison

Final Model Comparison

Method Dice ↑ IoU ↑ Instance F1 ↑ Count MAE ↓
Classical CV 0.8028 0.6998 N/A 12.90
Custom U-Net + Watershed 0.8353 0.7480 0.7350 9.72
StarDist 0.7667 0.7015 0.7405 7.54
Cellpose-SAM 0.9030 0.8424 0.8955 3.78

The comparison illustrates three different approaches to nuclei segmentation:

Classical CV

Highly interpretable and computationally inexpensive, but dependent on image-processing assumptions.

Custom U-Net + Watershed

A compact task-specific learned semantic model combined with classical instance separation.

StarDist

A specialized pretrained nuclear instance-segmentation model based on star-convex object representations.

Cellpose-SAM

A large pretrained generalist segmentation model that achieved the strongest overall performance on this test set.

The objective of the project is therefore not to claim that the compact custom model surpasses large pretrained systems.

Instead, the project demonstrates the complete process of building, diagnosing, improving, evaluating, and benchmarking a biomedical computer-vision pipeline.


15. Results Dashboard

Results Dashboard

The project reports both pixel-level and object-level performance because semantic overlap alone does not determine whether individual nuclei are correctly detected.


Repository Structure

microscopy-cell-analysis/
│
├── data/
│   └── splits.json
│
├── models/
│   ├── best_unet.pth
│   └── best_unet_robust.pth
│
├── outputs/
│   │
│   ├── features/
│   │   ├── classical_test_results_full.csv
│   │   ├── unet_test_results_full.csv
│   │   ├── robust_unet_test_results_full.csv
│   │   ├── baseline_vs_robust_unet.csv
│   │   ├── model_failure_analysis.csv
│   │   ├── instance_evaluation_results.csv
│   │   ├── watershed_tuning_results.csv
│   │   ├── stardist_test_results.csv
│   │   ├── cellpose_test_results.csv
│   │   └── ...
│   │
│   └── figures/
│       ├── classical_segmentation_pipeline.png
│       ├── analysis_summary.png
│       ├── failure_analysis_gallery.png
│       ├── final_pipeline_results.png
│       ├── final_results_dashboard.png
│       └── model_comparison.png
│
├── src/
│   ├── explore_dataset.py
│   ├── create_splits.py
│   ├── dataset.py
│   │
│   ├── classical_segmentation.py
│   ├── evaluate_classical.py
│   │
│   ├── unet.py
│   ├── train.py
│   ├── evaluate_unet.py
│   │
│   ├── train_robust.py
│   ├── evaluate_robust_unet.py
│   ├── analyze_failures.py
│   │
│   ├── evaluate_instances.py
│   ├── tune_watershed.py
│   │
│   ├── extract_morphology.py
│   ├── morphology_analysis.py
│   │
│   ├── evaluate_stardist.py
│   ├── evaluate_cellpose.py
│   │
│   ├── visualize_results.py
│   ├── create_final_figure.py
│   ├── create_final_dashboard.py
│   └── create_model_comparison.py
│
├── requirements.txt
├── .gitignore
└── README.md

Reproducing the Core Pipeline

1. Clone the Repository

git clone <repository-url>
cd microscopy-nuclei-segmentation

2. Create the Main Environment

Windows:

python -m venv .venv
.\.venv\Scripts\Activate.ps1

Linux/macOS:

python -m venv .venv
source .venv/bin/activate

Install dependencies:

python -m pip install -r requirements.txt

Core libraries include:

PyTorch
OpenCV
NumPy
Pandas
Matplotlib
SciPy
scikit-image

3. Prepare the Dataset

Place the extracted Data Science Bowl training samples under:

data/raw/stage1_train/

Expected structure:

data/raw/stage1_train/<sample_id>/images/
data/raw/stage1_train/<sample_id>/masks/

Raw data is intentionally ignored by Git.


4. Create Deterministic Splits

python src/create_splits.py

5. Train the Custom U-Net

python src/train.py

6. Evaluate Semantic Segmentation

python src/evaluate_unet.py

7. Evaluate the Classical Baseline

python src/evaluate_classical.py

8. Evaluate Instance Segmentation

python src/evaluate_instances.py

9. Tune Watershed Parameters

Watershed tuning uses the validation set only.

python src/tune_watershed.py

The selected parameters should then remain frozen during final test evaluation.


10. Run Failure Analysis

python src/analyze_failures.py

11. Extract Morphological Features

python src/extract_morphology.py

12. Generate Visualizations

python src/create_final_figure.py
python src/create_final_dashboard.py
python src/create_model_comparison.py

External Benchmark Environments

StarDist and Cellpose have substantially different dependency stacks from the core PyTorch pipeline.

Separate environments are recommended to avoid dependency conflicts.


StarDist

Example:

python -m venv .venv-stardist
.\.venv-stardist\Scripts\Activate.ps1

The benchmark used:

StarDist:   0.9.2
TensorFlow: 2.21.0
NumPy:      1.26.4
Model:      2D_versatile_fluo

Run:

python src/evaluate_stardist.py

Cellpose-SAM

Example:

python -m venv .venv-cellpose
.\.venv-cellpose\Scripts\Activate.ps1

The benchmark used:

Cellpose: 4.2.1.1
Model:    cpsam_v2

GPU acceleration is strongly recommended.

The benchmark environment detected:

CUDA available: True
GPU: NVIDIA GeForce RTX 4050 Laptop GPU

Run:

python src/evaluate_cellpose.py

The code should select GPU execution when CUDA is available.


Engineering Decisions

Why Start With Classical Computer Vision?

The classical pipeline provides an interpretable baseline.

It also reveals cases where handcrafted assumptions remain effective and prevents treating deep learning as automatically superior.


Why Build a Custom U-Net?

The objective was to implement and understand a complete segmentation system rather than only invoke pretrained models.

The custom U-Net provides control over:

  • data preparation,
  • architecture,
  • loss functions,
  • training,
  • probability prediction,
  • thresholding,
  • failure analysis,
  • post-processing.

Why Separate Semantic and Instance Evaluation?

High semantic Dice does not guarantee correct nucleus counting.

Several touching nuclei can be represented by one large foreground region while still achieving substantial pixel overlap.

For that reason the project evaluates:

Pixel Level

Dice
IoU

Object Level

Precision
Recall
F1
Matched-instance IoU
Count MAE

Why Tune Watershed on Validation Data?

Selecting post-processing parameters on the test set would leak information from the final evaluation set into system development.

Watershed parameters were therefore selected exclusively using the validation split and frozen before test evaluation.


Why Keep Negative Results?

The robustness experiment did not improve overall test performance.

Removing it would hide useful information.

Instead, the experiment demonstrates that targeted augmentation can improve selected failure domains while simultaneously degrading the dominant distribution.

Failure analysis and negative experiments are therefore treated as part of the model-development process.


Why Benchmark StarDist and Cellpose-SAM?

A custom model should not be evaluated in isolation.

StarDist provides a specialized nuclei instance-segmentation baseline, while Cellpose-SAM represents a substantially larger pretrained generalist segmentation approach.

Their inclusion provides context for the strengths and limitations of the compact custom pipeline.


Limitations

Domain Shift

The custom U-Net exhibits severe failures on a small subset of images whose appearance differs substantially from the dominant training distribution.


Input Resolution

Custom U-Net inference uses:

128 × 128

images.

This reduces computational cost but can remove fine boundary information and small nuclear structures.


Instance Recall

The custom pipeline achieved:

Precision: 0.8161
Recall:    0.6686

The system therefore tends to miss or merge some nuclei rather than generating excessive false-positive instances.


Watershed Dependence

Instance performance depends on handcrafted watershed parameters.

Although these were selected correctly using validation data, the optimal parameters may vary across microscopy domains.


Morphological Measurement Error

Morphological features are extracted from predicted instances.

Segmentation errors therefore propagate directly into downstream measurements such as area, circularity, and eccentricity.


Dataset Scale

The project contains 670 microscopy images.

Performance on this dataset should not be interpreted as evidence of clinical or laboratory deployment readiness.


External Pretrained Models

StarDist and Cellpose-SAM were evaluated using pretrained weights.

Their results therefore represent transfer performance from substantially larger external training regimes and are not directly equivalent to training the compact U-Net only on this project's training split.


Future Work

Potential extensions include:

  • higher-resolution U-Net training,
  • pretrained encoder backbones,
  • residual U-Net variants,
  • boundary-aware losses,
  • multi-scale inference,
  • stronger domain normalization,
  • test-time augmentation,
  • uncertainty estimation,
  • learned instance-separation approaches,
  • GPU training of the custom architecture,
  • cross-dataset generalization experiments,
  • morphology validation against ground-truth instances,
  • systematic inference-efficiency benchmarking under identical hardware.

What This Project Demonstrates

Computer Vision

  • image preprocessing
  • contrast enhancement
  • thresholding
  • morphology
  • distance transforms
  • connected components
  • marker-controlled watershed

Deep Learning

  • PyTorch
  • U-Net implementation
  • segmentation losses
  • training and validation
  • model checkpointing
  • GPU and CPU inference environments

Biomedical Image Analysis

  • semantic nuclei segmentation
  • nuclear instance separation
  • object matching
  • nuclei counting
  • morphological profiling

Evaluation

  • Dice
  • IoU
  • precision
  • recall
  • F1
  • matched-instance IoU
  • count MAE
  • failure-case analysis

Experimental Design

  • deterministic train/validation/test splits
  • frozen test evaluation
  • validation-only hyperparameter tuning
  • classical baseline comparison
  • negative-result reporting
  • domain-shift analysis
  • pretrained external benchmarking

Model Benchmarking

  • classical image processing
  • custom U-Net
  • StarDist
  • Cellpose-SAM

Summary

This project develops and evaluates an end-to-end microscopy nuclei-analysis pipeline rather than treating segmentation as an isolated model-training task.

The custom U-Net improved semantic segmentation over the classical baseline:

Classical Dice: 0.8028
U-Net Dice:     0.8353

Classical IoU:  0.6998
U-Net IoU:      0.7480

Validation-tuned watershed substantially improved instance separation:

Instance F1:
0.6308 → 0.7350

Count MAE:
13.34 → 9.72

Failure analysis exposed significant domain-shift behaviour, while a robustness experiment demonstrated the trade-off between recovering difficult cases and preserving overall performance.

The final system was then benchmarked against specialized pretrained segmentation models:

                         Dice     Instance F1    Count MAE

Custom U-Net + WS       0.8353       0.7350         9.72
StarDist                 0.7667       0.7405         7.54
Cellpose-SAM             0.9030       0.8955         3.78

Cellpose-SAM achieved the strongest overall benchmark performance.

The central contribution of the project is therefore the complete experimental workflow:

baseline
→ model development
→ failure analysis
→ robustness testing
→ instance separation
→ validation-based optimization
→ quantitative morphology
→ external benchmarking

The repository demonstrates practical computer-vision engineering across classical image processing, deep learning, biomedical segmentation, object-level evaluation, experimental design, and quantitative image analysis.

About

Biomedical computer vision pipeline for nuclei segmentation, instance detection and morphology, benchmarking custom U-Net, StarDist and Cellpose-SAM.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages