Skip to content

Commit

Permalink
add dp notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerroth committed Feb 25, 2025
1 parent 751ef81 commit b8647ac
Show file tree
Hide file tree
Showing 18 changed files with 1,014 additions and 1,145 deletions.
20 changes: 16 additions & 4 deletions examples/getting_started/pt/nvflare_pt_getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@
{
"cell_type": "markdown",
"id": "1b70da5d-ba8b-4e65-b47f-44bb9bddae4d",
"metadata": {},
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"source": [
"#### 2. Define a FedJob\n",
"The `FedJob` is used to define how controllers and executors are placed within a federated job using the `to(object, target)` routine.\n",
Expand All @@ -271,7 +275,11 @@
"cell_type": "code",
"execution_count": null,
"id": "13771bfb-901f-485a-9a23-84db1ccd5fe4",
"metadata": {},
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"from src.net import Net\n",
Expand Down Expand Up @@ -368,7 +376,11 @@
{
"cell_type": "markdown",
"id": "9ac3f0a8-06bb-4bea-89d3-4a5fc5b76c63",
"metadata": {},
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"source": [
"#### 6. Run FL Simulation\n",
"Finally, we can run our FedJob in simulation using NVFlare's [simulator](https://nvflare.readthedocs.io/en/main/user_guide/nvflare_cli/fl_simulator.html) under the hood. We can also specify which GPU should be used to run this client, which is helpful for simulated environments. The results will be saved in the specified `workdir`."
Expand Down Expand Up @@ -438,7 +450,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.19"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
3 changes: 3 additions & 0 deletions examples/tutorials/self-paced-training/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.virtual_documents

job_configs/
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "nvflare_example",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "nvflare_example"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -25,7 +25,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.2"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,23 @@
"id": "1398ef0a-f189-4d04-a8a9-276a17ab0f8b",
"metadata": {},
"source": [
"# Federated Learning with Differential Privacy for BraTS18 Segmentation\n",
"# Federated Learning with Differential Privacy\n",
"\n",
"Please make sure you set up virtual environment and follows [example root readme](../../README.md)\n",
"Please make sure you set up a virtual environment and follow [example root readme](../../README.md) before starting this notebook.\n",
"Then, install the requirements.\n",
"\n",
"## Introduction to MONAI, BraTS and Differential Privacy"
"<div class=\"alert alert-block alert-info\"> <b>NOTE</b> Some of the cells below generate long text output. We're using <pre>%%capture --no-display --no-stderr cell_output</pre> to suppress this output. Comment or delete this line in the cells below to restore full output.</div>"
]
},
{
"cell_type": "markdown",
"id": "af5f3c69-aeba-4cea-89c8-d54fd6520ab1",
"metadata": {},
"source": [
"### MONAI\n",
"This example shows how to use [NVIDIA FLARE](https://nvflare.readthedocs.io/en/main/index.html) on medical image applications.\n",
"It uses [MONAI](https://github.com/Project-MONAI/MONAI),\n",
"which is a PyTorch-based, open-source framework for deep learning in healthcare imaging, part of the PyTorch Ecosystem."
]
},
{
"cell_type": "markdown",
"id": "8a3b7e0b-9dbd-4d21-8b59-a3d08cf2b2bb",
"cell_type": "code",
"execution_count": 2,
"id": "5002e45c-f58e-4f68-bb5a-9626e084947f",
"metadata": {},
"outputs": [],
"source": [
"### BraTS\n",
"The application shown in this example is volumetric (3D) segmentation of brain tumor subregions from multimodal MRIs based on BraTS 2018 data.\n",
"It uses a deep network model published by [Myronenko 2018](https://arxiv.org/abs/1810.11654) [1].\n",
"\n",
"The model is trained to segment 3 nested subregions of primary brain tumors (gliomas): the \"enhancing tumor\" (ET), the \"tumor core\" (TC), the \"whole tumor\" (WT) based on 4 aligned input MRI scans (T1c, T1, T2, FLAIR). \n",
"\n",
"<img src=\"https://developer.download.nvidia.com/assets/Clara/Images/clara_pt_brain_mri_segmentation_workflow.png\" alt=\"clara_pt_brain_mri_segmentation_workflow\" width=\"600\"/>\\n\n",
"\n",
"- The ET is described by areas that show hyper intensity in T1c when compared to T1, but also when compared to \"healthy\" white matter in T1c. \n",
"- The TC describes the bulk of the tumor, which is what is typically resected. The TC entails the ET, as well as the necrotic (fluid-filled) and the non-enhancing (solid) parts of the tumor. \n",
"- The WT describes the complete extent of the disease, as it entails the TC and the peritumoral edema (ED), which is typically depicted by hyper-intense signal in FLAIR.\n",
"\n",
"To run this example, please make sure you have downloaded BraTS 2018 data, which can be obtained from [Multimodal Brain Tumor Segmentation Challenge (BraTS) 2018](https://www.med.upenn.edu/cbica/brats2018.html) [2-6]. Please download the data to [./dataset_brats18/dataset](./dataset_brats18/dataset). It should result in a sub-folder `./dataset_brats18/dataset/training`.\n",
"In this example, we split BraTS18 dataset into [4 subsets](./dataset_brats18/datalist) for 4 clients. Each client requires at least a 12 GB GPU to run. "
"%%capture --no-display --no-stderr cell_output\n",
"!pip install -r requirements.txt"
]
},
{
Expand All @@ -50,29 +30,8 @@
"metadata": {},
"source": [
"### Differential Privacy (DP)\n",
"[Differential Privacy (DP)](https://arxiv.org/abs/1910.00962) [7] is method for ensuring that Federated Learning (FL) preserves privacy by obfuscating the model updates sent from clients to the central server.\n",
"This example shows the usage of a MONAI-based trainer for medical image applications with NVFlare, as well as the usage of DP filters in your FL training. DP is added as a filter in `config_fed_client.json`. Here, we use the \"Sparse Vector Technique\", i.e. the [SVTPrivacy](https://nvflare.readthedocs.io/en/main/apidocs/nvflare.app_common.filters.svt_privacy.html) protocol, as utilized in [Li et al. 2019](https://arxiv.org/abs/1910.00962) [7] (see [Lyu et al. 2016](https://arxiv.org/abs/1603.01699) [8] for more information)."
]
},
{
"cell_type": "markdown",
"id": "33306153-a8c5-4c2b-9eea-28c3e0d705a6",
"metadata": {},
"source": [
"## Prepare local configs\n",
"First, we add the image and datalist directory roots to `config_train.json` files for generating the absolute path to the dataset by replacing the `DATASET_ROOT` and `DATALIST_ROOT` placeholders. In the current folder structure, it will be `${PWD}/dataset_brats18/dataset` for `DATASET_ROOT` and `${PWD}/dataset_brats18/datalist` for `DATALIST_ROOT` but you can update the below `sed` commands if the data is located somewhere else."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "be8a7f47-2a93-4992-85c0-d597f4ecf3d5",
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"sed -i \"s|DATASET_ROOT|${PWD}/dataset_brats18/dataset|g\" config_train.json\n",
"sed -i \"s|DATALIST_ROOT|${PWD}/dataset_brats18/datalist|g\" config_train.json"
"[Differential Privacy (DP)](https://arxiv.org/abs/1910.00962) [7] is a method for ensuring that Federated Learning (FL) preserves privacy by obfuscating the model updates sent from clients to the central server. \n",
"This example shows the usage of a CIFAR-10 training code with NVFlare, as well as the usage of DP filters in your FL training. DP is added as a filter in `config_fed_client.json`. Here, we use the \"Sparse Vector Technique\", i.e. the [SVTPrivacy](https://nvflare.readthedocs.io/en/main/apidocs/nvflare.app_common.filters.svt_privacy.html) protocol, as utilized in [Li et al. 2019](https://arxiv.org/abs/1910.00962) [7] (see [Lyu et al. 2016](https://arxiv.org/abs/1603.01699) [8] for more information)."
]
},
{
Expand All @@ -83,33 +42,8 @@
"## Run experiments with FL simulator\n",
"### Training with FL simulator\n",
"FL simulator is used to simulate FL experiments or debug codes, not for real FL deployment.\n",
"In this example, we assume four local GPUs with at least 12GB of memory are available."
]
},
{
"cell_type": "markdown",
"id": "8d256389-4112-46e6-86bd-115c9bf2e189",
"metadata": {},
"source": [
"Then, we can run the FL simulator with 1 client for centralized training"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9583165-af58-45ed-a86d-9fbfc74d80ca",
"metadata": {},
"outputs": [],
"source": [
"!python3 -u -m nvflare.private.fed.app.simulator.simulator './configs/brats_central' -w './workspace_brats/brats_central' -n 1 -t 1 -gpu 0"
]
},
{
"cell_type": "markdown",
"id": "f1121bc2-1118-4b64-8d61-06e1a49bc7ef",
"metadata": {},
"source": [
"Similarly, run the FL simulator with 4 clients for federated learning by running"
"\n",
"First, train a model using the FedAvg algorithm with four clients without DP."
]
},
{
Expand All @@ -119,7 +53,7 @@
"metadata": {},
"outputs": [],
"source": [
"!nvflare simulator './configs/brats_fedavg' -w './workspace_brats/brats_fedavg' -n 4 -t 4 -gpu 0,1,2,3"
"!nvflare simulator './configs/brats_fedavg' -w './workspace_brats/brats_fedavg' -n 4 -t 4"
]
},
{
Expand All @@ -137,7 +71,7 @@
"metadata": {},
"outputs": [],
"source": [
"!nvflare simulator './configs/brats_fedavg_dp' -w './workspace_brats/brats_fedavg_dp' -n 4 -t 4 -gpu 0,1,2,3"
"!nvflare simulator './configs/brats_fedavg_dp' -w './workspace_brats/brats_fedavg_dp' -n 4 -t 4"
]
},
{
Expand Down
Loading

0 comments on commit b8647ac

Please sign in to comment.