Tutorial

Brain Tumor Segmentation on MRI using nnU-Net

Difficulty: Advanced Time: 20 min read

Introduction

In the field of medical image segmentation, nnU-Net (no-new-Net) is famous for consistently dominating international challenges like BraTS (Brain Tumor Segmentation). Rather than inventing new architectures, nnU-Net automatically configures the entire pipeline (preprocessing, network topology, training, post-processing) based on the specific properties of your dataset. In this guide, we'll walk through using nnU-Net to segment brain tumors from multi-modal MRI scans.

Architecture Overview


graph TD
    RawData(Raw Medical Images & Labels) --> Fingerprint(Dataset Fingerprint Extraction)
    Fingerprint --> Heuristics(Heuristics Engine)
    
    Heuristics --> Config2D(2D U-Net Config)
    Heuristics --> Config3D(3D Fullres U-Net Config)
    Heuristics --> ConfigCascade(3D Cascade U-Net Config)
    
    Config3D --> Train(5-Fold Cross Validation Training)
    Train --> PostProcess(Automated Post-processing & Ensembling)
    PostProcess --> Final(Optimal Segmentation Pipeline)
        

Prerequisites

  • Linux/Ubuntu (nnU-Net is not officially supported on Windows natively)
  • Python 3.9+
  • PyTorch
  • A CUDA-capable GPU with at least 11GB VRAM (24GB+ recommended)
  • The BraTS dataset (T1, T1c, T2, and FLAIR MRI modalities)

Step 1: Install nnU-Net V2

# It is highly recommended to install nnU-Net as a hidden editable package
git clone https://github.com/MIC-DKFZ/nnUNet.git
cd nnUNet
pip install -e .

Step 2: Setup Environment Variables

nnU-Net requires three specific directories to function: one for raw data, one for preprocessed data, and one for trained models. You must set these as environment variables.

export nnUNet_raw="/path/to/nnUNet_raw"
export nnUNet_preprocessed="/path/to/nnUNet_preprocessed"
export nnUNet_results="/path/to/nnUNet_results"

Step 3: Dataset Conversion and JSON Generation

nnU-Net requires a very specific folder structure and naming convention. Each training case must end with a 4-digit identifier, and different imaging modalities (T1, FLAIR, etc.) are indicated by a 4-digit channel identifier at the very end of the filename (e.g., BraTS_0001_0000.nii.gz for T1, BraTS_0001_0001.nii.gz for FLAIR).

It also requires a dataset.json file describing the channels and labels. Here is a Python snippet using the nnU-Net helper function to generate it:

from nnunetv2.dataset_conversion.generate_dataset_json import generate_dataset_json
import os

target_base = os.environ['nnUNet_raw']
dataset_name = "Dataset001_BraTS"
target_dataset_dir = os.path.join(target_base, dataset_name)

generate_dataset_json(
    output_folder=target_dataset_dir,
    channel_names={
        0: 'T1',
        1: 'T1ce',
        2: 'T2',
        3: 'FLAIR'
    },
    labels={
        'background': 0,
        'necrotic tumor core': 1,
        'peritumoral edematous/invaded tissue': 2,
        'enhancing tumor': 3
    },
    num_training_cases=1251, # Number of training images
    file_ending='.nii.gz',
    dataset_name=dataset_name,
    reference='BraTS Challenge',
    release='1.0',
    description='Brain Tumor Segmentation'
)

Step 4: Automated Experiment Planning and Preprocessing

This is where the magic happens. nnU-Net analyzes your dataset's voxel spacings, image sizes, and class ratios, and automatically designs the optimal U-Net topologies (2D, 3D fullres, 3D cascade) and data augmentation strategies.

# Assuming your dataset ID is 001
nnUNetv2_plan_and_preprocess -d 001 --verify_dataset_integrity

Step 5: Training the Model

nnU-Net trains a 5-fold cross-validation ensemble by default. For the best performance on 3D MRI data, we train the 3d_fullres configuration. You must run this command 5 times, changing the fold number from 0 to 4.

# Train fold 0
nnUNetv2_train 001 3d_fullres 0

# (Optional: Run for folds 1, 2, 3, and 4 to build the ensemble)

Step 6: Inference on New Scans

Once trained, you can use the model to predict tumor segmentations on unseen MRI scans.

nnUNetv2_predict -i /path/to/testing_images/ -o /path/to/output_predictions/ -d 001 -c 3d_fullres -f 0

Conclusion

nnU-Net represents a paradigm shift in medical imaging AI. By automating the arduous process of hyperparameter tuning, patch size selection, and pipeline configuration, it provides an exceptionally strong, out-of-the-box baseline that frequently outperforms custom, hand-tuned networks on complex tasks like multi-modal brain tumor segmentation. It is arguably the most important baseline framework in modern medical image analysis.