This repository contains code for training a 3D U-Net for brain tumor segmentation on the FeTS 2022 dataset using federated learning with NVFlare and MONAI.
The repository supports two training modes:
- Standard Federated Learning using Federated Averaging (FedAvg)
- Uncertainty-Driven Sample Selection, where training focuses on the most informative (high-uncertainty) samples while temporarily excluding low-uncertainty cases from training.
Brain tumor segmentation is performed using four MRI modalities:
- T1
- T1ce
- T2
- FLAIR
The network predicts four segmentation classes:
- Background
- Non-enhancing tumor
- Edema
- Enhancing tumor
Each client trains locally on its own dataset and periodically sends model updates to a federated server. The server aggregates the client models using FedAvg before broadcasting the updated global model.
When uncertainty-driven training is enabled, clients dynamically adjust their training dataset by prioritizing uncertain samples and temporarily removing easy cases.
- 3D U-Net implementation using MONAI
- Optional UNETR architecture
- Federated learning with NVFlare
- MONAI preprocessing and data augmentation
- DiceLoss or DiceCELoss
- DiceMetric evaluation
- Sliding-window inference
- Automatic class-weight computation
- Dynamic uncertainty-driven sample selection
- Memory bank for tracking case uncertainty over training rounds
.
├── src/
│ ├── client.py # Standard federated client
│ ├── client_uncertainty.py # Federated client with uncertainty-driven sample selection
│ ├── job.py # Launches the federated learning simulation
│ ├── model.py # U-Net and UNETR model definitions
│ └── utils.py # Utility functions, uncertainty estimation, memory bank
├── config.yml # Server configuration
├── client_config.yml # Client configuration
├── environment.yml # Conda environment
├── submit_job.sh # Example SLURM submission script
└── nvflare_sim.sh # Launch script
The default network is a MONAI 3D U-Net with:
- 4 input channels
- 4 output channels
- Residual units
- Batch normalization
- Dropout
The repository also includes support for UNETR, which can be enabled through the configuration files.
Each subject should contain the following files:
CaseID/
├── CaseID_t1.nii.gz
├── CaseID_t1ce.nii.gz
├── CaseID_t2.nii.gz
├── CaseID_flair.nii.gz
└── CaseID_seg.nii.gz
The segmentation labels are converted into a four-channel one-hot representation during preprocessing.
Training uses MONAI transforms including:
- Image loading
- Intensity normalization
- Multi-channel label conversion
- Spatial padding
- Random 3D patch extraction
- Random flipping
- Intensity shifting
- Gaussian noise
- Intensity clipping
Supported loss functions:
- DiceLoss
- DiceCELoss
Optional voxel-frequency class weighting can be computed automatically.
Performance is measured using the Dice coefficient for:
- Non-enhancing tumor
- Edema
- Enhancing tumor
Validation and testing are performed using sliding-window inference.
The uncertainty-aware training strategy is implemented in client_uncertainty.py.
Instead of training equally on every case throughout the entire experiment, the client periodically estimates the uncertainty of each training sample.
The uncertainty is computed using normalized voxel-wise entropy of the network predictions over tumor voxels only. Cases with higher entropy are considered more informative because the model is less confident in its predictions.
During training:
- Case-level uncertainty scores are computed periodically.
- A memory bank stores the latest uncertainty value for every case.
- Cases whose uncertainty falls below a predefined threshold are temporarily removed from the active training set.
- After a fixed number of federated rounds, excluded cases are automatically reintroduced into training.
- A dynamic sampler updates the DataLoader so that subsequent epochs focus on the currently active cases.
This strategy encourages the model to spend more training time on difficult examples while reducing computation on samples that have already been learned.
Create the environment:
conda env create -f environment.yml
conda activate nvflareRun a simulation:
python job.py --config config.ymlFor HPC systems, example SLURM scripts are provided:
bash submit_job.shTwo configuration files control the experiments:
Contains server-side settings:
- Number of clients
- Number of federated rounds
- Model architecture
- Dataset partitioning
- Learning rate
- Training script
Contains client-side settings:
- Data augmentation
- Model parameters
- Loss function
- Metrics
- Optimizer
- Batch size
- Local epochs
- Validation interval
- Uncertainty-driven sampling options
Major libraries used:
- Python 3.10
- PyTorch
- MONAI
- NVFlare
- NumPy
- Pandas
- NiBabel
- SciPy
A complete list of dependencies is provided in environment.yml.
If you use this repository in your research, please cite the following resources:
- FeTS 2022 Challenge
- FeTS 2024 Challenge
- MONAI
- NVFlare
Please add an appropriate license before releasing this repository publicly.