A comprehensive segmentation pipeline combining SAM3, Cellpose, and DINO models for biological image analysis.
This pipeline performs semantic and instance segmentation on microscopy images using three state-of-the-art models:
- SAM3: Segment Anything Model 3 for text-prompted segmentation
- DINO: Vision transformer for semantic segmentation
The pipeline includes post-processing steps that combine instance and semantic segmentation results to improve accuracy and reduce false positives.
Due to package version conflicts, we need to create two separate conda environments:
conda create -n sam3_demo python==3.12
conda activate sam3_demo
pip install .conda create -n dino_demo python==3.12
conda activate dino_demo
pip install -r requirements.txtTo run the complete pipeline on NERSC:
./run_single_node_all_7classes.shThis script will:
- Run SAM3 inference (uses
sam3_demoenvironment) - Run DINO inference (uses
dino_demoenvironment) - Run post-processing (CPU-based,
dino_demoenvironment):- Filter and combine SAM3 + DINO results
- Optionally extract Hydrated and Dehydrated Xylem regions
Note: SAM3 and DINO inference can be parallelized in production deployment. Post-processing steps are CPU-based and independent of each other.
chmod +x submit_nersc_v6_reserved.sh
sbatch submit_nersc_v6_reserved.sh
squeue -u $USER- SAM3: Text-prompted segmentation for multiple anatomical classes
- DINO: Semantic segmentation for tissue classification
- SAM3 + DINO (
combine_sam_dino_v3): Filters SAM3 predictions using DINO semantic mask guidance; non-xylem classes can optionally be read directly from DINO (--dino-trust) - Region Extraction: Crops original images and filters instance masks to semantic regions; supports separate extraction of Hydrated and Dehydrated Xylem vessels
Note: All three post-processing steps are independent and can be run in parallel.
The main pipeline script (run_single_node_all_7classes.sh) uses the following key parameters:
torchrun --nproc_per_node=4 --nnodes=1 -m src.inference_v6 \
--patch-size 640 \
--batch-size 1 \
--confidence 0.1 \
--overlap-ratio 0.5 \
--prompts "Cortex" "Phloem Fibers" "Phloem" "Hydrated Xylem vessels" \
"Air-based Pith cells" "Water-based Pith cells" "Dehydrated Xylem vessels"torchrun --nproc_per_node=4 --nnodes=1 -m src.inference_dino_v1 \
--save-overlaypython -m src.combine_sam_dino_v3 \
--dilate-px 5 \
--save-extracted \
--dino-trust Cortex Phloem_Fibers Phloem Air-based_Pith_cells Water-based_Pith_cells--dino-trust specifies classes to read directly from DINO's semantic mask rather than filtering SAM3 predictions. Classes not in this list (e.g. Xylem) use SAM3 masks filtered by DINO.
results/
│
├── 0220_7classes_envtest_YYYYMMDD_HHMMSS/
│ │
│ ├── sam3/
│ │ ├── Cortex/
│ │ ├── Phloem/
│ │ ├── Phloem_Fibers/
│ │ ├── Air-based_Pith_cells/
│ │ ├── Water-based_Pith_cells/
│ │ ├── Hydrated_Xylem_vessels/
│ │ ├── Dehydrated_Xylem_vessels/
│ │ │ └── {stem}_mask.tif # Per-class binary masks
│ │ │
│ │ ├── combined/
│ │ │ └── {stem}_combined.tif # Multi-class combined mask (class IDs 1–7)
│ │ │
│ │ └── combined_overlay/
│ │ └── {stem}_overlay.tif # Color-coded visualization overlay
│ │
│ ├── dino/
│ │ ├── semantic_masks/
│ │ │ └── {stem}.tif # Pixel-wise class predictions
│ │ └── semantic_masks_overlay/
│ │ └── {stem}.tif
│ │
│ └── combined/
│ └── sam_dino/
│ ├── combined/
│ │ ├── masks/
│ │ │ └── {stem}_combined_mask.tif
│ │ └── overlays/
│ │ └── {stem}_overlay.tif
│ │
│ ├── Xylem_vessels/ # (with --save-extracted)
│ │ ├── images/
│ │ └── masks/
│ │
│ ├── Hydrated_Xylem_vessels/
│ │ ├── images/
│ │ └── masks/
│ │
│ └── Dehydrated_Xylem_vessels/
│ ├── images/
│ └── masks/
| Class ID | Class Name | Color (Overlay) |
|---|---|---|
| 0 | Background | Gray |
| 1 | Cortex | Blue |
| 2 | Phloem Fibers | Green |
| 3 | Phloem | Purple |
| 4 | Hydrated Xylem vessels | Red |
| 5 | Air-based Pith cells | Yellow |
| 6 | Water-based Pith cells | Orange |
| 7 | Dehydrated Xylem vessels | Teal |
Note: SAM3 prompts Hydrated and Dehydrated Xylem vessels as separate classes (combined IDs 4 and 7). DINO maps both to a single semantic class (ID 4). The
combine_sam_dino_v3step separates them by subtracting the dilated Dehydrated mask from the Hydrated prediction before applying the DINO spatial filter.
- Multi-GPU distributed inference with image-level sharding
- Patch-based processing with configurable overlap (default 50% in pipeline script)
- Max confidence stitching for overlap regions
- Text-prompted segmentation for multiple classes
- Batch processing with native SAM3 API
- Configurable confidence threshold per prompt
- Distributed semantic segmentation across 4 GPUs
- Fine-tuned model support via
lightly_train - Percentile-based image normalization (2nd–98th percentile)
- 16-bit / float TIFF input support
- Optional color overlay output
--dino-trust: Specified classes are taken directly from DINO semantic mask (no SAM3 mask required)- Non-trusted classes: SAM3 binary masks are filtered spatially using DINO semantic regions
- Xylem handling: Dehydrated mask is dilated by
--dilate-pxpixels before subtraction to isolate Hydrated vessels; both are filtered against DINO's xylem region (class 4) --save-extracted: Saves per-class masked images and binary masks underXylem_vessels/,Hydrated_Xylem_vessels/, andDehydrated_Xylem_vessels/- Multi-core CPU processing via Python
multiprocessing.Pool - Combined mask and color overlay saved per image
SYNAPS-AI: SYnergistic Neutron And Photon Science - Artificial Intelligence (SYNAPS-AI) Copyright (c) 2026, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved.
If you have questions about your rights to use or distribute this software, please contact Berkeley Lab's Intellectual Property Office at IPO@lbl.gov.
NOTICE. This Software was developed under funding from the U.S. Department of Energy and the U.S. Government consequently retains certain rights. As such, the U.S. Government has been granted for itself and others acting on its behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software to reproduce, distribute copies to the public, prepare derivative works, and perform publicly and display publicly, and to permit others to do so.
This software is released under Berkeley Lab's modified BSD license; see license.txt.