GithubHelp home page GithubHelp logo

hd-bm's Introduction

HD-BM

Introduction

This repository provides easy to use access to our HD-BM brain metastasis segmentation tool. HD-BM is the result of a joint project between the Department of Neuroradiology at the Heidelberg University Hospital, Germany and the Division of Medical Image Computing at the German Cancer Research Center (DKFZ) Heidelberg, Germany. If you are using HD-BM, please cite the following publications:

  1. Pflüger, I., Wald, T., Isensee, F., et al. (2022). Automated detection and quantification of brain metastases on clinical MRI data using artificial neural networks. Neuro-Oncology Advances. vdac138. https://doi.org/10.1093/noajnl/vdac138

  2. Isensee, F., Jaeger, P. F., Kohl, S. A., Petersen, J., & Maier-Hein, K. H. (2020). nnU-Net: a self-configuring method for deep learning-based biomedical image segmentation. Nature Methods, 1-9.

HD-BM was developed with 308 patients with clinically diagnosed BM from several primary tumors that were treated at the Heidelberg University Hospital (Heidelberg, Germany) and who underwent standardized MRI examination at the Department of Neuroradiology for radiation therapy between 2011 and 2018. The patients were assigned to either training or test set with a 4:1 ratio. Specifically, the training set consisted of n = 246 patients (with n = 246 MRI exams, i.e., one exam per patient) at any point in the disease course, whereas the institutional test set consisted of n = 62 patients. HD-BM was further validated on an external test set of 30 adult patients with BMs from the Heidelberg Thoracic Clinic, mainly originating from lung cancer, treated between 2013 and 2019.

MRI exams were acquired with a 3-T MR imaging system (Magnetom Verio, Skyra or Trio TIM; Siemens Healthcare) except a single measurement which was acquired on a 1.5-T field strength (Magnetom Avanto; Siemens Healthineers). All MRI exams of the external test set were acquired on a 1.5-T MRI system (Magnetom Avanto; Siemens Healthineers). All MRI scans in all Datasets included T1-weighted images before and after gadolinium contrast agent and FLAIR images (detailed description of acquisition parameters in the Supplement).

Given these modalities we provide two models that are capable to automatically predict BM of patients:

  1. HD-BM, which uses a T1-weighted, contrast enhanced T1-weighted, FLAIR and a T1-subtraction image
  2. HD-BM Slim, which only needs a contrast enhanced T1-weighted and a FLAIR image

Installation Instructions

Installation Requirements

HD-BM only runs on Linux with python3. Supported python3 versions are python3.6. Python 3.9 doesn't work as downloading of weights breaks. (Might work after downloading the weights manually and extracting them in the directory specified by paths.py). In order to run a pc with a GPU with at least 4 GB of VRAM and cuda/pytorch support is required. Running the prediction on CPU is not supported.

Installation with Pip

Installation with pip is currently not supported.

Manual installation

We generally recommend to create a new virtualenv for every project that is installed so package dependencies don't get mixed. To test if virtualenv is installed call virtualenv --version. This will return something like this: virtualenv 20.0.17 from <SOME_PATH> should it be installed. (Should it not be installed follow this how-to to install it (Optional).)

Installing with a virtualenv

# With virtualenv
virtualenv HD-BM-env --python=python3.6  # Creates a new Virtual environment 
source HD-BM-env/bin/activate  # Activates the environment

git clone [email protected]:NeuroAI-HD/HD-BM.git  # Clones the Repository
pip install HD-BM/  # Install the repository for the current virtualenv

Installing without virtualenv

# Without virtualenv
git clone [email protected]:NeuroAI-HD/HD-BM.git  # Clones the repository
pip3 install HD-BM/  # Installs the repository for the python3 interpreter

This will install HD-BM commands directly onto your system (or the virtualenv). You can use them from anywhere (when the virtualenv is active).

How to use it

Using HD-BM is straight forward. After installing you can call hd_bm_predict or hd_bm_predict_folder to predict cases with all four MRI modalities or call hd_bm_slim_predict or hd_bm_slim_folder to predict cases that are imaged with contrast enhanced T1-weighted and FLAIR MRI sequence.

As these commands are called for the first time the weights for the nnUNet model will be downloaded automatically and saved to your home directory. Should you want to change the location where weights are to be stored edit the file located in /<YOUR_PATH>/HD-BM/hd_bm/paths.py.

Prerequisites

HD-BM was trained with two/four MRI modalities: (optional) T1,constrast-enhanced T1, FLAIR and (optional), T1 subtraction image.

All input files must be provided as nifti (.nii.gz) files containing 2D or 3D MRI image data. Sequences with multiple temporal volumes (i.e. 4D sequences) are not supported (however can be split upfront into the individual temporal volumes using fslsplit1).

  • T1 inputs must be a T1-weighted sequence before contrast-agent administration (T1-w) acquired as 2D with axial orientation (e.g. TSE) or as 3D (e.g. MPRAGE)
  • cT1 inputs must be a T1-weighted sequence after contrast-agent administration (cT1-w) acquired as 2D with axial orientation (e.g. TSE) or as 3D (e.g. MPRAGE)
  • FLAIR inputs must be a fluid attenuated inversion recovery (FLAIR) sequence acquired as 2D with axial orientation (e.g. TSE). A 3D acquisition (e.g. 3D TSE/FSE) may work as well.
  • T1sub must be a substraction image between cT1 and T1 T1sub = (cT1 - T1) inputs after coregistration

(These specifications are in line with the consensus recommendations for a standardized brain tumor imaging protocol in clinical trials - see Ellingson et al. Neuro Oncol. 2015 Sep;17(9):1188-98 - www.ncbi.nlm.nih.gov/pubmed/26250565)

Input files must contain 3D images; Sequences with multiple temporal volumes (i.e. 4D sequences) are not supported (however can be split upfront into the individual temporal volumes using fslsplit1).

All input files must match the orientation of standard MNI152 template and must be brain extracted and co-registered. All non-brain voxels must be 0. To ensure that these pre-processing steps are performed correctly you may adhere to the following example:

Reorient MRI sequences to standard space

fslreorient2std T1.nii.gz T1_reorient.nii.gz
fslreorient2std CT1.nii.gz CT1_reorient.nii.gz
fslreorient2std FLAIR.nii.gz FLAIR_reorient.nii.gz

The following is the recommended workflow for FSL5. There is a better way to do this but this requires FSL6 (see below)

# perform brain extraction using HD-BET (https://github.com/MIC-DKFZ/HD-BET)
hd-bet -i T1_reorient.nii.gz
hd-bet -i CT1_reorient.nii.gz
hd-bet -i FLAIR_reorient.nii.gz

# register all sequences to T1
fsl5.0-flirt -in CT1_reorient_bet.nii.gz -ref T1_reorient_bet.nii.gz -out CT1_reorient_bet_reg.nii.gz -dof 6 -interp spline
fsl5.0-flirt -in T2_reorient_bet.nii.gz -ref T1_reorient.nii.gz -out T2_reorient_bet_reg.nii.gz -dof 6 -interp spline
fsl5.0-flirt -in FLAIR_reorient_bet.nii.gz -ref T1_reorient.nii.gz -out FLAIR_reorient_bet_reg.nii.gz -dof 6 -interp spline

# Create T1sub by subtracting CT1 - T1

# reapply T1 brain mask (this is important because HD-BM expects non-brain voxels to be 0 and the registration process can introduce nonzero values
# T1_BRAIN_MASK.nii.gz is the mask (not the brain extracted image!) as obtained from HD-Bet
fsl5.0-fslmaths CT1_reorient_bet_reg.nii.gz -mas T1_BRAIN_MASK.nii.gz CT1_reorient_bet_reg.nii.gz
fsl5.0-fslmaths T2_reorient_bet_reg.nii.gz -mas T1_BRAIN_MASK.nii.gz T2_reorient_bet_reg.nii.gz
fsl5.0-fslmaths FLAIR_reorient_bet_reg.nii.gz -mas T1_BRAIN_MASK.nii.gz FLAIR_reorient_bet_reg.nii.gz
fsl5.0-fslmaths T1sub_reorient_bet_reg.nii.gz -mas T1_BRAIN_MASK.nii.gz T1sub_reorient_bet_reg.nii.gz

This is how to do it with FSL6:

# run hd bet
hd-bet -i T1_reorient.nii.gz -o t1_bet.nii.gz -s 1
hd-bet -i CT1_reorient.nii.gz -o ct1_bet.nii.gz
hd-bet -i FLAIR_reorient.nii.gz -o flair_bet.nii.gz

# register brain extracted images to t1, save matrix
flirt -in ct1_bet.nii.gz -out ct1_bet_reg.nii.gz -ref t1_bet.nii.gz -omat ct1_to_t1.mat -interp spline -dof 6 &
flirt -in flair_bet.nii.gz -out flair_bet_reg.nii.gz -ref t1_bet.nii.gz -omat flair_to_t1.mat -interp spline -dof 6 &
wait

# Create the T1Sub by subtracting CT1 - T1

# we are only interested in the matrices, delete the other output images
rm ct1_bet.nii.gz flair_bet.nii.gz
rm ct1_bet_reg.nii.gz flair_bet_reg.nii.gz

# now apply the transformation matrices to the original images (pre hd-bet)
flirt -in CT1_reorient.nii.gz -out ct1_reg.nii.gz -ref t1_bet.nii.gz -applyxfm -init ct1_to_t1.mat -interp spline &
flirt -in FLAIR_reorient.nii.gz -out flair_reg.nii.gz -ref t1_bet.nii.gz -applyxfm -init flair_to_t1.mat -interp spline &
wait

# now apply t1 brain mask to all registered images
fslmaths ct1_reg.nii.gz -mas t1_bet_mask.nii.gz CT1_reorient_reg_bet.nii.gz & # t1_bet_mask.nii.gz was generated by hd-bet (see above)
fslmaths flair_reg.nii.gz -mas t1_bet_mask.nii.gz FLAIR_reorient_reg_bet.nii.gz & # t1_bet_mask.nii.gz was generated by hd-bet (see above)
fslmaths T1sub_reg.nii.gz -mas t1_bet_mask.nii.gz T1sub_reorient_reg_bet.nii.gz & # t1_bet_mask.nii.gz was generated by hd-bet (see above)
wait

# done

After applying this example you would use T1_reorient.nii.gz, CT1_reorient_reg_bet.nii.gz, FLAIR_reorient_reg_bet.nii.gz and `T1sub_reori to proceed.

Run HD-BM

HD-BM provides four main scripts: hd_bm_predict and hd_bm_predict_folder for with T1, T1ce, FLAIR and T1sub modalities are provided.

For cases that do not have T1 and T1sub modalities hd_bm_slim_predict and hd_bm_slim_predict_folder can be used instead.

Predicting cases

The models take multiple modalities as input, which have to be formatted in a nnUNet style fashion (i.e. {ARBITRARY_IMAGE_ID}_{MODALITY_ID}.nii.gz). See original nnUNet docu for more information about that.

For cases that are imaged with T1, cT1, FLAIR and T1sub the MODALITY_ID has to be: {T1: 0000, cT1: 0001, FLAIR: 0002, T1sub: 0003}, for the corresponding modality. For cases imaged without T1 and T1sub the MODALITY_ID has to be: {T1ce: 0000, FLAIR: 0001}, for the corresponding modalities. The user has to confirm this when running or skip the check by using the --skip_modality_check 1 option.

Predicting a single case

hd_bm_predict and hd_bm_slim_predict can be used to predict a single case. This can be useful for exploration or if the number of cases to be processed is low. Exemplary use of it:

hd_bm_predict -i INPUT_DIR -id ARBITRARY_IMAGE_ID -o OUTPUT_DIR -oid OUTPUT_ID

INPUT_DIR is the path to the directory that contains the images of the patient. ARBITRARY_IMAGE_ID can be either only the identifier or any image with/without the MODALITY_ID. OUTPUT_DIR is the path to the output directory to save the image to, if it does not exist all directories that are missing will be created. OUTPUT_ID is an optional name for the output, if not given the input image ID will be used instead.

For further information use the help option that comes with each command, providing a detailed explanation. (e.g. hd_bm_predict --help).

Predicting multiple cases

hd_bm_predict_folder / hd_bm_predict_slim_predict_folder is useful for batch processing, especially if the number of cases to be processed is large. By interleaving preprocessing, inference and segmentation export we can speed up the prediction significantly. Furthermore, the pipeline is initialized only once for all cases, again saving a lot of computation and I/O time. Here is how to use it:

hd_bm_predict_folder -i INPUT_DIR -o OUTPUT_DIR

The INPUT_DIR must contain nifti images (.nii.gz). The results will be written to the OUTPUT_DIR (with the same file names). If the output folder does not exist it will be created.

hd-bm's People

Contributors

tawald avatar

Stargazers

Laurenz Heidrich avatar  avatar Iris avatar giemmecci avatar Nicolas Cordier avatar Francisco Maria Calisto avatar frxderic avatar

Watchers

James Cloos avatar Fabian Isensee avatar Hagen Meredig avatar Kostas Georgiou avatar

hd-bm's Issues

nnunetv2 causing build problems

pip3 install HD-BM-main/
Processing ./HD-BM-main
Preparing metadata (setup.py) ... done
Collecting nnunet@ git+https://github.com/MIC-DKFZ/nnUNet
Cloning https://github.com/MIC-DKFZ/nnUNet to /tmp/pip-install-4tjat3b5/nnunet_fe54d19f47a548a5993363afa47bc565
Running command git clone --filter=blob:none --quiet https://github.com/MIC-DKFZ/nnUNet /tmp/pip-install-4tjat3b5/nnunet_fe54d19f47a548a5993363afa47bc565
Resolved https://github.com/MIC-DKFZ/nnUNet to commit e5fecd749325d7409c540e415fba972a9b222d9e
Preparing metadata (setup.py) ... done
WARNING: Generating metadata for package nnunet produced metadata for project name nnunetv2. Fix your #egg=nnunet fragments.
Discarding git+https://github.com/MIC-DKFZ/nnUNet: Requested nnunetv2 from git+https://github.com/MIC-DKFZ/nnUNet (from hd-bm==1.0) has inconsistent name: expected 'nnunet', but metadata has 'nnunetv2'
Requirement already satisfied: torch in ./miniconda3/lib/python3.10/site-packages (from hd-bm==1.0) (1.13.1+cu117)
ERROR: Could not find a version that satisfies the requirement nnunet (unavailable) (from hd-bm) (from versions: 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 1.0, 1.1, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.1.4.1, 1.1.4.3, 1.5, 1.6.2, 1.6.3, 1.6.4, 1.6.5, 1.6.6, 1.7.0, 1.7.1)
ERROR: No matching distribution found for nnunet (unavailable)

Pickle problem

Hello,

I get the following error when trying to run either hd_bm_predict or hd_bm_slim_predict on Mac OS Ventura:

starting prediction...
Traceback (most recent call last):
File "/opt/fsl/bin/hd_bm_slim_predict", line 8, in
sys.exit(main())
File "/opt/fsl/lib/python3.10/site-packages/hd_bm/hd_bm_slim_predict.py", line 93, in main
predict_cases(
File "/opt/fsl/lib/python3.10/site-packages/nnunet-1.7.0-py3.10.egg/nnunet/inference/predict.py", line 206, in predict_cases
for preprocessed in preprocessing:
File "/opt/fsl/lib/python3.10/site-packages/nnunet-1.7.0-py3.10.egg/nnunet/inference/predict.py", line 109, in preprocess_multithreaded
pr.start()
File "/opt/fsl/lib/python3.10/multiprocessing/process.py", line 121, in start
self._popen = self._Popen(self)
File "/opt/fsl/lib/python3.10/multiprocessing/context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "/opt/fsl/lib/python3.10/multiprocessing/context.py", line 288, in _Popen
return Popen(process_obj)
File "/opt/fsl/lib/python3.10/multiprocessing/popen_spawn_posix.py", line 32, in init
super().init(process_obj)
File "/opt/fsl/lib/python3.10/multiprocessing/popen_fork.py", line 19, in init
self._launch(process_obj)
File "/opt/fsl/lib/python3.10/multiprocessing/popen_spawn_posix.py", line 47, in _launch
reduction.dump(process_obj, fp)
File "/opt/fsl/lib/python3.10/multiprocessing/reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function at 0x2270184c0>: attribute lookup on nnunet.utilities.nd_softmax failed

I'm hoping you might be able to suggest a workaround?

thanks,

Greig

To replicate lesion-wise evaluation

Hi @TaWald,

I would like to check which information was crucial to replicate lesion-wise evaluation which is used for HD-BM paper.

What I have found in the supplement document is the following:

  1. resampling to fulfill 1x1x1 mm, isotropic spacing
  2. dilation to connect close enough.. voxels (radius=3, scikit-image v0.18.1)
  3. Connected component analysis (Probably, I need more information about this step, such as connectivity..?)
  4. Threshold for lesions (predicted lesions above L-Dice > 0.1 only count)
  5. Report median (instead of mean) lesion-wise metrics per patient to balance the effect of the number of lesions in each patient and deal with non-normalities of the values
  6. From the supplement document, does F1-score mean lesion-wise dice? And does dice mean case-wise dice? I was confused.

I have found another repository, which also reports lesion-wise metrics (https://github.com/rachitsaluja/BraTS-2023-Metrics).

However, I recognized the difference between HD-BM one and Brats 2023 in detail. That's how I ask you for more information or clarification.

Suggestions to update HD-BM

Hi Tassilo,

I'm Joey who recently joined Philipp's group as a PhD student.

I started to use HD-BM to evaluate it on other public datasets, had some issues with HD-BM and came up with the following suggestions:

  1. Add PyTorch reinstallation guide, similar as nnUNet, but with the specific version (?) of PyTorch.

Error: no valid convolution algorithms available in CuDNN
In my case I solved this by running: conda install pytorch==1.10.2 pytorch-cuda=12 -c pytorch -c nvidia

  1. Python version upgrade is required (3.6 -> 3.9 (or newer)). I confronted some package issues (scikit-build, and SimpleITK).
    scikit-build (0.17.6) SimpleITK (2.3.1) could not be installed with Python 3.6. They were necessary to resolve the following issue:

MIC-DKFZ/HD-BET#25

  1. Maybe add description about the predicted labels in ReadME file (Label 1: NEE, Label 2: CE)

  2. Probably include evaluate_folder function as well, if the ground truth files are available. What I did was 1) convert plans.pkl -> plans.json file 2) use nnUNetV2 evaludate_folder function. Do you think.. dataset and plan json files are still necessary if user only has prediction and ground truth segmentation files?

I will be happy to make PR if that sounds better for you.

Cannot run postprocessing because the postprocessing file is missing

There is a warning at the end of the run. Not sure if:

  • it is something which matters,
  • or info is an artifact from nnUNet and wrongly displayed.
WARNING! Cannot run postprocessing because the postprocessing file is missing.
Make sure to run consolidate_folds in the output folder of the model first!
The folder you need to run this in is /root/NeuroAI-HD/HD-BM/slim

More details below:

%cd /content
!git clone https://github.com/NeuroAI-HD/HD-BM.git
%cd /content/HD-BM
%pip install .
!hd_bm_slim_predict \
 -i /content/mri_data \
 -id case \
 -o output

Output:

Expecting the 'input modalities' to be in image with 'index':
T1ce    	0000
FLAIR   	0001
Found modalities:
0000
0001
bet
mask
Warning: Found more than the specified modalities!
Do the input images contain the correct modalities? '(y/n)'y
emptying cuda cache
loading parameters for folds, (0, 1, 2, 3, 4)
[...]
using precomputed Gaussian
prediction done
inference done. Now waiting for the segmentation export to finish...
force_separate_z: None interpolation order: 1
separate z: False lowres axis None
no separate z, order 1
WARNING! Cannot run postprocessing because the postprocessing file is missing. Make sure to run consolidate_folds in the output folder of the model first!
The folder you need to run this in is /root/NeuroAI-HD/HD-BM/slim

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.