A napari plugin for interactive Segment Anything Model 3 (SAM3) segmentation using text, points, boxes, exemplar prompts, point-based refinement, and 3D/video-like propagation
Project description
napari-sam3-assistant
napari-sam3-assistant is a napari plugin for interactive Segment Anything Model 3 (SAM3) segmentation using text, points, boxes, exemplar prompts, point-based refinement, and 3D/video-like propagation.
The plugin focuses on task-based segmentation workflows:
- 2D segmentation with text, box, point, and mask-style prompts
- 3D stack / video-like propagation from prompts on a selected slice or frame
- exemplar segmentation from Shapes ROI boxes
- text-based concept segmentation
- refinement with positive and negative point prompts
SAM 3 is not bundled with this plugin. Install the SAM 3 backend and download the SAM 3 model files separately from Meta's Hugging Face repository.
Status
This project is under active development. The current widget supports local SAM 3 model loading, napari prompt collection, background execution, and writing results back to napari layers.
Changelog
2.0.0
Major update focused on SAM3.1 support, clearer model selection, live refinement, and result handling.
Added:
- SAM3.1 video multiplex support through
sam3.1_multiplex.pt. - Explicit
Model typeselector:SAM3.0 2D/3D/videoSAM3.1 video multiplex
- Model-type-aware validation so SAM3.0 and SAM3.1 folders are not confused.
- SAM3.1 routing for 3D/video propagation through the multiplex video predictor.
- Automatic task guidance for SAM3.1:
Load 2D Modelis disabled and the task is set to3D/video propagation. - Two-column step-based widget layout:
- left column:
Model Setup,Task,Layers - right column:
Prompt Tools,Run,Results, and collapsibleStatus
- left column:
- Muted professional widget theme for lower eye strain during long napari sessions.
- Compact activity indicator in
Runshowing model execution, propagation, refinement, idle, and failure states. - Results table showing
Layer,Prompt,Frame,Object ID,Score, andArea. - Detection threshold control for SAM3 grounding, useful when text prompts return no candidates.
- More visible text prompt input with
Enterbound toRun Preview. - Text prompt cleanup that sends short model-facing phrases such as
myelin ringinstead of instruction text such assegment all the myelin ring. - Automatic lower-threshold retry for text segmentation when the first pass returns zero objects.
- Batch mode for running the same 2D prompt setup across all open image layers, with separate preview and saved label layers per image.
- Multi-text batch mode: enter one text concept per line and run each prompt independently against the selected image or all image layers.
- Results actions:
Clear ResultsCopy ClipboardExport CSV
- Label-value merge controls for converting multiple label IDs into one class value.
- Stable object-ID label mapping for propagated video/stack results.
- Remembered model type, model directory, and device selection through Qt settings.
- Safer CUDA error reporting for unsupported GPU kernel architectures.
Live refinement improvements:
Refinement (live point correction)mode now arms live point refinement immediately afterCreate Prompt Layer.- The first point starts live refinement; the first run may take longer if the model is lazy-loaded.
SAM3 preview labelsis pre-created for live refinement so napari does not switch users away from the points layer after the first result.- After each live refinement update, the active layer returns to
SAM3 pointsin add mode. Next point modeaffects only future points.Ttoggles next point mode only and does not rerun refinement.Shift+Tflips selected point polarity, or the latest point if none is selected, and reruns refinement.- Existing dot colors now change only when stored point
properties["polarity"]changes. Apply mode to selected pointsstill edits selected existing points and reruns preview.
Changed:
Backend / Model Setupwas renamed toModel Setup.Napari Layerswas renamed toLayers.Batch all image layerscan be used independently from multi-text prompts.Load Image Modelwas renamed toLoad 2D Model.Lazy-load on runwas renamed toLoad model when running.- Text prompts no longer require a prompt layer.
- Text prompt submission can be run with
Enterwithout moving to theRun Previewbutton. - Preview clearing removes generated preview layers only and keeps prompts, saved labels, and loaded models.
pytorch_model.binwas removed from documented and validated model-file names.
Current model support:
- SAM3.0 weights support 2D image tasks and 3D/video propagation.
- SAM3.1
sam3.1_multiplex.ptsupports 3D/video propagation. - SAM3.1 is not currently routed through the plugin's 2D image model loader.
1.0.0
Initial SAM3 Assistant plugin foundation.
Added:
- Local SAM3 backend adapter for napari workflows.
- Task-based UI for:
- 2D segmentation
- 3D stack/video-like propagation
- exemplar segmentation
- text segmentation
- refinement with positive and negative prompts
- Prompt collection from napari Points, Shapes, Labels, and text input.
- Box prompts from Shapes layers.
- Labels-layer mask prompts.
- Text prompts for concept segmentation.
- Preview outputs as napari Labels, Image, and Shapes layers.
- Saved label outputs through
Save Result as Labels. - Background worker execution to keep the napari UI responsive.
- Channel-axis handling for grayscale, RGB/RGBA, channel-first, and stack-like data.
- Basic model-directory validation for local SAM3 files.
Requirements
- Python
>=3.12 - napari
>=0.5 - SAM 3 Python package importable as
sam3 - PyTorch and torchvision installed for your platform
- A local SAM 3 checkpoint directory containing:
config.jsonprocessor_config.json- one weight file such as
sam3.pt,model.safetensors, orsam3.1_multiplex.pt
GPU use requires a PyTorch / torchvision / SAM 3 stack that is compatible with your GPU architecture. If CUDA kernels are not available for the device, select CPU in the widget.
Setup
Setup has three parts:
- Install the SAM 3 backend.
- Download the SAM 3 model files from Hugging Face and configure the model path.
- Install this napari plugin.
1. Install SAM 3
Create and activate an environment:
conda create -n napari-sam3 python=3.12
conda activate napari-sam3
Install PyTorch and torchvision for your platform. Use the official PyTorch selector for the current command:
pip install torch torchvision
Install SAM 3:
git clone https://github.com/facebookresearch/sam3.git
cd sam3
pip install -e .
2. Download SAM 3 Weights
This plugin does not ship with SAM 3 weights or model configuration files. Download them from the official Hugging Face repository:
https://huggingface.co/facebook/sam3
SAM3.1 multiplex weights are available from:
https://huggingface.co/facebook/sam3.1
These repositories are gated.
- Sign in to your Hugging Face account.
- Open the facebook/sam3 model page.
- Request or accept access to the repository.
- Once access is approved, open the Files and versions tab.
- Download the required model and configuration files directly from the website.
A reference screenshot of the file list is shown below:
Expected model directory:
~/models/sam3/
config.json
processor_config.json
sam3.pt
model.safetensors is also supported as a SAM3 3.0 weight file.
For SAM3.1 video multiplex, use:
~/models/sam3.1/
config.json
processor_config.json
sam3.1_multiplex.pt
Depending on the Hugging Face layout, the directory may also contain tokenizer files such as tokenizer.json, vocab.json, and merges.txt.
Current model support:
- SAM3 3.0 weights: 2D image tasks and 3D/video propagation.
- SAM3.1
sam3.1_multiplex.pt: 3D/video propagation through the SAM3.1 multiplex video predictor. - SAM3.1 is not currently routed through the plugin's 2D image model loader.
Keep each model version in its own directory. In the plugin widget, first choose Model type, then click Browse, select the matching directory, and click Validate.
Example local folders:
~/Projects/napari/sam3/model -> choose SAM3.0 2D/3D/video
~/Projects/napari/sam3/model3_1 -> choose SAM3.1 video multiplex
Note that the upstream SAM 3 source repository does not ship with a model/ folder by default. In this project, sam3/model and sam3/model3_1 are user-created local directories used to store downloaded SAM 3 weights and configuration files.
The selected model type, model directory, and device are remembered by the widget.
3. Install napari-sam3-assistant
Install this plugin:
git clone https://github.com/wulinteousa2-hash/napari-sam3-assistant
cd napari-sam3-assistant
pip install -e .
Start napari:
napari
Open the widget from:
Plugins > SAM3 Assistant
Basic Workflow
- Open an image in napari.
- Open
Plugins > SAM3 Assistant. - Select the image in
Layers > Image. - Select a task.
- Create a prompt layer if the task needs one.
- Click
Run Preview. - Inspect
SAM3 preview labels,SAM3 preview masks, orSAM3 preview boxes. - Click
Save Result as Labelsto keep the result.
Use Clear Preview to remove generated preview layers without deleting prompts or saved labels.
Batch Multiple 2D Images
Use Batch all image layers when several open 2D images should receive the same prompt setup.
Workflow:
- Open multiple images in napari.
- Configure a 2D task such as text, box, exemplar, or labels-mask segmentation.
- Add the prompt once.
- Enable
Batch all image layersinStep 3. Layers. - Click
Run Preview.
Each source image gets its own output layers:
SAM3 preview labels [image name]
SAM3 preview masks [image name]
SAM3 preview boxes [image name]
Save Result as Labels saves each batch preview labels layer separately:
SAM3 saved labels [image name]
Batch mode is intended for 2D image tasks. It is disabled for live refinement and 3D/video propagation because those workflows depend on one active image/session.
Multi-Text Batch Mode
Use Batch text prompts when you want each text concept to run independently instead of writing one combined phrase such as cat and dog.
Workflow:
- Set
TasktoText segmentation. - Enter one concept per line in
Batch text prompts:
cat
dog
person
- Leave
Batch all image layersoff to run all prompts on the selected image only. - Enable
Batch all image layersto run every prompt on every open image. - Click
Run Preview.
Outputs include both image and prompt:
SAM3 preview labels [Image 1 - cat]
SAM3 preview labels [Image 1 - dog]
SAM3 preview labels [Image 2 - cat]
SAM3 preview labels [Image 2 - dog]
The Results table includes a Prompt column. Object IDs are scoped to each image-prompt result, so Object ID 1 for Image 1 - cat is separate from Object ID 1 for Image 1 - dog.
Tasks
Text Segmentation
Use text to segment all matching instances of a concept.
Workflow:
- Set
TasktoText segmentation. - Leave
Prompt typeasText only. - Enter a short phrase, for example:
cell
nucleus
myelin
myelin sheath
- Keep
Detection thresholdnear the default0.35, or lower it if the result is empty. - Press
Enterin the text prompt field or clickRun Preview.
No prompt layer is needed for text segmentation. Create Prompt Layer is not required.
Text prompts usually work better as short noun phrases than instructions. Prefer myelin sheath over segment all the myelin rings. The plugin strips common instruction prefixes before sending the prompt to SAM3, but microscopy-specific language can still be difficult for the model.
If the result says objects=0, SAM3 ran but did not return masks above threshold. Try a shorter noun phrase, lower Detection threshold, or use a box/exemplar prompt for structures that are visually clear but not well recognized by text.
2D Segmentation With Boxes
Use boxes to identify the target object or concept.
Workflow:
- Set
Taskto2D segmentation. - Set
Prompt typetoBox. - Click
Create Prompt Layer. - Draw one or more rectangles in the
SAM3 boxesShapes layer. - Click
Run Preview.
The output appears in preview layers.
Exemplar Segmentation
Use example ROIs to segment similar objects.
Workflow:
- Set
TasktoExemplar segmentation. - Set
Prompt typetoBox. - Click
Create Prompt Layer. - Draw boxes around one or more example objects.
- Click
Run Preview.
The local SAM 3 image API exposes visual exemplars through geometric box prompts. The plugin stores ROI metadata, but inference currently passes exemplar ROIs as SAM 3 visual box prompts.
Refinement With Positive and Negative Points
Use points to correct a result.
Workflow:
- Set
TasktoRefinement (live point correction). - Set
Prompt typetoPoints (positive/negative). - Click
Create Prompt Layer. - Choose
Positiveand add points on regions to include. - Choose
Negativeand add points on regions to exclude. - Click
Run Preview.
This is useful after a text, box, or exemplar preview is close but not correct.
Labels Mask Prompt
Use a napari Labels layer as a mask-style prompt.
Workflow:
- Set a task that supports mask prompts.
- Set
Prompt typetoLabels mask. - Click
Create Prompt Layer. - Paint non-zero pixels in
SAM3 mask prompt. - Click
Run Preview.
3D Stack / Video Propagation
Treat a stack as video-like data and propagate a prompt through frames or slices.
Workflow:
- Open a stack in napari.
- Set
Taskto3D/video propagation. - Select the target frame or slice in napari.
- Create a prompt layer and add prompts on that frame.
- Choose propagation direction:
bothforwardbackward
- Click
Run PrevieworPropagate Stack/Video.
Preview output is written to:
SAM3 propagated preview labels
Saved output is written to:
SAM3 saved propagated labels
The current SAM 3 video predictor backend is CUDA-only. CPU mode is supported for 2D/image workflows, not 3D/video propagation.
Channel Axis
Channel axis tells the plugin which data axis is color/channel.
Default:
-1
Use -1 for grayscale images and normal RGB/RGBA images. The plugin auto-detects trailing RGB/RGBA axes of size 3 or 4.
Examples:
(H, W) -> -1
(H, W, 3) -> -1
(H, W, 4) -> -1
(Z, H, W) -> -1
(C, H, W) -> 0
(Z, C, H, W) -> 1
(T, C, H, W) -> 1
(Z, H, W, C) -> 3
Leave it at -1 unless your image has an explicit multi-channel microscopy dimension.
Output Layers
Preview layers:
SAM3 preview labels
SAM3 preview masks
SAM3 preview boxes
SAM3 propagated preview labels
Saved layers:
SAM3 saved labels
SAM3 saved propagated labels
Buttons:
Validate: check the selected SAM 3 model directory.Load 2D Model: load the 2D/image model.Load 3D/Video Model: load the video propagation model.Run Preview: run the selected task.Clear Preview: remove generated preview layers only.Save Result as Labels: copy preview labels into saved labels.Cancel: stop a running worker.Unload: unload the SAM3 model from memory.
Results table:
Layer | Prompt | Frame | Object ID | Score | Area
Layer: source image layer.Prompt: text prompt used for text and multi-text results. For non-text workflows this is-.Frame: propagated frame or slice index. For 2D results this is-.Object ID: SAM3 object ID when available, otherwise a generated label ID.Score: SAM3 confidence/probability when returned by the backend.Area: number of mask pixels for that object in the displayed 2D plane or frame.
Results actions:
Clear Results: clear the table only.Copy Clipboard: copy tab-separated results, including headers, for pasting into Excel or statistics software.Export CSV: save the results table to a CSV file.
Label-value merge:
Use this when multiple SAM3 objects should become the same class value in a Labels layer. For example, if labels 3, 4, 5, and 6 are all the same biological class, set:
Values to replace: 3,4,5,6
New value: 3
Then click Merge Label Values. The selected Labels layer is updated in place.
ARM64, CUDA, and DGX Spark
For ARM64 systems such as NVIDIA DGX Spark / GB10:
- Use Python 3.12 or newer.
- Keep the NVIDIA driver and CUDA stack current.
- Install a PyTorch/torchvision build that supports your GPU architecture.
- Use
CPUmode for reliable 2D execution if CUDA kernels are unavailable. - Use explicit
CUDAonly when testing a compatible GPU build.
Check PyTorch GPU support:
python - <<'PY'
import torch
print("torch:", torch.__version__)
print("torch cuda runtime:", torch.version.cuda)
print("cuda available:", torch.cuda.is_available())
if torch.cuda.is_available():
print("device:", torch.cuda.get_device_name(0))
print("capability:", torch.cuda.get_device_capability(0))
print("arch list:", torch.cuda.get_arch_list())
PY
GB10 reports compute capability 12.1 (sm_121). If your PyTorch build does not include compatible kernels, you may see:
CUDA error: no kernel image is available for execution on the device
nvrtc: error: invalid value for --gpu-architecture
The plugin does not compile PyTorch, torchvision, or SAM 3 CUDA extensions.
Troubleshooting
No mask appears and status says objects=0
SAM 3 returned no detections above threshold. Try:
- a shorter text prompt
- a more common concept phrase
- a lower
Detection threshold - box or exemplar prompts
- CPU mode if the CUDA path is unstable
CUDA kernel image error
Error:
CUDA error: no kernel image is available for execution on the device
The GPU is visible, but at least one required CUDA kernel was not built for the device architecture. Use CPU, or install compatible PyTorch/torchvision/SAM 3 builds.
Invalid GPU architecture
Error:
nvrtc: error: invalid value for --gpu-architecture
The installed PyTorch CUDA runtime cannot compile for the detected GPU. Use CPU or install a build that supports the GPU.
BFloat16 conversion errors
The plugin converts SAM3 bfloat16 outputs to float32 before writing NumPy-backed napari layers. If you still see dtype errors, restart napari after changing device mode and run again.
Text prompt creates no layer
That is expected. Text segmentation does not need a prompt layer. Enter text and click Run Preview.
Development
Install in editable mode:
pip install -e .
Run tests:
PYTHONPATH=src pytest -q
The test suite covers coordinate mapping, prompt collection, adapter utility behavior, and static widget UI checks. It does not download SAM 3 weights.
References
- SAM 3 repository: https://github.com/facebookresearch/sam3
- SAM 3 model files: https://huggingface.co/facebook/sam3
- PyTorch installation selector: https://pytorch.org/get-started/locally/
Acknowledgement
The demo image was provided by the Electron Microscopy Core Facility at Houston Methodist Research Institute
License
MIT. See the project license file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file napari_sam3_assistant-2.0.0.tar.gz.
File metadata
- Download URL: napari_sam3_assistant-2.0.0.tar.gz
- Upload date:
- Size: 50.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04a552bb0b22d3ad1ccfab32b31b91ff55c43f354650c75a5e64c919d0ebb7ae
|
|
| MD5 |
471f6abfeda1009298484810fae749a7
|
|
| BLAKE2b-256 |
d2eb5a6b23186eb69af0a7ecfd6bd763e2f87e036c4344854494291f886580a3
|
File details
Details for the file napari_sam3_assistant-2.0.0-py3-none-any.whl.
File metadata
- Download URL: napari_sam3_assistant-2.0.0-py3-none-any.whl
- Upload date:
- Size: 44.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54dec232a1b7f42c8b3c477710f945467745100b805f95898deaf9127c21fd02
|
|
| MD5 |
92ae954e3b8b08a8dc95eccb8c1b9853
|
|
| BLAKE2b-256 |
0d4f6fcdbb510065bcd7563268325b33a0c300bdc05ecedfe8aa4bec3b678183
|