From 26f7fb5cb345fd9641bda0c988513e1e2e206858 Mon Sep 17 00:00:00 2001 From: valosekj Date: Mon, 26 Aug 2024 15:58:15 -0400 Subject: [PATCH 1/3] Add script to run SCIsegV2 inference on the spine-generic multi-subject dataset --- baselines/run_inference_spine-generic.sh | 95 ++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 baselines/run_inference_spine-generic.sh diff --git a/baselines/run_inference_spine-generic.sh b/baselines/run_inference_spine-generic.sh new file mode 100644 index 0000000..048c5d3 --- /dev/null +++ b/baselines/run_inference_spine-generic.sh @@ -0,0 +1,95 @@ +#!/bin/bash +# +# Segment SC and lesions using our nnUNet SCIsegV2 model on spine-generic multi-subject T2w iso images and generate +# QC report +# +# Usage: +# sct_run_batch -config config.json +# +# Example of config.json: +# { +# "path_data" : "data-multi-subject", +# "path_output" : "data-multi-subject_2024-08-25", +# "script" : "/model_seg_sci/baselines/run_inference_spine-generic.sh", +# "jobs" : 8 +# } +# +# +# Author: Jan Valosek +# + +# 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 + +# Print retrieved variables from the sct_run_batch script to the log (to allow easier debug) +echo "Retrieved variables from from the caller sct_run_batch:" +echo "PATH_DATA: ${PATH_DATA}" +echo "PATH_DATA_PROCESSED: ${PATH_DATA_PROCESSED}" +echo "PATH_RESULTS: ${PATH_RESULTS}" +echo "PATH_LOG: ${PATH_LOG}" +echo "PATH_QC: ${PATH_QC}" + +SUBJECT=$1 + +echo "SUBJECT: ${SUBJECT}" + +# ------------------------------------------------------------------------------ +# SCRIPT STARTS HERE +# ------------------------------------------------------------------------------ +# get starting time: +start=`date +%s` + +# Display useful info for the log, such as SCT version, RAM and CPU cores available +sct_check_dependencies -short + +# Go to folder where data will be copied and processed +cd $PATH_DATA_PROCESSED + +# Copy source T2w images +# Note: we use '/./' in order to include the sub-folder 'ses-0X' +# We do a substitution '/' --> '_' in case there is a subfolder 'ses-0X/' +# copy only T2w to save space +rsync -Ravzh ${PATH_DATA}/./${SUBJECT}/anat/${SUBJECT//[\/]/_}_*T2w.* . + +# Go to subject folder for source images +cd ${SUBJECT}/anat + +# ------------------------------------------------------------------------------ +# T2w +# ------------------------------------------------------------------------------ + +# We do a substitution '/' --> '_' in case there is a subfolder 'ses-0X/' +file_t2="${SUBJECT//[\/]/_}"_T2w + +# Check if file_t2 exists +if [[ ! -e ${file_t2}.nii.gz ]]; then + echo "File ${file_t2}.nii.gz does not exist" >> ${PATH_LOG}/missing_files.log + echo "ERROR: File ${file_t2}.nii.gz does not exist. Exiting." + exit 1 +else + # Segment SC and lesion using SCIsegV2 (part of SCT v6.4) + # Note: a single axial QC report contains both SC and lesion segmentations + # Note: we use CUDA_VISIBLE_DEVICES=0 to run the inference on GPU 0; details: + # https://github.com/spinalcordtoolbox/spinalcordtoolbox/pull/4421#issuecomment-2263344151 + CUDA_VISIBLE_DEVICES=0 sct_deepseg -i ${file_t2}.nii.gz -task seg_sc_lesion_t2w_sci -qc ${PATH_QC} -qc-subject ${SUBJECT} +fi + +# ------------------------------------------------------------------------------ +# End +# ------------------------------------------------------------------------------ + +# Display results (to easily compare integrity across SCT versions) +end=`date +%s` +runtime=$((end-start)) +echo +echo "~~~" +echo "SCT version: `sct_version`" +echo "Ran on: `uname -nsr`" +echo "Duration: $(($runtime / 3600))hrs $((($runtime / 60) % 60))min $(($runtime % 60))sec" +echo "~~~" From 9f2573407d2863387fa320224822a1b4726d98e2 Mon Sep 17 00:00:00 2001 From: valosekj Date: Mon, 26 Aug 2024 16:11:44 -0400 Subject: [PATCH 2/3] Add missing 'SCT_USE_GPU=1' --- baselines/run_inference_spine-generic.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baselines/run_inference_spine-generic.sh b/baselines/run_inference_spine-generic.sh index 048c5d3..04381d8 100644 --- a/baselines/run_inference_spine-generic.sh +++ b/baselines/run_inference_spine-generic.sh @@ -75,9 +75,9 @@ if [[ ! -e ${file_t2}.nii.gz ]]; then else # Segment SC and lesion using SCIsegV2 (part of SCT v6.4) # Note: a single axial QC report contains both SC and lesion segmentations - # Note: we use CUDA_VISIBLE_DEVICES=0 to run the inference on GPU 0; details: + # Note: we use CUDA_VISIBLE_DEVICES=0 SCT_USE_GPU=1 to run the inference on GPU 0; details: # https://github.com/spinalcordtoolbox/spinalcordtoolbox/pull/4421#issuecomment-2263344151 - CUDA_VISIBLE_DEVICES=0 sct_deepseg -i ${file_t2}.nii.gz -task seg_sc_lesion_t2w_sci -qc ${PATH_QC} -qc-subject ${SUBJECT} + CUDA_VISIBLE_DEVICES=0 SCT_USE_GPU=1 sct_deepseg -i ${file_t2}.nii.gz -task seg_sc_lesion_t2w_sci -qc ${PATH_QC} -qc-subject ${SUBJECT} fi # ------------------------------------------------------------------------------ From 409dcabc38fd6a08ca3e26fbab0edc29f42b687c Mon Sep 17 00:00:00 2001 From: Jan Valosek <39456460+valosekj@users.noreply.github.com> Date: Wed, 28 Aug 2024 07:20:48 -0400 Subject: [PATCH 3/3] Add ref to SCT v6.4 --- baselines/run_inference_spine-generic.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/baselines/run_inference_spine-generic.sh b/baselines/run_inference_spine-generic.sh index 04381d8..c7fa87d 100644 --- a/baselines/run_inference_spine-generic.sh +++ b/baselines/run_inference_spine-generic.sh @@ -1,7 +1,7 @@ #!/bin/bash # -# Segment SC and lesions using our nnUNet SCIsegV2 model on spine-generic multi-subject T2w iso images and generate -# QC report +# Segment SC and lesions using our nnUNet SCIsegV2 model (part of SCT v6.4) on spine-generic multi-subject T2w iso images +# and generate QC report # # Usage: # sct_run_batch -config config.json