Skip to content

feat: nnUNet integration for CoPick cryo-ET datasets#100

Merged
jtschwar merged 5 commits into
mainfrom
nnunet
May 14, 2026
Merged

feat: nnUNet integration for CoPick cryo-ET datasets#100
jtschwar merged 5 commits into
mainfrom
nnunet

Conversation

@jtschwar

@jtschwar jtschwar commented May 6, 2026

Copy link
Copy Markdown
Contributor

Adds a full nnUNet training and inference pipeline accessible via the copick-torch CLI. The workflow covers three stages — dataset preparation, model training, and inference — each exposed as a
CLI subcommand and registered as a copick entry point.

What's new

copick_torch/nnunet/ module

  • prepare (copick.convert.commands): Converts a CoPick project into nnUNet raw dataset format. Reads tomograms and multilabel segmentations, writes them as .nii.gz NIfTI files in the imagesTr /
    labelsTr / imagesTs directory structure, and emits a dataset.json. Voxel spacing is converted from Ångströms to nanometres for nnUNet's patch-size planner. Conversion is multi-threaded.
  • train (copick.training.commands): Runs nnUNetv2_plan_and_preprocess followed by nnUNetv2_train. Supports the standard nnUNet trainer, the Residual Encoder Large variant (resnecl), and all ten
    MedNeXt variants (S/B/M/L × kernel-3/kernel-5). Multi-GPU DDP is supported via --num-gpus; the plans batch size is scaled automatically so per-GPU memory stays constant. Resumes from an existing
    checkpoint if one is found.
  • segment (copick.inference.commands): Wraps nnUNet inference as nnUNetPredictor. Single-volume inference runs on cuda:0; batch inference shards runs round-robin across all available GPUs using
    mp.spawn. Supports fold ensembling (pass multiple -w checkpoint paths). Writes predictions back to the CoPick overlay as multilabel segmentations and saves a YAML parameter log.
  • mednext_trainer.py: MedNeXt trainer class definitions (10 variants). Copied into nnUNet's trainer discovery directory on first use so the standard nnUNetv2_train CLI and checkpoint loading
    both work without patching nnUNet.

pyproject.toml

  • Adds nnunetv2 and SimpleITK as dependencies.
  • Registers CLI entry points in copick.convert, copick.training, and copick.inference command groups.

Usage sketch

1. Prepare dataset

  copick nnunet prepare -c config.json --tomo-uri wbp@10.0 \                                                                                                                                  
      --seg-info targets --dataset-name CZII --raw /data/nnunet_raw

2. Train (standard nnUNet, fold 0)

  copick nnunet train -n CZII -r /data/nnunet_raw \                                                                                                                                           
      --preprocessed /data/preprocessed --results /data/results     

3. Segment

  copick nnunet segment -c config.json \                                                                                                                                                      
      -p /data/results/.../nnUNetPlans.json \                                                                                                                                                       
      -d /data/nnunet_raw/Dataset001_CZII/dataset.json \                                                                                                                                            
      -w /data/results/.../fold_0/checkpoint_best.pth 

@jtschwar jtschwar merged commit 53e7dcc into main May 14, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant