From be63ca02fa6bf64bd099c8626ebe763289eb4641 Mon Sep 17 00:00:00 2001 From: Naga Karthik Date: Mon, 7 Apr 2025 14:11:28 -0400 Subject: [PATCH 01/18] remove unused train.sh script --- scripts/train.sh | 74 ------------------------------------------------ 1 file changed, 74 deletions(-) delete mode 100644 scripts/train.sh diff --git a/scripts/train.sh b/scripts/train.sh deleted file mode 100644 index 9a44b17c..00000000 --- a/scripts/train.sh +++ /dev/null @@ -1,74 +0,0 @@ -#!/bin/bash -# -# This script does the following: -# 1. Creates a virtual environment `venv_monai` and installs the required dependencies -# 2. Generates a MSD-style datalist containing image/label pairs for training -# 3. Trains the contrast-agnostic soft segmentation model -# 4. Evaluates the model on the test set -# -# Usage: -# bash train.sh -# -# Examples: -# 1. Train a model on 'T1w' contrast with 'hard' labels -# bash train.sh /path/to/spine-generic-processed/ t1w hard train.yaml -# 2. Train a model on 'all' contrasts with 'soft' labels -# bash train.sh /path/to/spine-generic-processed/ all soft train.yaml -# -# - - -# Uncomment for full verbose -# set -x - -# Immediately exit if error -set -e -o pipefail - -# Exit if user presses CTRL+C (Linux) or CMD+C (OSX) -trap "echo Caught Keyboard Interrupt within script. Exiting now.; exit" INT - -# Set the following variables to the desired values -# Path to the pre-processed spine-generic dataset -PATH_DATA=$1 -CONTRAST=$2 # options: ["t1w", "t2w", "t2star", "mton", "mtoff", "dwi", "all"] -LABEL_TYPE=$3 # options: ["hard", "soft", "soft_bin"] -PATH_TRAIN_YAML=$4 # path to the yaml file containing the training configuration - -PATH_DATALIST_OUT="../datalists/spine-generic/temp/seed15/" -# create folder if doesn't exist -if [ ! -d $PATH_DATALIST_OUT ]; then - mkdir -p $PATH_DATALIST_OUT -fi - -MODEL="nnunet" - -# # Create virtual environment -# conda create -n venv_monai python=3.9 -y - -# NOTE: running conda activate errors out requesting conda init to be run, -# the eval expression here makes it work without conda init. -# source: https://stackoverflow.com/questions/34534513/calling-conda-source-activate-from-bash-script -eval "$(conda shell.bash hook)" -conda activate venv_monai - -# # Install dependencies quietly -# pip install -r monai/requirements.txt --quiet - -# Run the script to generate the datalist JSON file -python monai/create_msd_data.py \ - --path-data $PATH_DATA \ - --path-out $PATH_DATALIST_OUT \ - --path-joblib "monai/split_datasets_all_seed=15.joblib" \ - --contrast $CONTRAST \ - --label-type $LABEL_TYPE \ - --seed 15 - -echo "----------------------------------------" -echo "Training contrast-agnostic '$MODEL' model on '$CONTRAST' contrasts with '$LABEL_TYPE' labels" -echo "----------------------------------------" - -# Train the model -python monai/main.py --model $MODEL --config $PATH_TRAIN_YAML --debug - -# Deactivate the virtual environment -conda deactivate From 525322641dc4d98df92ac02eabe0f7984afb778e Mon Sep 17 00:00:00 2001 From: Naga Karthik Date: Mon, 7 Apr 2025 14:24:06 -0400 Subject: [PATCH 02/18] add docstring for creating msd datalist json --- nnUnet/02_create_msd_data.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/nnUnet/02_create_msd_data.py b/nnUnet/02_create_msd_data.py index 374b8e56..aa569938 100644 --- a/nnUnet/02_create_msd_data.py +++ b/nnUnet/02_create_msd_data.py @@ -1,3 +1,11 @@ +""" +This script takes as input the path to the original BIDS dataset and outputs a datalist json file + containing the train/val/test splits. To reproduce contrast-agnostic v3.0 training, the script + uses pre-defined splits by default (they can be found under the folder .) + +Authors: Naga Karthik +""" + import os import re import json From a44d300f11b6680fe931b2d9727660e04d8a543d Mon Sep 17 00:00:00 2001 From: Naga Karthik Date: Mon, 7 Apr 2025 14:26:28 -0400 Subject: [PATCH 03/18] minor update readme --- scripts/train_contrast_agnostic.sh | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/scripts/train_contrast_agnostic.sh b/scripts/train_contrast_agnostic.sh index bcb83501..9edc48df 100644 --- a/scripts/train_contrast_agnostic.sh +++ b/scripts/train_contrast_agnostic.sh @@ -1,5 +1,5 @@ #!/bin/bash -# This script is used for reproducing contrast-agnostic v3.1 training and also provides the option to extend the +# This script is used for reproducing contrast-agnostic v3.0 training and also provides the option to extend the # contrast-agnostic spinal cord segmentation model with new datasets. It achieves the following: # 1. Clones the datasets from NeuroPoly's git-annex server. # 2. Creates datalists (i.e. json files with image/label pairs) based on pre-defined or random dataset splits @@ -20,22 +20,23 @@ PATH_REPO="/home/GRAMES.POLYMTL.CA/u114716/contrast-agnostic/contrast-agnostic-s SEED=50 # List of datasets to train on -# NOTE 1: the following datasets were used for training the contrast-agnostic v3.1 model -# https://github.com/sct-pipeline/contrast-agnostic-softseg-spinalcord/releases/tag/v3.1 -# NOTE 2: training on praxis acute SCI data requires special access to spineimage.ca. Because this is different from +# NOTE 1: the following datasets were used for training the contrast-agnostic v3.0 model +# https://github.com/sct-pipeline/contrast-agnostic-softseg-spinalcord/releases/tag/v3.0 +# NOTE 2: training on praxis acute SCI data requires special access to `spineimage.ca`. Because this is different from # the usual downloading from git-annex, this script does not support downloading praxis data. To train contrast-agnostic model # download the dataset manually and store it in PATH_DATA_BASE (see below) -DATASETS=("data-multi-subject" "basel-mp2rage" "canproco" \ - "lumbar-epfl" "lumbar-vanderbilt" "dcm-brno" "dcm-zurich" "dcm-zurich-lesions" "dcm-zurich-lesions-20231115" \ - "sci-paris" "sci-zurich" "sci-colorado" "sct-testing-large" \ - "site_006" "site_007" - ) -DATASETS=("site_006") +# DATASETS=("data-multi-subject" "basel-mp2rage" "canproco" \ +# "lumbar-epfl" "lumbar-vanderbilt" "dcm-brno" "dcm-zurich" "dcm-zurich-lesions" "dcm-zurich-lesions-20231115" \ +# "sci-paris" "sci-zurich" "sci-colorado" "sct-testing-large" \ +# "site_006" "site_007" +# ) +# for debugging purposes, test the script on 1 dataset from the above list +DATASETS=("lumbar-vanderbilt") # Path to the folder where the datasets will be downloaded -# PATH_DATA_BASE="/home/GRAMES.POLYMTL.CA/u114716/datasets" -PATH_DATA_BASE="/scratch/naga/contrast_agnostic/datasets" +PATH_DATA_BASE="/home/GRAMES.POLYMTL.CA//datasets" +# PATH_DATA_BASE="/scratch/naga/contrast_agnostic/datasets" # Path to the output folder where the dataset in MSD-style format will be saved as json files with image/label pairs # and other dataset-related statistics. To keep track of the experiments, date is also appended as a prefix or suffix From ca36cec3bdc0397bd3c76881286451bd224e31a1 Mon Sep 17 00:00:00 2001 From: Naga Karthik Date: Mon, 7 Apr 2025 14:34:32 -0400 Subject: [PATCH 04/18] pin package versions --- nnUnet/requirements.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/nnUnet/requirements.txt b/nnUnet/requirements.txt index d3d5f5a0..9db781cc 100644 --- a/nnUnet/requirements.txt +++ b/nnUnet/requirements.txt @@ -1,9 +1,9 @@ -nibabel -seaborn -matplotlib -pandas -numpy +nibabel==5.1.0 +seaborn==0.13.2 +matplotlib==3.7.1 +pandas==2.0.1 +numpy<2.0.0 tqdm -pyyaml +pyyaml==6.0 loguru -scikit-learn \ No newline at end of file +scikit-learn==1.2.2 \ No newline at end of file From 784b34bbe6ca8bc108afe9318a08acafdc9761fd Mon Sep 17 00:00:00 2001 From: Naga Karthik Date: Mon, 7 Apr 2025 14:38:52 -0400 Subject: [PATCH 05/18] update training instructions --- README.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d6ed5f46..66708f63 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ note = {Shared authorship -- authors contributed equally} 1. Create a conda environment with the following command: ```bash -conda create -n contrast_agnostic python=3.9 +conda create -n contrast_agnostic python=3.9.16 ``` 2. Activate the environment with the following command: @@ -64,8 +64,8 @@ git clone https://github.com/sct-pipeline/contrast-agnostic-softseg-spinalcord.g 3. Install the required packages with the following command: ```bash -cd contrast-agnostic-softseg-spinalcord/nnUnet -pip install -r requirements.txt +cd contrast-agnostic-softseg-spinalcord +pip install -r nnUnet/requirements.txt ``` > **Note** @@ -74,11 +74,16 @@ pip install -r requirements.txt ### Step 2: Train the model -The script `scripts/train_contrast_agnostic.sh` downloads the datasets from git-annex, creates datalists, converts them into nnUNet-specific format, and trains the model. More instructions about what variables to set and which datasets to use can be found in the script itself. Once these variables are set, the script can be run simply as follows: +The script `scripts/train_contrast_agnostic.sh` downloads the datasets from git-annex, creates datalists, converts them into nnUNet-specific format, and trains the model. More instructions about what variables to set and which datasets to use can be found in the script itself. Once these variables are set, run: ```bash bash scripts/train_contrast_agnostic.sh ``` + +> [!IMPORTANT] +> The script `train_contrast_agnostic.sh` will NOT run out-of-the-box. User-specific variables such as the path to download datasets and nnUnet repository need to be set. Info about which varibles to set can be found in the script itself. + +