MIC-DKFZ/nnssl

Python

184

3,039 commits

updated Sep 24, 2026

See the code

README

nnFoundation

nnFoundation: 3D Foundation Models for Radiology

arXiv Hugging Face

Copyright German Cancer Research Center (DKFZ) and contributors. Please make sure that your usage of this code is in compliance with its license.

This repository holds the code used to pre-train nnFoundation, our pair of 3D radiology foundation models, built on top of the nnssl self-supervised learning framework.

Model pair

ModelArchitectureDetailsParamsTrainerCheckpoint
nnFoundationCNNResEnc6 stages, features 32–64–128–256–320–320102MnnFoundationCNN_trainer🤗 nnFoundationCNN
nnFoundationViTPrimus40 layers, embedding dim 1056, 16 heads, 8³ patch tokens674MnnFoundationViT_trainer🤗 nnFoundationViT

Using nnFoundation

To fine-tune nnFoundation on your own downstream tasks, use one of our dedicated repositories:

OpenMind

This repository also holds the code for An OpenMind for 3D medical vision self-supervised learning, the benchmark study of 3D SSL pre-training methods that nnssl grew out of.

  • OpenMind readme -- the benchmarked architectures and pre-training methods, the OpenMind dataset, the fine-tuning frameworks, and the released pre-trained checkpoints.

Below you will find a brief description of the needed steps, to get started with nnssl. Check-out the documentation directory for a lot more information on how to use this repository.

Installation

  1. Download/clone the repository
  2. Unzip and navigate into the repository
  3. Install the repository pip install -e . (-e optional)
  4. Set the environment variables (see below)

More details can be found in the installation instructions.

Setting environment variables

In addition to the installation, this repository requires setting up three additional paths:

  1. nnssl_raw -- The path holding datasets of raw pretrain_data.json files.
  2. nnssl_preprocessed -- A path where preprocessed data will be stored.
  3. nnssl_results -- A path where results will be stored.

More details can be found here.

Workflow

In order to conduct pre-training with this repository three main steps need to be conducted.

1. Raw Data Preparation

This is a brief description. For a more detailed version, check here.

First, some pre-training dataset needs to be chosen. You can use the OpenMind dataset. However any other dataset could be used as well. Opposed to nnU-Net, the data does not have to be in a specific format. Instead, a pretrain_data.json file needs to be created detailing the datasets specific information. (For simplicity the OpenMind dataset comes with this). To create this file for your own dataset or to understand the file, we refer to the instructions below.

If you use the OpenMind dataset, use the dedicated call nnssl_convert_openmind to create its associated pretrain_data.json

Understanding and creating the `pretrain_data.json` file

Medical datasets generally center around studies of subjects. These subjects can be imaged in different sessions with different scanners or through different imaging protocols. This is reflected in the common BIDS data structure, which differentiates into:

  • subjects - The individual subjects in the dataset
  • sessions - The individual sessions of the subjects
  • scans - The individual scans of the sessions

In our case, we are also interested in aggregating multiple datasets, hence we include

  • dataset - The individual datasets that was included

All this information may be valuable for pre-training, e.g. one may want to develop a contrastive pre-training method that uses scans of the same subject during one session as positive pair and others as negative. Or one may want to develop a longitudinal pre-training that e.g. tries to predict the next scan of the next session. To allow using such information, we need to maintain this information in the pretrain_data.json file.

Hence, our `pretrain_data.json` file mirrors the BIDS structure:
{  // Examplary Structure
  "collection_index": 745,    // Collection Index -- 
  "collection_name": "Dataset745__OpenNeuro",     // Collection Index -- 
  "datasets": {     // Dict of all datasets included
    "ds000001": {
      "dataset_index": "ds000001",
      "dataset_info": null,   // dict holding meta info of the dataset
      "subjects": {    // dict of all subjects
        "sub-01": {
          "subject_id": "sub-01",
          "subject_info": {
            "age": 26,
            "sex": "female",
          },
          "sessions": {     // Dict of all session and session information
            "ses-DEFAULT": {
                "session_id": "ses-DEFAULT",
                "session_info": null,
                "images": [   // list of images -- Each image is it's own dictionary
                    {
                        "name": "sub-01_T1w.nii.gz",   // Image Name
                        "modality": "T1w",  // Modality of the Image
                        "image_info": {},  // Additional meta-data of the image (e.g. Scanner, etc.)
                        "image_path": "<Path_to_image>",
                        "associated_masks": {  // Associated masks of the image (if available)
                            "anatomy_mask": "<Path_to_associated_anatomy_mask>",
                            "anonymization_mask": "<Path_to_associated_anon_mask>",
                        },
                    },
                    ... // Additional images if taken in the session.
                ],
            }
          }
        }
      }
    }
  }
}

To generate this file, we recommend writing a python script that creates a Collection dataclass (located in src/nnssl/data/raw_dataset.py) and uses the .to_dict() method of the collection which will yield a valid pretrain_data.json file.

To allow this file to be valid for differing machines, the file-paths support relative paths. Relative paths are indicated through the pre-fix $. Moreover, when saving absolute paths the paths are checked, if the image path beginnings can be replaced by the paths in the Environment Variables: ["nnssl_raw", "nnssl_preprocessed"], replacing them with $nnssl_raw or $nnssl_preprocessed respectively.

2. Preprocessing the data

Currently the framework follows the nnU-Net preprocessing pipeline. This generally includes a fingerprinting, planning, and lastly preprocessing of the data. Fingerprinting determines overall shape and spacing of the data. Planning determines which patch size to use and which spacing to resample to. Preprocessing normalizes, crops and resamples the data and saves it compressed in the bloscv2 format1. Moreover, the pretrain_data.json file will be copied to the nnssl_preprocessed directory, with the image and mask paths adjusted accordingly.

To conduct these three steps run

nnssl_plan_and_preprocess -d <Dataset ID>

3. Pretraining

Given the preprocessed data we can now pre-train the models. This is done by selecting a trainera dataset and a plan. The trainer determines pre-training method and architecture, the dataset the data to use and the plan the preprocessing of the data.

An exemplary pre-training call for a 4xGPU ResEnc-L MAE pre-training would be: python ./src/nnssl/run/run_training.py ID CONFIG -tr BaseMAETrainer_BS8 -p nnsslPlans -num_gpus 4 or nnssl_train ID CONFIG -tr BaseMAETrainer_BS8 -p nnsslPlans -num_gpus 4

Note: Due to the lack of e.g. linear-probing for segmentation, no metrics aside from the train and validation loss are tracked during pre-training.

4. Adaptation

After pre-training, the resulting model checkpoint (or, pre-existing checkpoints) can be adapted to a specific downstream task. This can be done via the dedicated downstream repositories linked above.

Extending and Contributing

Due to the lack of established frameworks in the domain of 3D SSL, we are open to code contributions and extensions of the current framework.

Citation

If you use the nnFoundation models or the nnssl framework, please cite:

nnFoundation BibTeX
@misc{harsy2026nnfoundation3dfoundationmodels,
      title={nnFoundation: 3D Foundation Models for Radiology}, 
      author={Constantin Ulrich Harsy and Tassilo Wald and Karol Gotkowski and Yannick Kirchhoff and Marcel Knopp and Maximilian Rokuss and Elisa Stegmeier and Philipp Schader and Dasha Trofimova and Raphael Stock and Kim-Celine Kahl and Stephen Schaumann and Selen Erkan and David Zimmerer and Stefan Denner and Moritz Langenberg and Sebastian Ziegler and Katharina Eckstein and Maximilian Fischer and Jonathan Suprijadi and Bálint Kovács and Benjamin Hamm and Anand Deshpande and Dimitrios Bounias and Nico Disch and Shuhan Xiao and Jessica Kächele and Jan Sellner and Rajesh Baidya and Jeremias Traub and Lars Krämer and Maximilian Zenk and Tim Rädsch and Stefan Dvoretskii and Robin Peretzke and Jonathan Deissler and Alexandra Ertl and Partha Ghosh and Kris Dreher and Stefan Dinkelacker and Annika Reinke and Evangelia Christodoulou and Numan Saeed and Yoland Savriama and Santiago Estrada and David Kügler and Laura Alexandra Daza Barragan and Cristina Isabel Gonzalez Osorio and Jan Peeken and Michael Baumgartner and Marvin Teichmann and Guillaume Chabin and Matthias Kirchler and Valentin Koch and for the ALFA study and Markus Hohenhaus and Dimitri Koslov and Nina Decker and Mohammad Yaqub and Arnd Heuser and Martin Reuter and Julia A. Schnabel and Tobias Heimann and Florin Ghesu and Paul Brachmann and Claus P. Heußel and Alexander Radbruch and Gianluca Brugnara and Aditya Rastogi and Martha Foltyn-Dumitru and Heinz-Peter Schlemmer and Ignaz Reicht and Julius C. Holzschuh and Michael Bach and Bram Stieltjes and Kai Schlamp and Lena Maier-Hein and Marco Nolden and Ralf Floca and Paul F. Jäger and Philipp Vollmuth and Fabian Isensee and Klaus H. Maier-Hein},
      year={2026},
      eprint={2609.26924},
      archivePrefix={arXiv},
      primaryClass={cs.CV},
      url={https://arxiv.org/abs/2609.26924}, 
}

If you use the nnssl framework, the OpenMind dataset, or the OpenMind checkpoints, please cite:

OpenMind BibTeX
@InProceedings{Wald_2025_ICCV,
    author    = {Wald, Tassilo and Ulrich, Constantin and Suprijadi, Jonathan and Ziegler, Sebastian and Nohel, Michal and Peretzke, Robin and Kohler, Gregor and Maier-Hein, Klaus},
    title     = {An OpenMind for 3D Medical Vision Self-supervised Learning},
    booktitle = {Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV)},
    month     = {October},
    year      = {2025},
    pages     = {23839-23879}
}

Footnotes

  1. Bloscv2 is a compressed format that allows partial decompression and reading of the data, allowing fast I/O while minimizing CPU usage. ↩

Contributors

(top 30 of 62)

FabianIsensee

1,446 commits

TaWald

1,223 commits

jsuprijadi-dkfz

48 commits

MIC-DKFZ/nnssl

Python

184

3,039 commits

updated Sep 24, 2026

See the code

README

nnFoundation

nnFoundation: 3D Foundation Models for Radiology

arXiv Hugging Face

Copyright German Cancer Research Center (DKFZ) and contributors. Please make sure that your usage of this code is in compliance with its license.

This repository holds the code used to pre-train nnFoundation, our pair of 3D radiology foundation models, built on top of the nnssl self-supervised learning framework.

Model pair

ModelArchitectureDetailsParamsTrainerCheckpoint
nnFoundationCNNResEnc6 stages, features 32–64–128–256–320–320102MnnFoundationCNN_trainer🤗 nnFoundationCNN
nnFoundationViTPrimus40 layers, embedding dim 1056, 16 heads, 8³ patch tokens674MnnFoundationViT_trainer🤗 nnFoundationViT

Using nnFoundation

To fine-tune nnFoundation on your own downstream tasks, use one of our dedicated repositories:

OpenMind

This repository also holds the code for An OpenMind for 3D medical vision self-supervised learning, the benchmark study of 3D SSL pre-training methods that nnssl grew out of.

  • OpenMind readme -- the benchmarked architectures and pre-training methods, the OpenMind dataset, the fine-tuning frameworks, and the released pre-trained checkpoints.

Below you will find a brief description of the needed steps, to get started with nnssl. Check-out the documentation directory for a lot more information on how to use this repository.

Installation

  1. Download/clone the repository
  2. Unzip and navigate into the repository
  3. Install the repository pip install -e . (-e optional)
  4. Set the environment variables (see below)

More details can be found in the installation instructions.

Setting environment variables

In addition to the installation, this repository requires setting up three additional paths:

  1. nnssl_raw -- The path holding datasets of raw pretrain_data.json files.
  2. nnssl_preprocessed -- A path where preprocessed data will be stored.
  3. nnssl_results -- A path where results will be stored.

More details can be found here.

Workflow

In order to conduct pre-training with this repository three main steps need to be conducted.

1. Raw Data Preparation

This is a brief description. For a more detailed version, check here.

First, some pre-training dataset needs to be chosen. You can use the OpenMind dataset. However any other dataset could be used as well. Opposed to nnU-Net, the data does not have to be in a specific format. Instead, a pretrain_data.json file needs to be created detailing the datasets specific information. (For simplicity the OpenMind dataset comes with this). To create this file for your own dataset or to understand the file, we refer to the instructions below.

If you use the OpenMind dataset, use the dedicated call nnssl_convert_openmind to create its associated pretrain_data.json

Understanding and creating the `pretrain_data.json` file

Medical datasets generally center around studies of subjects. These subjects can be imaged in different sessions with different scanners or through different imaging protocols. This is reflected in the common BIDS data structure, which differentiates into:

  • subjects - The individual subjects in the dataset
  • sessions - The individual sessions of the subjects
  • scans - The individual scans of the sessions

In our case, we are also interested in aggregating multiple datasets, hence we include

  • dataset - The individual datasets that was included

All this information may be valuable for pre-training, e.g. one may want to develop a contrastive pre-training method that uses scans of the same subject during one session as positive pair and others as negative. Or one may want to develop a longitudinal pre-training that e.g. tries to predict the next scan of the next session. To allow using such information, we need to maintain this information in the pretrain_data.json file.

Hence, our `pretrain_data.json` file mirrors the BIDS structure:
{  // Examplary Structure
  "collection_index": 745,    // Collection Index -- 
  "collection_name": "Dataset745__OpenNeuro",     // Collection Index -- 
  "datasets": {     // Dict of all datasets included
    "ds000001": {
      "dataset_index": "ds000001",
      "dataset_info": null,   // dict holding meta info of the dataset
      "subjects": {    // dict of all subjects
        "sub-01": {
          "subject_id": "sub-01",
          "subject_info": {
            "age": 26,
            "sex": "female",
          },
          "sessions": {     // Dict of all session and session information
            "ses-DEFAULT": {
                "session_id": "ses-DEFAULT",
                "session_info": null,
                "images": [   // list of images -- Each image is it's own dictionary
                    {
                        "name": "sub-01_T1w.nii.gz",   // Image Name
                        "modality": "T1w",  // Modality of the Image
                        "image_info": {},  // Additional meta-data of the image (e.g. Scanner, etc.)
                        "image_path": "<Path_to_image>",
                        "associated_masks": {  // Associated masks of the image (if available)
                            "anatomy_mask": "<Path_to_associated_anatomy_mask>",
                            "anonymization_mask": "<Path_to_associated_anon_mask>",
                        },
                    },
                    ... // Additional images if taken in the session.
                ],
            }
          }
        }
      }
    }
  }
}

To generate this file, we recommend writing a python script that creates a Collection dataclass (located in src/nnssl/data/raw_dataset.py) and uses the .to_dict() method of the collection which will yield a valid pretrain_data.json file.

To allow this file to be valid for differing machines, the file-paths support relative paths. Relative paths are indicated through the pre-fix $. Moreover, when saving absolute paths the paths are checked, if the image path beginnings can be replaced by the paths in the Environment Variables: ["nnssl_raw", "nnssl_preprocessed"], replacing them with $nnssl_raw or $nnssl_preprocessed respectively.

2. Preprocessing the data

Currently the framework follows the nnU-Net preprocessing pipeline. This generally includes a fingerprinting, planning, and lastly preprocessing of the data. Fingerprinting determines overall shape and spacing of the data. Planning determines which patch size to use and which spacing to resample to. Preprocessing normalizes, crops and resamples the data and saves it compressed in the bloscv2 format1. Moreover, the pretrain_data.json file will be copied to the nnssl_preprocessed directory, with the image and mask paths adjusted accordingly.

To conduct these three steps run

nnssl_plan_and_preprocess -d <Dataset ID>

3. Pretraining

Given the preprocessed data we can now pre-train the models. This is done by selecting a trainera dataset and a plan. The trainer determines pre-training method and architecture, the dataset the data to use and the plan the preprocessing of the data.

An exemplary pre-training call for a 4xGPU ResEnc-L MAE pre-training would be: python ./src/nnssl/run/run_training.py ID CONFIG -tr BaseMAETrainer_BS8 -p nnsslPlans -num_gpus 4 or nnssl_train ID CONFIG -tr BaseMAETrainer_BS8 -p nnsslPlans -num_gpus 4

Note: Due to the lack of e.g. linear-probing for segmentation, no metrics aside from the train and validation loss are tracked during pre-training.

4. Adaptation

After pre-training, the resulting model checkpoint (or, pre-existing checkpoints) can be adapted to a specific downstream task. This can be done via the dedicated downstream repositories linked above.

Extending and Contributing

Due to the lack of established frameworks in the domain of 3D SSL, we are open to code contributions and extensions of the current framework.

Citation

If you use the nnFoundation models or the nnssl framework, please cite:

nnFoundation BibTeX
@misc{harsy2026nnfoundation3dfoundationmodels,
      title={nnFoundation: 3D Foundation Models for Radiology}, 
      author={Constantin Ulrich Harsy and Tassilo Wald and Karol Gotkowski and Yannick Kirchhoff and Marcel Knopp and Maximilian Rokuss and Elisa Stegmeier and Philipp Schader and Dasha Trofimova and Raphael Stock and Kim-Celine Kahl and Stephen Schaumann and Selen Erkan and David Zimmerer and Stefan Denner and Moritz Langenberg and Sebastian Ziegler and Katharina Eckstein and Maximilian Fischer and Jonathan Suprijadi and Bálint Kovács and Benjamin Hamm and Anand Deshpande and Dimitrios Bounias and Nico Disch and Shuhan Xiao and Jessica Kächele and Jan Sellner and Rajesh Baidya and Jeremias Traub and Lars Krämer and Maximilian Zenk and Tim Rädsch and Stefan Dvoretskii and Robin Peretzke and Jonathan Deissler and Alexandra Ertl and Partha Ghosh and Kris Dreher and Stefan Dinkelacker and Annika Reinke and Evangelia Christodoulou and Numan Saeed and Yoland Savriama and Santiago Estrada and David Kügler and Laura Alexandra Daza Barragan and Cristina Isabel Gonzalez Osorio and Jan Peeken and Michael Baumgartner and Marvin Teichmann and Guillaume Chabin and Matthias Kirchler and Valentin Koch and for the ALFA study and Markus Hohenhaus and Dimitri Koslov and Nina Decker and Mohammad Yaqub and Arnd Heuser and Martin Reuter and Julia A. Schnabel and Tobias Heimann and Florin Ghesu and Paul Brachmann and Claus P. Heußel and Alexander Radbruch and Gianluca Brugnara and Aditya Rastogi and Martha Foltyn-Dumitru and Heinz-Peter Schlemmer and Ignaz Reicht and Julius C. Holzschuh and Michael Bach and Bram Stieltjes and Kai Schlamp and Lena Maier-Hein and Marco Nolden and Ralf Floca and Paul F. Jäger and Philipp Vollmuth and Fabian Isensee and Klaus H. Maier-Hein},
      year={2026},
      eprint={2609.26924},
      archivePrefix={arXiv},
      primaryClass={cs.CV},
      url={https://arxiv.org/abs/2609.26924}, 
}

If you use the nnssl framework, the OpenMind dataset, or the OpenMind checkpoints, please cite:

OpenMind BibTeX
@InProceedings{Wald_2025_ICCV,
    author    = {Wald, Tassilo and Ulrich, Constantin and Suprijadi, Jonathan and Ziegler, Sebastian and Nohel, Michal and Peretzke, Robin and Kohler, Gregor and Maier-Hein, Klaus},
    title     = {An OpenMind for 3D Medical Vision Self-supervised Learning},
    booktitle = {Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV)},
    month     = {October},
    year      = {2025},
    pages     = {23839-23879}
}

Footnotes

  1. Bloscv2 is a compressed format that allows partial decompression and reading of the data, allowing fast I/O while minimizing CPU usage. ↩

Contributors

(top 30 of 62)

FabianIsensee

1,446 commits

TaWald

1,223 commits

jsuprijadi-dkfz

48 commits

Languages

Python

100.0%