Skip to main content

nnunet-lite

Run nnU-Net models without nnU-Net.

nnunet-lite converts a trained nnU-Net model into a small, self-contained bundle (bundle.json + weights.safetensors) and provides an inference API that reproduces nnU-Net's preprocessing, sliding-window prediction and resampling. Segmentations match nnU-Net within a small tolerance, with no nnunetv2 dependency at inference time.

It is a library for conversion and inference, not a serving platform: it makes no attempt at cloud deployment, queuing or orchestration.

Why

  • Independent of nnU-Net. A bundle needs only PyTorch, NumPy, SciPy, nibabel and safetensors.
  • Tensor in, tensor out. The network is a plain torch.nn.Module. Predictions accept paths, bytes, file objects, NIfTI images or raw arrays, so nothing has to touch the disk.
  • Safe to load. Weights are stored as safetensors and checked against a SHA-256 recorded in bundle.json. Conversion reads nnU-Net checkpoints with weights_only=True and refuses anything else in the pickle.
  • Fast preprocessing. An optional GPU path is roughly 12–40x faster than nnU-Net's CPU preprocessing, with an exact CPU path when you need bit-equality.

Installation

The project uses uv. Python 3.11+ is required.

git clone <repo-url> && cd nnunet_lite
uv sync

Quick start

import nnunet_lite as nl

# 1. Convert once. Point at the nnU-Net trainer__plans__configuration folder.
nl.convert(
    "nnUNet_results/Dataset485_x/nnUNetTrainer__nnUNetPlans__3d_fullres",
    output_dir="bundles/dataset485",
)

# 2. Predict anywhere, with no nnU-Net files or dependencies.
predictor = nl.Predictor("bundles/dataset485")  # CUDA if available
result = predictor.predict("ct.nii.gz", qc=True)

result.segmentation  # CPU uint8 tensor on the input's grid
result.qc["muscle"].estimated_dice  # optional per-label quality control
result.save("seg.nii.gz")

Capabilities

Conversion

bundle = nl.convert(model_dir)  # convert in memory
bundle = nl.convert(model_dir, output_dir="bundles/x")  # convert and save
bundle.save("bundles/x")  # or save later
bundle = nl.load_bundle("bundles/x", device="cuda")  # load, verifying weights
config = nl.load_config("bundles/x")  # bundle.json only, no weights

convert takes fold, checkpoint, overwrite, device, and an inference override for the sliding-window settings (default: nnU-Net's own).

Preprocessing

nl.preprocess(volume, spacing, config.preprocessing) runs nnU-Net's pipeline: transpose, crop to nonzero, normalize, resample. It needs only the config, not the weights.

CPU (device="cpu") GPU (default when CUDA is available)
Match to nnU-Net Byte-equal to DefaultPreprocessor.run_case_npy Within float32 rounding
Max difference from CPU path — ~5e-7 on normalized data
Voxels bit-identical to CPU 100% ~93%
Crop box and shapes nnU-Net's Identical to CPU

The GPU path applies spline resampling as one float64 matrix product per axis, using matrices from the same scipy call the CPU path makes. If it fails for any reason (out of memory, CUDA error, non-finite input) it emits a RuntimeWarning and falls back to the exact CPU path.

Measured on an RTX 2080 Ti, resampling to 1.5 mm:

Input CPU GPU Speedup Peak GPU memory
300×512×512 at 0.8 mm 8.76 s 0.30 s 29x 1.3 GB
80×512×512, 5 mm slices 2.32 s 0.11 s 22x 0.6 GB
600×512×512 at 1×0.7 mm 17.68 s 0.45 s 39x 3.6 GB
2-channel MRI 150×240×240, masked z-score 2.82 s 0.24 s 12x 0.3 GB

Use the default for throughput; use device="cpu" when you need exactly what nnU-Net would produce, for example when validating a conversion. The effect of GPU rounding on final segmentations has not been measured yet.

Prediction

predictor.predict("ct.nii.gz")  # path
predictor.predict(s3_body_bytes)  # bytes or binary file object, nothing written to disk
predictor.predict(volume, spacing)  # raw (C, X, Y, Z) array or tensor plus spacing
predictor.predict(source, qc=["muscle"], mirror=True)
  • Preprocessing always runs inside predict using the bundle's own configuration. There is no entry point for pre-preprocessed tensors.
  • Sliding window is a port of nnUNetPredictor: tile positions, Gaussian weighting, zero padding, float16 accumulation and CUDA autocast. On CUDA out-of-memory it warns and moves accumulation to the CPU. 2D bundles run slice by slice.
  • Restoring resamples logits to the cropped input shape, argmaxes, un-crops and transposes back, one class at a time so full-resolution logits for all classes are never held at once.
  • Mirroring (test-time augmentation) is off by default, unlike nnU-Net. Pass mirror=True to enable it.
  • Output: result.save(path) and result.to_bytes() write NIfTI on the input file's own grid and orientation, using the smallest unsigned integer type that holds the model's highest label. These need a file input, since array inputs carry no affine.
  • Inputs: .nii and .nii.gz are both accepted, including from memory, via nnU-Net's NibabelIO and NibabelIOWithReorient readers. Other readers work for arrays only.

Quality control

qc=True (every foreground label) or a list of label names or ids adds per-label statistics. QC never changes the segmentation and costs nothing unless requested.

  • Segmentation stats (26-connected components): voxel_count, component_count, largest_component_fraction, small_component_voxel_counts, touches_boundary.
  • Confidence stats (softmax at network resolution): estimated_dice, mean_margin, low_margin_fraction, mean_entropy, network_voxel_count.

estimated_dice is uncalibrated: it is the raw confidence-based estimate at temperature 1. Per-model temperatures and calibration against true Dice are not implemented yet, so treat it as a relative signal, not a Dice value.

Multiple models on one input

multi = nl.MultiPredictor({"299": "bundles/dataset299", "485": "bundles/dataset485"})
results = multi.predict("ct.nii.gz", qc=True)  # dict[str, PredictionResult]

The input is parsed once and preprocessed once per distinct preprocessing config. All networks stay on the device; models run one after another.

Constraints

Models

  • Architectures: PlainConvUNet and ResidualEncoderUNet, 2D and 3D, with InstanceNorm, LeakyReLU or ReLU, and no dropout. Legacy and architecture-style plans, including inherits_from, are supported.
  • One fold per bundle. No ensembling across folds.
  • Not supported (raises NotImplementedError): cascade configurations, region-based labels, the ignore label.

Preprocessing

  • Normalization: CTNormalization, ZScoreNormalization (with and without mask), RescaleTo01Normalization, RGBTo01Normalization, NoNormalization.
  • Resampling: only nnU-Net's default, resample_data_or_seg_to_shape. Others raise NotImplementedError.
  • Array inputs must use the axis and spacing order nnU-Net's reader produced for the model, recorded as config.preprocessing.image_reader. For example, the nibabel readers give (z, y, x) with reversed spacing. 2D images need a singleton first spatial axis, as in nnU-Net.
  • Masked z-score still computes its hole-filled mask on the CPU, even on the GPU path.

Scope

  • The target is nnU-Net-quality output within a low tolerance, not bitwise-identical segmentations. Network logits are tested bitwise-equal to nnU-Net's own network on CPU FP32 and CUDA autocast.
  • Bundles record the sliding-window defaults (tile_step_size=0.5, Gaussian weighting) because nnU-Net keeps them outside its plans. Override them at conversion time if you need different values.
  • Schema 1 bundles still load; saving always writes schema 2.

Development

uv run pytest            # synthetic nnU-Net folders, no nnunetv2 needed
uv run ruff check . && uv run ruff format .
uv run mypy

Equivalence tests compare against nnU-Net itself and need nnunetv2, so they run in the leadup environment:

PYTHONPATH=src packages/nnunet_model_conversion/.venv/bin/python -m pytest tests
  • tests/test_nnunet_equivalence.py: bitwise-equal logits (needs real model folders).
  • tests/test_nnunet_preprocessing_equivalence.py: byte-equal preprocessing against DefaultPreprocessor.
  • tests/test_nnunet_prediction_equivalence.py: whole-CT segmentations against nnUNetPredictor (needs CUDA and a test CT via NNUNET_LITE_TEST_IMAGE).

The earlier speed, accuracy and precision work behind this library lives in packages/nnunet_model_conversion.

Metadata

Release files for nnunet-lite 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nnunet-lite 0.1.0
File Size Uploaded Provenance Actions
nnunet_lite-0.1.0.tar.gz 31.1 kB Without provenance Details for nnunet_lite-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nnunet-lite 0.1.0
File Interpreter ABI Platform Actions
nnunet_lite-0.1.0-py3-none-any.whl Python 3 none any Details for nnunet_lite-0.1.0-py3-none-any.whl

Total release size: 67.9 kB

Release files / nnunet_lite-0.1.0.tar.gz

Download URL nnunet_lite-0.1.0.tar.gz
Size 31.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e32c91ed640901027631a58945b1a9329c1e9fc1156e1daeb25ea17a177040f9
BLAKE2b-256 checksum
How to use checksums
1e1c12fd8a2244ac9e2ec210edced54e74a1a6fcb8e77d8084a1d476ee1e55ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / nnunet_lite-0.1.0-py3-none-any.whl

Download URL nnunet_lite-0.1.0-py3-none-any.whl
Size 36.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4fbe23d112033385b7a6be5ab2a6607e390c75fce2c05a89a83dcdb1e3b3a27d
BLAKE2b-256 checksum
How to use checksums
d239286bef2b7b69a5758c922fd6908c78eea538211f0211d66773749889d971
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for nnunet-lite 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page