Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

SegForge

English | 中文

Extension framework for nnU-Net v2: custom trainers, network factories, and discovery CLIs.

Recommended usage: install SegForge as a library, point nnUNet_extTrainer at the installed built-in trainers, and keep experiment-specific -tr classes in your own project folder (do not fork SegForge per dataset).

Requirements

  • Python ≥ 3.10
  • nnU-Net v2 (nnunetv2≥2.8.1) and a working nnU-Net data layout (nnUNet_raw / nnUNet_preprocessed / nnUNet_results)

1. Install as a library

# From PyPI (recommended for users)
pip install segforge
pip install "segforge[neuron]"     # NeuronSWC / npu
pip install "segforge[mamba]"      # U-Mamba / LightM / LKM / nnMamba
pip install "segforge[segmamba]"   # SegMamba BiMamba-v3
pip install "segforge[opaque]"     # Nuitka; hide project network .py as .so
# pip install "segforge[full]"     # mamba + segmamba + scipy / scikit-image

# From a local checkout (SegForge development)
pip install -e ".[dev]"

Extras detail: docs/networks.md.

2. Wire nnU-Net to SegForge trainers (+ optional project trainers)

# Built-in trainers from the installed package
export SEGFORGE_TR="$(python -c 'import segforge.training.nnUNetTrainer as t; print(t.__path__[0])')"

# Recommended for experiments: also append your project trainer dir
export nnUNet_extTrainer="${SEGFORGE_TR}:/path/to/my_project/trainers"
# Built-ins only:
# export nnUNet_extTrainer="${SEGFORGE_TR}"

(: on Linux/macOS, ; on Windows.)

3. List and train

segforge_list_trainers -s external    # SegForge built-ins + project trainers
nnUNetv2_train DATASET_ID 3d_fullres 0 -tr YourProjectTrainer

Binary {0, 255} export (segmentation; remap at write time — no second pass):

# same flags as nnUNetv2_predict
segforge_predict_binary255 -d DATASET_ID -c 3d_fullres -f 0 -i INPUT -o OUTPUT -tr YourTrainer

Distance / soft-map regression predict (default float32 continuous; do not use stock nnUNetv2_predict — it truncates to uint8):

# continuous soft map (~[0,1] with default sigmoid)
segforge_predict_regression -d DATASET_ID -c 3d_fullres -f 0 -i INPUT -o OUTPUT \
  -p nnUNetPlans_reg -tr nnUNetDistanceTrainer

# also dump continuous .npz
segforge_predict_regression ... --save_probabilities

# thresholded binary {0,255} or {0,1} (can combine with --save_probabilities)
segforge_predict_regression ... --threshold 0.5 --binary-value 255
segforge_predict_regression ... --threshold 0.5 --binary-value 1

Project trainers import SegForge bases / mixins / existing trainers; they should not live inside the SegForge source tree unless you are contributing upstream. Details: docs/customizing.md.

Quick start

segforge_list_trainers -s external
segforge_list_mixins -g optim
segforge_list_networks -b monai

nnUNetv2_train DATASET_ID 3d_fullres 0 -tr UNetTrainer

segforge_list_trainers / segforge_list_mixins show a NOTES column from class docstrings (summary + optional paper: / dataset:; use -v for full text). segforge_list_networks -v prints module/builder details.

Networks

Builders, -tr names, CUDA / 2D–3D constraints, and SegMamba BiMamba notes: docs/networks.md.

Highlight (recent): MedNeXt-v2, SegMamba ([segmamba]), SegFormer3D, D-LKA (2D), UNETR++, CMUNeXt (2D).

Trainer layout

Path Role
training/bases/arch.py Inheritance spine (NoDeepSupervisionTrainer, FullResTargetDeepSupervisionTrainer, MonaiDeepSupervisionTrainer, …)
training/bases/mixins/ Optional recipes (geometry / optim / loss / benchmark)
training/nnUNetTrainer/ Runnable public -tr trainers

Naming (recommended): Model[_Patch][_Opt][_LR][_Schedule] — say what changed, not the paper dataset. Put provenance in docstring tags (paper: / dataset:).

Hyperparams: use FixedHyperMixin (or any Adam* mixin that inherits it) for fixed_initial_lr / fixed_weight_decay / fixed_num_epochs.

Compose order: mixins on the left, architecture trainer on the right:

from segforge.training.bases import (
    AdamWCosineNoClipMixin,
    FixedPatchMixin,
    NoDeepSupervisionTrainer,
    SoftclDiceCELossMixin,
    SystemBenchmarkMixin,
)

# Single-output net + forced isotropic patch + AdamW cosine (FixedHyper included)
class FooTrainer_128_AdamW1en3_Cosine(
    AdamWCosineNoClipMixin, FixedPatchMixin, NoDeepSupervisionTrainer
):
    """Foo; patch 128; AdamW lr=1e-3 + cosine.

    paper: …
    """

    fixed_patch_size = 128
    fixed_initial_lr = 1e-3

# Hang a loss recipe on an existing model trainer
from segforge.training.nnUNetTrainer.CoTrTrainer import CoTrTrainer_128
from segforge.training.nnUNetTrainer.MedNeXtTrainer import MedNeXtTrainer_S_kernel3

class CoTr_clDice(SoftclDiceCELossMixin, CoTrTrainer_128):
    pass

# Efficiency probe (not a stock -tr; compose then train briefly)
class MedNeXtBench(SystemBenchmarkMixin, MedNeXtTrainer_S_kernel3):
    benchmark_patch_size = 128
  • Forces patch: name includes size and uses FixedPatchMixin (e.g. CoTrTrainer_128, VNetTrainer_96).
  • Plans patch: no size in the class name (e.g. VNetTrainer, UNETRTrainer).

Dataset conversion

Challenge → nnU-Net raw helpers live under segforge/dataset_conversion/ (not on the training import path). Set nnUNet_raw, then:

python -m segforge.dataset_conversion.Parse2022 -i /path/to/PARSE2022 -d 501
python -m segforge.dataset_conversion.LiTS2017 -i /path/to/LiTS2017 -d 502
python -m segforge.dataset_conversion.DRIVE -i /path/to/DRIVE -d 503
python -m segforge.dataset_conversion.AIIB2023 -i /path/to/AIIB2023 -d 504
python -m segforge.dataset_conversion.BTCV -i /path/to/RawData -d 505
# Neuron: image + SWC → mask or dist
# pip install "segforge[neuron]"   # installs neuron-process-utils (npu) from PyPI
python -m segforge.dataset_conversion.NeuronSWC --image /data/img --swc /data/swc -d 601 --label-type mask
python -m segforge.dataset_conversion.NeuronSWC --image /data/img --swc /data/swc -d 602 --label-type dist -t soft_centerline
# -d is required (nnU-Net folder: DatasetXXX_<Name>)

Docs

中文版:README.zh-CN.md

License

GPL-3.0-only. See LICENSE.

SegForge builds on nnU-Net and third-party architectures/losses; cite the original papers when you use those components. Some vendored files retain their original upstream licenses (e.g. Apache-2.0 CoTr Deformable DETR snippets, MIT CMUNeXt); the overall SegForge distribution is GPL-3.0. Details: docs/networks.md.

Metadata

Release files for segforge 0.1.0a2

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

Source distribution (sdist)

Source distribution for segforge 0.1.0a2
File Size Uploaded
segforge-0.1.0a2.tar.gz 247.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for segforge 0.1.0a2
File Interpreter ABI Platform
segforge-0.1.0a2-py3-none-any.whl Python 3 none any Details

Total release size: 553.0 kB

Release files / segforge-0.1.0a2.tar.gz

Download URL segforge-0.1.0a2.tar.gz
Size 247.5 kB
Tags Source
SHA-256 checksum
How to use checksums
673d1ca137bab2f38bcbff1bbb9ae90f9657e622f8e3ac43d5674ed1b9fb95f1
BLAKE2b-256 checksum
How to use checksums
4110734b70585f136cab4fd37f189cc61c8c20b2089bf82b6ee1cde390a15436
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / segforge-0.1.0a2-py3-none-any.whl

Download URL segforge-0.1.0a2-py3-none-any.whl
Size 305.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f9c14221baf1e41aa7087eb46249380581d03b95d0d9096dd2f1f2a5a49fda6
BLAKE2b-256 checksum
How to use checksums
9958f3e6de6c3b33c918c245ec961a44bea7e17c81e3dd9d8f399eacbc35d291
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.1.0a2 This release

2 release files

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