Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update to nnunetV2 #145

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions docs/AutoPipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,18 @@ The contours can be selected by creating a YAML file to define a regular express
```sh
OUTPUT_DIRECTORY
├── nnUNet_preprocessed
├── nnUNet_raw_data_base
│ └── nnUNet_raw_data
│ └── Task500_HNSCC
│ ├── imagesTr
│ ├── imagesTs
│ ├── labelsTr
│ └── labelsTs
└── nnUNet_trained_models
├── nnUNet_raw
│   └── Dataset001_HNSCC
│   ├── dataset.csv
│   ├── dataset.json
│   ├── imagesTr
│   ├── imagesTs
│   ├── labelsTr
│   ├── labelsTs
│   ├── markdown_images
│   ├── nnunet_preprocess_and_train.sh
│   └── report.md
└── nnUNet_results
```

2. **Training Size**
Expand Down Expand Up @@ -231,7 +235,7 @@ The contours can be selected by creating a YAML file to define a regular express

```sh
OUTPUT_DIRECTORY
├── 0_subject1_0000.nii.gz
├── {DATASET}_{SUBJECT_NUM}_{MODALITY}.nii.gz
└── ...
```

Expand All @@ -246,8 +250,15 @@ The contours can be selected by creating a YAML file to define a regular express
A dataset json file may look like this:
```json
{
"modality":{
"0": "CT"
}
"channel_names": {
"0": "CT"
},
"labels": {
"background": 0,
"GTV": 1
},
"numTraining": 5,
"file_ending": ".nii.gz",
"licence": "hands off!"
}
```
155 changes: 20 additions & 135 deletions docs/nnUNet.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,122 +19,7 @@ autopipeline\
Modalities can also be set to `--modalities MR,RTSTRUCT`

AutoPipeline offers many more options and features for you to customize your outputs: <<https://github.com/bhklab/med-imagetools/tree/main/README.md>
>.

## nnUNet Preprocess and Train

### One-Step Preprocess and Train

Med-ImageTools generates a file in your output folder called `nnunet_preprocess_and_train.sh` that combines all the commands needed for preprocessing and training your nnUNet model. Run that shell script to get a fully trained nnUNet model.

Alternatively, you can go through each step individually as follows below:

### nnUNet Preprocessing

Follow the instructions for setting up your paths for nnUNet: <https://github.com/MIC-DKFZ/nnUNet/blob/master/documentation/setting_up_paths.md>

Med-ImageTools generates the dataset.json that nnUNet requires in the output directory that you specify.

The generated output directory structure will look something like:

```sh
OUTPUT_DIRECTORY
├── nnUNet_preprocessed
├── nnUNet_raw_data_base
│ └── nnUNet_raw_data
│ └── Task500_HNSCC
│ ├── nnunet_preprocess_and_train.sh
│ └── ...
└── nnUNet_trained_models
```

nnUNet requires that environment variables be set before any commands are executed. To temporarily set them, run the following:

```sh
export nnUNet_raw_data_base="/OUTPUT_DIRECTORY/nnUNet_raw_data_base"
export nnUNet_preprocessed="/OUTPUT_DIRECTORY/nnUNet_preprocessed"
export RESULTS_FOLDER="/OUTPUT_DIRECTORY/nnUNet_trained_models"
```

To permanently set these environment variables, make sure that in your `~/.bashrc` file, these environment variables are set for nnUNet. The `nnUNet_preprocessed` and `nnUNet_trained_models` folders are generated as empty folders for you by Med-ImageTools. `nnUNet_raw_data_base` is populated with the required raw data files. Add this to the file:

```sh
export nnUNet_raw_data_base="/OUTPUT_DIRECTORY/nnUNet_raw_data_base"
export nnUNet_preprocessed="/OUTPUT_DIRECTORY/nnUNet_preprocessed"
export RESULTS_FOLDER="/OUTPUT_DIRECTORY/nnUNet_trained_models"
```

Then, execute the command:

```sh
source ~/.bashrc
```

Too allow nnUNet to preprocess your data for training, run the following command. Set XXX to the ID that you want to preprocess. This is your task ID. For example, for Task500_HNSCC, the task ID is 500. Task IDs must be between 500 and 999, so Med-ImageTools can run 500 instances with the `--nnunet` flag in a single output folder.

```sh
nnUNet_plan_and_preprocess -t XXX --verify_dataset_integrity
```

### nnUNet Training

Once nnUNet has finished preprocessing, you may begin training your nnUNet model. To train your model, run the following command. Learn more about nnUNet's options here: <https://github.com/MIC-DKFZ/nnUNet#model-training>

```sh
nnUNet_train CONFIGURATION TRAINER_CLASS_NAME TASK_NAME_OR_ID FOLD
```

## nnUNet Inference

For inference data, nnUNet requires data to be in a different output format. To run AutoPipeline for nnUNet inference, run the following command:

```sh
autopipeline\
[INPUT_DIRECTORY] \
[OUTPUT_DIRECTORY] \
--modalities CT \
--nnunet_inference \
--dataset_json_path [DATASET_JSON_PATH]
```
To execute this command AutoPipeline needs a json file with the image modality definitions.

Modalities can also be set to `--modalities MR`.

The directory structue will look like:

```sh
OUTPUT_DIRECTORY
├── 0_subject1_0000.nii.gz
└── ...
```

To run inference, run the command:

```sh
nnUNet_predict -i INPUT_FOLDER -o OUTPUT_FOLDER -t TASK_NAME_OR_ID -m CONFIGURATION
```

In this case, the `INPUT_FOLDER` of nnUNet is the `OUTPUT_DIRECTORY` of Med-ImageTools.# Preparing Data for nnUNet

nnUNet repo can be found at: <https://github.com/MIC-DKFZ/nnUNet>

## Processing DICOM Data with Med-ImageTools

Ensure that you have followed the steps in <https://github.com/bhklab/med-imagetools#installing-med-imagetools> before proceeding.

To convert your data from DICOM to NIfTI for training an nnUNet auto-segmentation model, run the following command:

```sh
autopipeline\
[INPUT_DIRECTORY] \
[OUTPUT_DIRECTORY] \
--modalities CT,RTSTRUCT \
--nnunet
```

Modalities can also be set to `--modalities MR,RTSTRUCT`

AutoPipeline offers many more options and features for you to customize your outputs: <https://github.com/bhklab/med-imagetools/imgtools/README.md>.

## nnUNet Preprocess and Train

Expand All @@ -155,28 +40,28 @@ The generated output directory structure will look something like:
```sh
OUTPUT_DIRECTORY
├── nnUNet_preprocessed
├── nnUNet_raw_data_base
└── nnUNet_raw_data
── Task500_HNSCC
── nnunet_preprocess_and_train.sh
└── ...
└── nnUNet_trained_models
├── nnUNet_raw
   └── Dataset001_HNSCC
   ── nnunet_preprocess_and_train.sh
   ── ...
└── nnUNet_results

```

nnUNet requires that environment variables be set before any commands are executed. To temporarily set them, run the following:

```sh
export nnUNet_raw_data_base="/OUTPUT_DIRECTORY/nnUNet_raw_data_base"
export nnUNet_raw="/OUTPUT_DIRECTORY/nnUNet_raw"
export nnUNet_preprocessed="/OUTPUT_DIRECTORY/nnUNet_preprocessed"
export RESULTS_FOLDER="/OUTPUT_DIRECTORY/nnUNet_trained_models"
export nnUNet_results="/OUTPUT_DIRECTORY/nnUNet_results"
```

To permanently set these environment variables, make sure that in your `~/.bashrc` file, these environment variables are set for nnUNet. The `nnUNet_preprocessed` and `nnUNet_trained_models` folders are generated as empty folders for you by Med-ImageTools. `nnUNet_raw_data_base` is populated with the required raw data files. Add this to the file:
To permanently set these environment variables, make sure that in your `~/.bashrc` file, these environment variables are set for nnUNet. The `nnUNet_preprocessed` and `nnUNet_results` folders are generated as empty folders for you by Med-ImageTools. `nnUNet_raw` is populated with the required raw data files. Add this to the file:

```sh
export nnUNet_raw_data_base="/OUTPUT_DIRECTORY/nnUNet_raw_data_base"
export nnUNet_raw="/OUTPUT_DIRECTORY/nnUNet_raw"
export nnUNet_preprocessed="/OUTPUT_DIRECTORY/nnUNet_preprocessed"
export RESULTS_FOLDER="/OUTPUT_DIRECTORY/nnUNet_trained_models"
export nnUNet_results=="/OUTPUT_DIRECTORY/nnUNet_results"
```

Then, execute the command:
Expand All @@ -185,18 +70,18 @@ Then, execute the command:
source ~/.bashrc
```

Too allow nnUNet to preprocess your data for training, run the following command. Set XXX to the ID that you want to preprocess. This is your task ID. For example, for Task500_HNSCC, the task ID is 500. Task IDs must be between 500 and 999, so Med-ImageTools can run 500 instances with the `--nnunet` flag in a single output folder.
Too allow nnUNet to preprocess your data for training, run the following command. Set X to the ID that you want to preprocess. This is your dataset ID. For example, for Dataset001_HNSCC, the dataset ID is 1. Dataset IDs must be between 1 and 999, so Med-ImageTools can run 999 instances with the `--nnunet` flag in a single output folder.

```sh
nnUNet_plan_and_preprocess -t XXX --verify_dataset_integrity
nnUNetv2_plan_and_preprocess -d X --verify_dataset_integrity -c 3d_fullres
```

### nnUNet Training

Once nnUNet has finished preprocessing, you may begin training your nnUNet model. To train your model, run the following command. Learn more about nnUNet's options here: <https://github.com/MIC-DKFZ/nnUNet#model-training>
Once nnUNet has finished preprocessing, you may begin training your nnUNet model. To train your model, run the following command. Learn more about nnUNet's options here: <https://github.com/MIC-DKFZ/nnUNet/blob/master/documentation/how_to_use_nnunet.md>

```sh
nnUNet_train CONFIGURATION TRAINER_CLASS_NAME TASK_NAME_OR_ID FOLD
nnUNetv2_train DATASET_NAME_OR_ID UNET_CONFIGURATION FOLD
```

## nnUNet Inference
Expand All @@ -218,15 +103,15 @@ Modalities can also be set to `--modalities MR`.
The directory structue will look like:

```sh
OUTPUT_DIRECTORY
├── 0_subject1_0000.nii.gz
└── ...
OUTPUT_DIRECTORY
├── {DATASET}_{SUBJECT_NUM}_{MODALITY}.nii.gz
└── ...
```

To run inference, run the command:

```sh
nnUNet_predict -i INPUT_FOLDER -o OUTPUT_FOLDER -t TASK_NAME_OR_ID -m CONFIGURATION
nnUNetv2_predict -i INPUT_FOLDER -o OUTPUT_FOLDER -d DATASET_NAME_OR_ID -c CONFIGURATION
```

In this case, the `INPUT_FOLDER` of nnUNet is the `OUTPUT_DIRECTORY` of Med-ImageTools.
In this case, the `INPUT_FOLDER` of nnUNet is the `OUTPUT_DIRECTORY` of Med-ImageTools.
Loading