a napari plugin for TIFF-based 2D and 3D U-Net segmentation workflows.
Project description
napari-unet-assistant
napari-unet-assistant is a napari plugin for supervised 2D and 3D U-Net segmentation workflows.
It is designed for users who already have image-mask training data and want to pair files, train a U-Net model, run inference, and inspect results directly inside napari.
This plugin is separate from SAM-based annotation workflows. Its focus is conventional supervised U-Net training from existing image-mask pairs.
What's new in 0.6.4
- Added recursive dataset-folder auto pairing for TIFF datasets with
images/masks,raw/labels, mixed-folder, and nested layouts. - Added folder-name role hints so dataset folders can guide image-mask pairing even when filenames are less explicit.
- Added configurable augmentation presets:
none,conservative,balanced, andstrong. - Added custom augmentation controls for flips, rotation, shear, scale, brightness jitter, and Gaussian noise.
- Added model-capacity options for standard, large, and xlarge U-Net widths.
- Added a model registry with built-in U-Net, MONAI, nnU-Net, and segmentation-models-pytorch backend hooks.
- Added separate model-family, backbone/encoder, and encoder-weight controls for clearer U-Net variant testing.
- Added a Model Sweep tab for running multiple backend, model-family, backbone, weight, and capacity combinations sequentially.
- Added a dedicated nnU-Net pipeline path that exports training pairs, runs nnU-Net v2 CLI training, and imports prediction TIFFs.
- Added early stopping with one learning-rate reduction on validation plateau before stopping after a second plateau.
- Added a Training Monitor tab with structured run, pair, metric, and output-path summaries.
- Changed single new training runs to create timestamped child run folders under the selected output parent so repeated runs do not overwrite history.
- Improved resume metadata loading so epochs, batch size, validation split, and early-stopping controls are restored as editable defaults.
- Clarified model selection wording with user-facing labels: model source, architecture, feature extractor, pretrained weights, and model size.
- Made Model Sweep write completed/failed result rows immediately from the worker thread so
sweep_results.csvstays current through long sweeps. - Added multiclass mask label-range validation before training starts.
- Added saving and loading of augmentation settings in run configuration metadata.
- Added a training stop button that cancels after the current batch, discards the interrupted model state, and clears GPU cache when available.
- Added a U-Net architecture preview for the selected 2D/3D mode and output-channel configuration.
- Added short tooltips across the main controls to explain settings in place.
- Improved the training UI layout with clearer tabs and more stable dock-widget sizing.
Highlights
- TIFF-first 2D and 3D U-Net training
- Binary and multiclass segmentation
- Clear binary output-channel wording: binary uses one foreground channel while background remains label 0
- Recursive smart image-mask pairing
- Pair review with confidence, reason, and shape-check status
- Patch-based training with configurable patch size and overlap
- Optional empty-mask patch inclusion
- Configurable augmentation presets and custom augmentation controls
- Standard, large, and xlarge U-Net capacity options
- Optional model backends for MONAI, nnU-Net, and segmentation-models-pytorch
- Model Sweep for batch training and validation comparison across model choices
- Training Monitor for structured current-run status, latest/best metrics, and output paths
- 80/20 validation split
- Continue training from a previous run
- Training cancellation from the UI
- Single-image and folder inference
- 2D image and 3D volume prediction
- TIFF prediction export
- napari-based visualization and QC
Smart image-mask pairing
The plugin can pair training data from:
- a dataset root scanned recursively
- separate image and mask folders
- one mixed folder containing both images and masks
- a manual CSV file
Dataset-root auto scan supports common layouts such as:
images/+masks/raw/+labels/- nested TIFF folders under one dataset root
- one mixed folder containing
sample.tif+sample_mask.tif
Supported naming patterns include:
sample.tif+sample_mask.tifsample_1.tif+sample_2.tifsample_image.tif+sample_mask.tifsample_raw.tif+sample_label.tif
After scanning, the plugin shows each proposed pair with confidence, reason, and shape-check status. Ambiguous or invalid pairs are reported instead of being silently used for training.
Folder names such as images, raw, masks, and labels can also provide role hints during recursive dataset scans.
Augmentation
Training supports augmentation presets and custom controls:
none: no augmentationconservative: flips, small rotations/scales, and light brightness jitterbalanced: stronger rotation/scale, shear, brightness jitter, and light Gaussian noisestrong: wider rotation/scale/shear ranges, stronger brightness jitter, and stronger Gaussian noisecustom: user-selected flips, rotation, shear, scale, brightness, and noise settings
The selected augmentation configuration is saved in each run folder's config.json and restored when loading run metadata.
Training controls
The training panel includes a stop button for cancelling an active training run. Cancellation is checked between batches, so the current batch may finish before the run stops.
When a run is stopped, the interrupted model state is discarded and GPU cache is cleared when available.
The validation tab also includes early stopping controls. When enabled, the plugin monitors validation Dice. If validation Dice does not improve by at least the configured minimum gain for the patience window, the learning rate is reduced once. If validation Dice still does not improve for another patience window after that reduction, training stops early and keeps the best validation-Dice checkpoint. Validation uses augmentation disabled, even when training augmentation is enabled. When at least two image-mask pairs are available, validation is split by image/mask pair so training and validation patches come from different source images. Single-pair datasets fall back to patch-level validation and record that fallback in validation.json. The per-epoch history.csv records learning_rate, lr_reduced, and early_stopped, and validation.json records split level plus augmentation state.
When loading resume metadata, the plugin restores architecture/data settings for compatibility and also restores editable schedule controls such as epochs, batch size, validation split, and early-stopping settings. In continue-training mode, the restored epoch value is the default number of additional epochs to run, not a total lifetime epoch target.
Training Monitor
The Monitor tab in Training Options gives an organized current-run view instead of another scrolling log. It shows the active run configuration, pair-scan summary, latest validation metrics, best validation metrics, and important output paths such as config.json, history.csv, summary.json, best_model.pt, and prediction folders.
This is different from Step 4 Results: Step 4 remains the chronological message log for detailed events, warnings, pair reports, and command output. The Monitor tab is a compact dashboard that overwrites the same fields as training progresses, so users can leave training running and come back to the current state quickly. It also includes lightweight actions to preview one selected/first OK image-mask pair in napari and to reuse the current run folder in the Inference section.
Model size
Training can use standard, large, or xlarge U-Net widths. For 2D models, these use base channel widths of 32, 64, and 128. Larger models can learn more complex boundaries, but they need more GPU memory and may require a smaller batch size.
Model sources
The default model source is the built-in U-Net and works without extra model packages. MONAI, segmentation-models-pytorch, and nnU-Net are optional backends, so they must be installed into the same Python environment that runs napari. Prefer python -m pip from that environment so the package is installed where napari can import it.
If you install a release of napari-unet-assistant that declares the optional extras, you can use:
python -m pip install napari-unet-assistant[monai]
python -m pip install napari-unet-assistant[smp]
python -m pip install napari-unet-assistant[nnunet]
python -m pip install napari-unet-assistant[models]
Use models to install all optional model backends at once. If your shell treats square brackets specially, quote the requirement:
python -m pip install "napari-unet-assistant[models]"
If the extras command does not install the backend package, install the backend dependency directly:
python -m pip install monai
python -m pip install segmentation-models-pytorch
python -m pip install nnunetv2
For a development checkout, install the extras from the repository root:
python -m pip install -e ".[models]"
For the GitHub version, put the extras before the @ URL:
python -m pip install "napari-unet-assistant[models] @ git+https://github.com/wulinteousa2-hash/napari-unet-assistant.git"
You can verify that the optional packages are available with:
python -c "import monai; import segmentation_models_pytorch; import nnunetv2; print('optional model backends ok')"
If this import check fails, napari will still load the built-in U-Net backend, but the missing optional model source will not be usable until its package is installed in the active environment. After installing a source, restart napari and select it from the Model source dropdown.
The model registry lives under src/napari_unet_assistant/models/ and separates provider code into providers/. MONAI and segmentation-models-pytorch models are regular torch.nn.Module backends. nnU-Net uses a dedicated pipeline workflow because nnU-Net manages its own data conversion, planning, preprocessing, training, and prediction commands.
Model selection is split into:
Model source: the library or workflow provider, such as built-in plugin models, MONAI, SMP, or the separate nnU-Net pipeline. This is the ML/backend term in plainer UI wording.Architecture: the model family inside that source, such as U-Net, U-Net++, SegResNet, or DeepLabV3+.Feature extractor: the encoder/backbone used by encoder-style models when supported, such as ResNet34, ResNet50, EfficientNet-B0, DenseNet121, or MobileNetV2.Pretrained weights: optional starting weights for the feature extractor, such as ImageNet weights for supported 2D SMP encoders.Model size: width/depth capacity preset used by sources that expose size controls.
Model Sweep
Use the Model Sweep tab when you want to compare multiple model configurations without starting each run manually. Set the shared training constants first, including data shape, task type, patch settings, augmentation, validation split, epochs, and batch size. Then check the model sources and model sizes to test, click Preview model sweep, and click Run model sweep. The regular Model source, Architecture, and Feature extractor controls in the Run tab are for single training runs; Model Sweep expands checked sources into valid architectures, feature extractors, and pretrained weights automatically.
The sweep runs configurations sequentially so one GPU is used predictably. Each completed configuration gets its own run folder with the usual files:
config.jsonrun_summary.txtvalidation.jsonhistory.csvsummary.jsonbest_model.pt
The sweep folder also writes:
sweep_config.json: shared settings, augmentation, selected configurations, and skipped entriessweep_results.csv: one summary row per skipped, failed, or completed configurationpairs.csv: image-mask pairs used by the sweep
The sweep_results.csv table includes source/backend, architecture/model family, model size, feature extractor/encoder, pretrained weights, best validation loss, best validation Dice/IoU/F1, run folder, and any error message. Missing optional backend packages are reported as skipped during preview/run. Failed combinations do not stop the sweep; they are recorded and the next configuration runs. Completed and failed rows are written from the sweep worker as each configuration ends, so partial results stay current even during long runs. The stop button cancels after the current batch or epoch boundary and leaves partial results on disk.
nnU-Net is not trained through Model Sweep because it uses its own data-conversion, planning, training, and prediction pipeline rather than the plugin's direct torch.nn.Module training loop. To train nnU-Net, select nnunet in the Run tab and start a normal training run. The plugin exports the paired TIFF data to a self-contained nnU-Net work folder, runs nnUNetv2_plan_and_preprocess, runs nnUNetv2_train, and saves nnunet_workflow.json so the Inference panel can run nnUNetv2_predict and convert predictions back to TIFF.
Manual CSV pairing
For manual pairing, provide a CSV file with one image-mask pair per row.
Required columns:
image_pathmask_path
Optional column:
key
Example:
key,image_path,mask_path
sample01,/path/to/images/sample01.tif,/path/to/masks/sample01_mask.tif
sample02,/path/to/images/sample02.tif,/path/to/masks/sample02_mask.tif
Use absolute paths for the clearest behavior. Relative paths are interpreted from the current working directory.
Each image and mask should have matching spatial dimensions.
Installation
pip install git+https://github.com/wulinteousa2-hash/napari-unet-assistant.git
For editable development:
git clone https://github.com/wulinteousa2-hash/napari-unet-assistant.git
cd napari-unet-assistant
pip install -e .
napari
Basic workflow
- Open napari.
- Launch U-Net Assistant.
- Choose a pairing mode.
- Scan and review image-mask pairs.
- Set training options.
- Train a 2D or 3D U-Net model.
- Load a saved run folder.
- Run inference on new images or volumes.
- Review prediction masks in napari.
Binary vs multiclass outputs
For binary segmentation, masks still use 0 = background and nonzero/1 = foreground, but the model uses one output channel: foreground probability. Background is implicit when foreground probability is below the threshold.
For multiclass segmentation, enter the total number of labels including background. For example, three classes means 0 = background, 1 = class 1, and 2 = class 2.
Supported data
2D training
- image:
(Y, X)grayscale TIFF - mask:
(Y, X)label TIFF - binary masks:
0 = background, nonzero = foreground - multiclass masks: integer labels from
0throughnum_classes - 1; training checks this before starting
3D training
- image:
(Z, Y, X)grayscale TIFF - mask:
(Z, Y, X)label TIFF - multiclass masks should use integer labels from
0throughnum_classes - 1; training checks this before starting:0 = background1 = class 12 = class 23 = class 3
Patch options
2D
XY patch sizes:
- 64
- 128
- 256
- 512
- 1024
Default: 256 x 256
3D
Z patch sizes:
- 8
- 16
- 32
- 64
XY patch sizes:
- 64
- 128
- 256
- 512
- 1024
Default: 16 x 256 x 256
Validation
The current training workflow uses a standard train/validation split. The default validation split is 20%.
K-fold cross-validation is not active in this release.
Each run writes validation.json with the active validation mode, split fraction, random seed, total patch count, train patch count, and validation patch count. The training log also reports the same split. Per-epoch validation metrics are written to history.csv.
At training start, the log reports the model source, architecture, feature extractor, pretrained weights, model size, patch settings, augmentation preset, validation setup, epochs, and batch size. The same user-readable summary is written to run_summary.txt, while full structured settings are saved in config.json.
Output History
The Run output field is treated as a parent folder for new training runs. Each new single training run creates a timestamped child run folder so repeated one-by-one experiments do not overwrite earlier history.csv, summary.json, or best_model.pt files.
Example:
my_training_outputs/
run_20260520_143000_builtin_unet_large_plain_none/
run_20260520_151500_monai_unet_large_plain_none/
run_20260520_163000_smp_unetplusplus_large_resnet34_imagenet/
run_20260520_174500_nnunet_nnunetv2_dataset600_2d_fold0/
Model Sweep already uses a parent folder with child run folders under sweep_YYYYMMDD_HHMMSS/. Continue-training workflows still target a specific compatible run folder instead of creating a new sibling.
Outputs
Each run folder can contain:
best_model.ptconfig.jsonrun_summary.txtsummary.jsonhistory.csvvalidation.jsonpairs.csv- prediction TIFF outputs
Current scope
This release is focused on TIFF-based supervised U-Net training and inference.
OME-Zarr, spectral/lambda workflows, and SAM-assisted annotation are intentionally outside the scope of this version.
Reference
This project builds on U-Net-based nerve morphometry workflows described in:
Moiseev D, Hu B, Li J. Morphometric Analysis of Peripheral Myelinated Nerve Fibers through Deep Learning. Journal of the Peripheral Nervous System. 2019;24(1):87-93.
https://pmc.ncbi.nlm.nih.gov/articles/PMC6420354/
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_unet_assistant-0.6.4.tar.gz.
File metadata
- Download URL: napari_unet_assistant-0.6.4.tar.gz
- Upload date:
- Size: 70.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
009b4bcf3515c305136f16fa3d9c1869346530f28d1a501894288e708b83ad2b
|
|
| MD5 |
e3d49955c8b7708ea49d1056f0a963e3
|
|
| BLAKE2b-256 |
b0676ef56d93c24471973c4c9fafe83a915a258b7881c2fd397a4356411d3bce
|
File details
Details for the file napari_unet_assistant-0.6.4-py3-none-any.whl.
File metadata
- Download URL: napari_unet_assistant-0.6.4-py3-none-any.whl
- Upload date:
- Size: 73.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f0b2236688c8f48d61891877d322a8753bd4d7b003d93d10e0f50de9b936775
|
|
| MD5 |
07cfd1acea6d7378d56299c650b334b0
|
|
| BLAKE2b-256 |
34d9312e43938310baa5941626e2523be84946c874e170ac6f1ac6b7dcd2b958
|