This repository contains the code for Brain Segmenter, the main product of the company Medvision.
Brain Segmenter is a pipeline to segment Brain MRI scans in four different different regions:
- White Matter (WM)
- Gray Matter (GM)
- Cerebro-Spinal Fluid (CSF)
- Background + Bone + Extra-Skull-Soft-Tissue
The pipeline general strructure is the following:
- Pre-Processing:
- Registering
- Intensity Normalization
- Voxel Spacing Normalization
- Skull Stripping
- Segmentation:
- Reorganizing data
- Expectation Maximization voxel-classification
- Post-Processing:
- Brain reconstruction
- Label matching
- Image storing
Checkploint file | Model | CSF | WM | GM |
---|---|---|---|---|
checkpoint_def.pkl | EM - init:TM - atlas:mni/after | 0.463 | 0.805 | 0.895 |
The presented pipeline can be fully reproduced locally. Below we provide BASH commands, which can be run in an Unix/Unix-like OS (Mac OS, GNU-Linux) and CMD comands for windows user (¬¬ consider changing to GNU-Linux, your life will be better).
-
Create the environment
Unix:
conda create -n medvision python==3.9.13 anaconda -y && conda activate medvision
Windows:
conda create -n medvision python==3.9.13 anaconda -y && conda activate medvision
-
Install requirements
Both OS:
pip install -r requirements.txt
-
Add current repository path to PYTHONPATH
Unix:
export PYTHONPATH="${PYTHONPATH}:/path/to/your/project/"
Windows:
set PYTHONPATH=%PYTHONPATH%;/path/to/your/project/
To reproduce our results over one example run the following commands:
-
3.2.1 Download the example image
Unix:
cd data/ && gdown https://drive.google.com/uc?id=1MlypAmSqJIcYi7HRewPFeutDUL3nx2Fx && unzip example_image.zip && rm example_image.zip && cd ../
Windows:
cd data/ && gdown https://drive.google.com/uc?id=1MlypAmSqJIcYi7HRewPFeutDUL3nx2Fx && tar -xf example_image.zip && del example_image.zip && cd ..
Alternative:
If you can't or don't want to download from command line. Here is the drive link
The directories should be collaborative_coding/data/example_img/[content of example_img zip]
-
3.2.2 Download the model checkpoints
Unix:
cd models/ && gdown https://drive.google.com/uc?id=1bNROHoZSQgwaqF-w0NwWyFHyv3H1tXWx && unzip checkpoints.zip && rm checkpoints.zip && cd ../
Windows:
cd models\ && gdown https://drive.google.com/uc?id=1bNROHoZSQgwaqF-w0NwWyFHyv3H1tXWx && tar -xf checkpoints.zip && del checkpoints.zip && cd ..
Alternative:
If you can't or don't want to download from command line. Here is the drive link
The directories should be collaborative_coding/models/checkpoints/[content of checkpoints zip]
-
3.2.3 Run the pipeline
python brain_segmenter.py --ip [PATH] --chkpt [PATH] --op [PATH]
Example:
python brain_segmenter.py --ip /home/jseia/Desktop/MAIA/classes/spain/ehealth/lab/collaborative_coding/data/example_image/1003/1003.nii.gz --chkpt /home/jseia/Desktop/MAIA/classes/spain/ehealth/lab/collaborative_coding/models/checkpoints/checkpoint_def.pkl --op /home/jseia/Desktop/MAIA/classes/spain/ehealth/lab/collaborative_coding/data/example_image/1003
-
3.3.1 Download the database
Unix:
cd data/ && gdown https://drive.google.com/uc?id=1-o0pSnkKytqoqaqsW472Ze1wj4ccjD3y && unzip data.zip && rm data.zip && cd ../
Windows:
cd data/ && gdown https://drive.google.com/uc?id=1-o0pSnkKytqoqaqsW472Ze1wj4ccjD3y && tar -xf data.zip && del data.zip && cd ..
Alternative:
If you don't want to download from command line. Here is the drive link
The directories should be collaborative_coding/data/[content of data zip]
-
3.3.2 Run a experiment
-
Modify experiments/train_config.yaml accordingly.
Copy the .example file, remove the .example from the name and modify the file
-
Run experiments/train.py
python experiments/train.py
-
-
3.3.3 Test on the complete test dataset
-
Modify experiments/test_config.yaml accordingly.
Copy the .example file, remove the .example from the name and modify the file
-
Run experiments/test.py
python experiments/test.py
-
-
3.3.4 Get quick analysis of available experiments
-
Run analysis/analysis.py
python analysis/analysis.py --rf [PATH]/experiments/test_results --op [PATH]/data/comp_imgs --exp [LIST OF EXPERIMENTS] --cases [LIST OF 5 CASES]
Example:
python analysis/analysis.py --rf /home/jseia/Desktop/MAIA/classes/spain/ehealth/lab/collaborative_coding/experiments/test_results --op /home/jseia/Desktop/MAIA/classes/spain/ehealth/lab/collaborative_coding/data/comp_imgs --exp try_01 try_02 --cases 1025 1024 1003
-
- The code in Medvision is developed following:
- numpy docstring format
- flake8 linter
- characters per line: 100