Skip to content

Commit b73709c

Browse files
authored
Merge pull request #66 from ivadomed/plb/nnunet-region-based
Region based nnUNet scripts
2 parents 92f635e + a4b20f4 commit b73709c

8 files changed

+3227
-0
lines changed

exclude_M0_M12_comparison.yml

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# PSIR
2+
- sub-mon118_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/43
3+
- 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
4+
- sub-mon009_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758684880
5+
- sub-mon032_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758686509
6+
- sub-mon097_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758686772
7+
- sub-mon113_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1760175861
8+
- sub-mon148_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758686965
9+
- sub-mon152_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1760248384
10+
- sub-mon168_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758687112
11+
- sub-mon191_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758687352
12+
- sub-van134_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1761698648
13+
- sub-van135_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1761702591
14+
- sub-van171_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1761910863
15+
- sub-van176_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758687602
16+
- sub-van181_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1767235494
17+
- sub-van201_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1767238882
18+
- sub-van206_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758688851
19+
- sub-van207_ses-M0 # No disc labeling file : impossible to do it : https://github.com/ivadomed/canproco/issues/53#issuecomment-1761973064
20+
- sub-tor014_ses-M0 # Poor data quality and no disc labeling file; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1760420705
21+
- sub-tor133_ses-M0 # Poor data quality; see https://github.com/ivadomed/canproco/issues/53#issuecomment-1758689928
22+
# STIR
23+
- sub-cal149_ses-M0 # FOV does not cover the whole SC in sagittal plane; see https://github.com/ivadomed/canproco/issues/22#issue-1568568207
24+
# Similar accross timepoints
25+
- sub-edm005_ses-M0
26+
- sub-edm031_ses-M0
27+
- sub-edm035_ses-M0
28+
- sub-edm054_ses-M0
29+
- sub-edm065_ses-M0
30+
- sub-edm088_ses-M0
31+
- sub-mon002_ses-M0
32+
- sub-mon003_ses-M0
33+
- sub-mon005_ses-M0
34+
- sub-mon006_ses-M0
35+
- sub-mon007_ses-M0
36+
- sub-mon009_ses-M0
37+
- sub-mon010_ses-M0
38+
- sub-mon011_ses-M0
39+
- sub-mon014_ses-M0
40+
- sub-mon031_ses-M0
41+
- sub-mon032_ses-M0
42+
- sub-mon041_ses-M0
43+
- sub-mon052_ses-M0
44+
- sub-mon060_ses-M0
45+
- sub-tor010_ses-M0
46+
- sub-tor012_ses-M0
47+
- sub-tor014_ses-M0
48+
- sub-tor016_ses-M0
49+
- sub-tor021_ses-M0
50+
- sub-tor029_ses-M0
51+
- sub-tor036_ses-M0
52+
- sub-tor038_ses-M0
53+
- sub-tor039_ses-M0
54+
- sub-tor043_ses-M0
55+
- sub-tor044_ses-M0
56+
- sub-tor051_ses-M0
57+
# Subjects only present at M12
58+
- sub-cal064_ses-M12
59+
- sub-edm161_ses-M12
60+
- sub-mon164_ses-M12
61+
# Subject with 4 dimensions
62+
- sub-cal123_ses-M12
63+
# Subject only present at M0
64+
- sub-cal080_ses-M0
65+
- sub-cal113_ses-M0
66+
- sub-cal125_ses-M0
67+
- sub-cal130_ses-M0
68+
- sub-cal131_ses-M0
69+
- sub-cal136_ses-M0
70+
- sub-cal140_ses-M0
71+
- sub-cal151_ses-M0
72+
- sub-cal153_ses-M0
73+
- sub-cal154_ses-M0
74+
- sub-cal156_ses-M0
75+
- sub-cal162_ses-M0
76+
- sub-cal173_ses-M0
77+
- sub-cal185_ses-M0
78+
- sub-cal191_ses-M0
79+
- sub-cal197_ses-M0
80+
- sub-cal198_ses-M0
81+
- sub-cal199_ses-M0
82+
- sub-cal200_ses-M0
83+
- sub-cal206_ses-M0
84+
- sub-cal207_ses-M0
85+
- sub-cal209_ses-M0
86+
- sub-cal210_ses-M0
87+
- sub-cal213_ses-M0
88+
- sub-cal214_ses-M0
89+
- sub-edm008_ses-M0
90+
- sub-edm010_ses-M0
91+
- sub-edm018_ses-M0
92+
- sub-edm023_ses-M0
93+
- sub-edm029_ses-M0
94+
- sub-edm034_ses-M0
95+
- sub-edm038_ses-M0
96+
- sub-edm053_ses-M0
97+
- sub-edm064_ses-M0
98+
- sub-edm067_ses-M0
99+
- sub-edm089_ses-M0
100+
- sub-edm095_ses-M0
101+
- sub-edm098_ses-M0
102+
- sub-edm107_ses-M0
103+
- sub-edm138_ses-M0
104+
- sub-edm157_ses-M0
105+
- sub-edm162_ses-M0
106+
- sub-edm164_ses-M0
107+
- sub-edm169_ses-M0
108+
- sub-edm178_ses-M0
109+
- sub-edm181_ses-M0
110+
- sub-edm183_ses-M0
111+
- sub-mon013_ses-M0
112+
- sub-mon027_ses-M0
113+
- sub-mon064_ses-M0
114+
- sub-mon107_ses-M0
115+
- sub-mon108_ses-M0
116+
- sub-mon109_ses-M0
117+
- sub-mon139_ses-M0
118+
- sub-mon163_ses-M0 # parent folder exist, missing only PSIR contrast
119+
- sub-mon180_ses-M0
120+
- sub-mon181_ses-M0
121+
- sub-tor004_ses-M0
122+
- sub-tor018_ses-M0
123+
- sub-tor024_ses-M0
124+
- sub-tor025_ses-M0
125+
- sub-tor031_ses-M0
126+
- sub-tor032_ses-M0
127+
- sub-tor041_ses-M0
128+
- sub-tor055_ses-M0
129+
- sub-tor063_ses-M0
130+
- sub-tor066_ses-M0
131+
- sub-tor073_ses-M0
132+
- sub-tor076_ses-M0
133+
- sub-tor077_ses-M0
134+
- sub-tor106_ses-M0
135+
- sub-tor109_ses-M0
136+
- sub-tor110_ses-M0
137+
- sub-tor112_ses-M0
138+
- sub-tor124_ses-M0
139+
- sub-tor125_ses-M0
140+
- sub-tor147_ses-M0
141+
- sub-van009_ses-M0
142+
- sub-van093_ses-M0
143+
- sub-van112_ses-M0
144+
- sub-van116_ses-M0
145+
- sub-van123_ses-M0
146+
- sub-van125_ses-M0
147+
- sub-van129_ses-M0
148+
- sub-van131_ses-M0
149+
- sub-van134_ses-M0
150+
- sub-van144_ses-M0
151+
- sub-van145_ses-M0
152+
- sub-van147_ses-M0
153+
- sub-van149_ses-M0
154+
- sub-van150_ses-M0
155+
- sub-van151_ses-M0
156+
- sub-van154_ses-M0
157+
- sub-van168_ses-M0
158+
- sub-van172_ses-M0
159+
- sub-van173_ses-M0
160+
- sub-van178_ses-M0
161+
- sub-van184_ses-M0
162+
- sub-van186_ses-M0
163+
- sub-van204_ses-M0
164+
- sub-van210_ses-M0
165+
- sub-van211_ses-M0
166+
- sub-van214_ses-M0
167+
- sub-van215_ses-M0
168+
- sub-van217_ses-M0
169+
- sub-van219_ses-M0
170+
- sub-van221_ses-M0
171+
- sub-van222_ses-M0

nnunet/README.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Installation instructions
2+
3+
## Installation of Anima metrics
4+
5+
Installation of
6+
7+
```
8+
cd ~
9+
mkdir anima/
10+
cd anima/
11+
wget -q https://github.com/Inria-Empenn/Anima-Public/releases/download/v4.2/Anima-macOS-4.2.zip # for MACOS
12+
unzip Anima-macOS-4.2.zip
13+
rm Anima-macOS-4.2.zip
14+
git lfs install
15+
git clone --depth 1 https://github.com/Inria-Visages/Anima-Scripts-Public.git
16+
git clone --depth 1 https://github.com/Inria-Visages/Anima-Scripts-Data-Public.git
17+
```
18+
19+
Configure directories
20+
21+
```
22+
cd ~
23+
mkdir .anima/
24+
touch .anima/config.txt
25+
26+
echo "[anima-scripts]" >> .anima/config.txt
27+
echo "anima = ${HOME}/anima/Anima-Binaries-4.2/" >> .anima/config.txt
28+
echo "anima-scripts-public-root = ${HOME}/anima/Anima-Scripts-Public/" >> .anima/config.txt
29+
echo "extra-data-root = ${HOME}/anima/Anima-Scripts-Data-Public/" >> .anima/config.txt
30+
```
31+
32+
## Installation of required libraries
33+
34+
Create a virtual invironment:
35+
~~~
36+
conda create -n venv_nnunet python=3.9
37+
~~~
38+
39+
Activate the environment with the following command:
40+
~~~
41+
conda activate venv_nnunet
42+
~~~
43+
44+
To install required libraries to train an nnUNet v2:
45+
46+
```
47+
pip install -r requirements_nnunet.txt
48+
```
49+
50+
Install SpinalCordToolbox 6.0 :
51+
52+
Installation link : https://spinalcordtoolbox.com/user_section/installation.html
53+
54+
55+
# Data preparation
56+
57+
Create the following folders:
58+
59+
~~~
60+
mkdir nnUNet_raw
61+
mkdir nnUNet_preprocessed
62+
mkdir nnUNet_results
63+
~~~
64+
65+
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.
66+
67+
Convert the data to the nnUNet format :
68+
69+
~~~
70+
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
71+
~~~
72+
73+
> **Note**
74+
> The test ratio is 0.2 for 20% (train ratio is therefore 80%). For M0 images, the time point is ses-M0.
75+
76+
To mutliply PSIR images by -1 before training and convert the data to the nnUNet format :
77+
78+
~~~
79+
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
80+
~~~
81+
82+
# Model training
83+
84+
Before training the model, nnU-Net performs data preprocessing and checks the integrity of the dataset:
85+
86+
~~~
87+
export nnUNet_raw="/path/to/nnUNet_raw"
88+
export nnUNet_preprocessed="/path/to/nnUNet_preprocessed"
89+
export nnUNet_results="/path/to/nnUNet_results"
90+
91+
nnUNetv2_plan_and_preprocess -d DATASET-ID --verify_dataset_integrity
92+
~~~
93+
94+
You will get the configuration plan for all four configurations (2d, 3d_fullres, 3d_lowres, 3d_cascade_fullres).
95+
96+
To train the model, use the following command:
97+
~~~
98+
CUDA_VISIBLE_DEVICES=XXX nnUNetv2_train DATASET-ID CONFIG FOLD --npz
99+
~~~
100+
101+
> **Note**
102+
> Example for Dataset 101, on 2d config on fold 0: CUDA_VISIBLE_DEVICES=2 nnUNetv2_train 101 2d 0 --npz
103+
104+
# Model inference
105+
106+
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`.
107+
108+
Then perform inference:
109+
~~~
110+
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
111+
~~~
112+
113+
# Inference evaluation
114+
115+
First, convert the predictions back the BIDS format ; this only keeps the lesion segmentation and discards spinal cord segmentation :
116+
117+
~~~
118+
python convert_predictions_to_BIDS.py --pred-folder /path/to/predictions --out-folder /path/to/output/folder --conversion-dict /path/to/conversion/dict
119+
~~~
120+
121+
If you are converting predictions which are not the evaluations set from nnUNet, use the flag `--not-imageTs`.
122+
123+
Then, you can evaluate the lesion prediction with Anima metrics
124+
125+
~~~
126+
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
127+
~~~
128+
129+
# Evaluation analysis
130+
131+
The following Notebook `nnUNet_inference_analysis.ipynb` was used to perform analysis of the nnUNet segmentations.
132+
To use it, change the path to the csv files.

0 commit comments

Comments
 (0)