Skip to content

Commit

Permalink
Merge pull request #66 from ivadomed/plb/nnunet-region-based
Browse files Browse the repository at this point in the history
Region based nnUNet scripts
  • Loading branch information
plbenveniste authored Nov 10, 2023
2 parents 92f635e + a4b20f4 commit b73709c
Show file tree
Hide file tree
Showing 8 changed files with 3,227 additions and 0 deletions.
171 changes: 171 additions & 0 deletions exclude_M0_M12_comparison.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# PSIR
- sub-mon118_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/43
- sub-mon006_ses-M0 # FOV covers the whole brain. The SC has only C1-C6; see https://github.com/ivadomed/canproco/issues/22#issue-1568568207
- sub-mon009_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758684880
- sub-mon032_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758686509
- sub-mon097_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758686772
- sub-mon113_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1760175861
- sub-mon148_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758686965
- sub-mon152_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1760248384
- sub-mon168_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758687112
- sub-mon191_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758687352
- sub-van134_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1761698648
- sub-van135_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1761702591
- sub-van171_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1761910863
- sub-van176_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758687602
- sub-van181_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1767235494
- sub-van201_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1767238882
- sub-van206_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758688851
- sub-van207_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1761973064
- sub-tor014_ses-M0 # Poor data quality and no disc labeling file; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1760420705
- sub-tor133_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758689928
# STIR
- sub-cal149_ses-M0 # FOV does not cover the whole SC in sagittal plane; see https://github.com/ivadomed/canproco/issues/22#issue-1568568207
# Similar accross timepoints
- sub-edm005_ses-M0
- sub-edm031_ses-M0
- sub-edm035_ses-M0
- sub-edm054_ses-M0
- sub-edm065_ses-M0
- sub-edm088_ses-M0
- sub-mon002_ses-M0
- sub-mon003_ses-M0
- sub-mon005_ses-M0
- sub-mon006_ses-M0
- sub-mon007_ses-M0
- sub-mon009_ses-M0
- sub-mon010_ses-M0
- sub-mon011_ses-M0
- sub-mon014_ses-M0
- sub-mon031_ses-M0
- sub-mon032_ses-M0
- sub-mon041_ses-M0
- sub-mon052_ses-M0
- sub-mon060_ses-M0
- sub-tor010_ses-M0
- sub-tor012_ses-M0
- sub-tor014_ses-M0
- sub-tor016_ses-M0
- sub-tor021_ses-M0
- sub-tor029_ses-M0
- sub-tor036_ses-M0
- sub-tor038_ses-M0
- sub-tor039_ses-M0
- sub-tor043_ses-M0
- sub-tor044_ses-M0
- sub-tor051_ses-M0
# Subjects only present at M12
- sub-cal064_ses-M12
- sub-edm161_ses-M12
- sub-mon164_ses-M12
# Subject with 4 dimensions
- sub-cal123_ses-M12
# Subject only present at M0
- sub-cal080_ses-M0
- sub-cal113_ses-M0
- sub-cal125_ses-M0
- sub-cal130_ses-M0
- sub-cal131_ses-M0
- sub-cal136_ses-M0
- sub-cal140_ses-M0
- sub-cal151_ses-M0
- sub-cal153_ses-M0
- sub-cal154_ses-M0
- sub-cal156_ses-M0
- sub-cal162_ses-M0
- sub-cal173_ses-M0
- sub-cal185_ses-M0
- sub-cal191_ses-M0
- sub-cal197_ses-M0
- sub-cal198_ses-M0
- sub-cal199_ses-M0
- sub-cal200_ses-M0
- sub-cal206_ses-M0
- sub-cal207_ses-M0
- sub-cal209_ses-M0
- sub-cal210_ses-M0
- sub-cal213_ses-M0
- sub-cal214_ses-M0
- sub-edm008_ses-M0
- sub-edm010_ses-M0
- sub-edm018_ses-M0
- sub-edm023_ses-M0
- sub-edm029_ses-M0
- sub-edm034_ses-M0
- sub-edm038_ses-M0
- sub-edm053_ses-M0
- sub-edm064_ses-M0
- sub-edm067_ses-M0
- sub-edm089_ses-M0
- sub-edm095_ses-M0
- sub-edm098_ses-M0
- sub-edm107_ses-M0
- sub-edm138_ses-M0
- sub-edm157_ses-M0
- sub-edm162_ses-M0
- sub-edm164_ses-M0
- sub-edm169_ses-M0
- sub-edm178_ses-M0
- sub-edm181_ses-M0
- sub-edm183_ses-M0
- sub-mon013_ses-M0
- sub-mon027_ses-M0
- sub-mon064_ses-M0
- sub-mon107_ses-M0
- sub-mon108_ses-M0
- sub-mon109_ses-M0
- sub-mon139_ses-M0
- sub-mon163_ses-M0 # parent folder exist, missing only PSIR contrast
- sub-mon180_ses-M0
- sub-mon181_ses-M0
- sub-tor004_ses-M0
- sub-tor018_ses-M0
- sub-tor024_ses-M0
- sub-tor025_ses-M0
- sub-tor031_ses-M0
- sub-tor032_ses-M0
- sub-tor041_ses-M0
- sub-tor055_ses-M0
- sub-tor063_ses-M0
- sub-tor066_ses-M0
- sub-tor073_ses-M0
- sub-tor076_ses-M0
- sub-tor077_ses-M0
- sub-tor106_ses-M0
- sub-tor109_ses-M0
- sub-tor110_ses-M0
- sub-tor112_ses-M0
- sub-tor124_ses-M0
- sub-tor125_ses-M0
- sub-tor147_ses-M0
- sub-van009_ses-M0
- sub-van093_ses-M0
- sub-van112_ses-M0
- sub-van116_ses-M0
- sub-van123_ses-M0
- sub-van125_ses-M0
- sub-van129_ses-M0
- sub-van131_ses-M0
- sub-van134_ses-M0
- sub-van144_ses-M0
- sub-van145_ses-M0
- sub-van147_ses-M0
- sub-van149_ses-M0
- sub-van150_ses-M0
- sub-van151_ses-M0
- sub-van154_ses-M0
- sub-van168_ses-M0
- sub-van172_ses-M0
- sub-van173_ses-M0
- sub-van178_ses-M0
- sub-van184_ses-M0
- sub-van186_ses-M0
- sub-van204_ses-M0
- sub-van210_ses-M0
- sub-van211_ses-M0
- sub-van214_ses-M0
- sub-van215_ses-M0
- sub-van217_ses-M0
- sub-van219_ses-M0
- sub-van221_ses-M0
- sub-van222_ses-M0
132 changes: 132 additions & 0 deletions nnunet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Installation instructions

## Installation of Anima metrics

Installation of

```
cd ~
mkdir anima/
cd anima/
wget -q https://github.com/Inria-Empenn/Anima-Public/releases/download/v4.2/Anima-macOS-4.2.zip # for MACOS
unzip Anima-macOS-4.2.zip
rm Anima-macOS-4.2.zip
git lfs install
git clone --depth 1 https://github.com/Inria-Visages/Anima-Scripts-Public.git
git clone --depth 1 https://github.com/Inria-Visages/Anima-Scripts-Data-Public.git
```

Configure directories

```
cd ~
mkdir .anima/
touch .anima/config.txt
echo "[anima-scripts]" >> .anima/config.txt
echo "anima = ${HOME}/anima/Anima-Binaries-4.2/" >> .anima/config.txt
echo "anima-scripts-public-root = ${HOME}/anima/Anima-Scripts-Public/" >> .anima/config.txt
echo "extra-data-root = ${HOME}/anima/Anima-Scripts-Data-Public/" >> .anima/config.txt
```

## Installation of required libraries

Create a virtual invironment:
~~~
conda create -n venv_nnunet python=3.9
~~~

Activate the environment with the following command:
~~~
conda activate venv_nnunet
~~~

To install required libraries to train an nnUNet v2:

```
pip install -r requirements_nnunet.txt
```

Install SpinalCordToolbox 6.0 :

Installation link : https://spinalcordtoolbox.com/user_section/installation.html


# Data preparation

Create the following folders:

~~~
mkdir nnUNet_raw
mkdir nnUNet_preprocessed
mkdir nnUNet_results
~~~

We are training a region-based nnUNet taking an image from contrasts PSIR or STIR and creating a mask with 0=background, 1=spinal cord and 2=MS lesion.

Convert the data to the nnUNet format :

~~~
python convert_BIDS_to_nnunet.py --path-data /path/to/BIDS/dataset --path-out /path/to/nnUNet_raw --taskname TASK-NAME --tasknumber DATASET-ID --contrasts PSIR,STIR --test-ratio XX --time-point ses-XX --type training --exclude-file /path/to/exclude_file.yml
~~~

> **Note**
> The test ratio is 0.2 for 20% (train ratio is therefore 80%). For M0 images, the time point is ses-M0.
To mutliply PSIR images by -1 before training and convert the data to the nnUNet format :

~~~
python convert_BIDS_to_nnunet_with_mul_PSIR.py --path-data /path/to/BIDS/dataset --path-out /path/to/nnUNet_raw --taskname TASK-NAME --tasknumber DATASET-ID --contrasts PSIR,STIR --test-ratio XX --time-point ses-XX --type training --exclude-file /path/to/exclude_file.yml
~~~

# Model training

Before training the model, nnU-Net performs data preprocessing and checks the integrity of the dataset:

~~~
export nnUNet_raw="/path/to/nnUNet_raw"
export nnUNet_preprocessed="/path/to/nnUNet_preprocessed"
export nnUNet_results="/path/to/nnUNet_results"
nnUNetv2_plan_and_preprocess -d DATASET-ID --verify_dataset_integrity
~~~

You will get the configuration plan for all four configurations (2d, 3d_fullres, 3d_lowres, 3d_cascade_fullres).

To train the model, use the following command:
~~~
CUDA_VISIBLE_DEVICES=XXX nnUNetv2_train DATASET-ID CONFIG FOLD --npz
~~~

> **Note**
> Example for Dataset 101, on 2d config on fold 0: CUDA_VISIBLE_DEVICES=2 nnUNetv2_train 101 2d 0 --npz
# Model inference

Convert data to nnUNet format for inference using `convert_BIDS_to_nnunet.py` or `convert_BIDS_to_nnunet_with_mul_PSIR.py` with `--type=inference`.

Then perform inference:
~~~
CUDA_VISIBLE_DEVICES=XXX nnUNetv2_predict -i /path/to/image/folder -o /path/to/predictions -d DATASET_ID -c CONFIG --save_probabilities -chk checkpoint_best.pth -f FOLD
~~~

# Inference evaluation

First, convert the predictions back the BIDS format ; this only keeps the lesion segmentation and discards spinal cord segmentation :

~~~
python convert_predictions_to_BIDS.py --pred-folder /path/to/predictions --out-folder /path/to/output/folder --conversion-dict /path/to/conversion/dict
~~~

If you are converting predictions which are not the evaluations set from nnUNet, use the flag `--not-imageTs`.

Then, you can evaluate the lesion prediction with Anima metrics

~~~
python evaluate_lesion_seg_prediction.py --pred-folder path/to/predictions --dataset path/to/dataset --animaPath path/to/animaSegPerfAnalyzer --output-folder path/to/output_folder
~~~

# Evaluation analysis

The following Notebook `nnUNet_inference_analysis.ipynb` was used to perform analysis of the nnUNet segmentations.
To use it, change the path to the csv files.
Loading

0 comments on commit b73709c

Please sign in to comment.