Diff of /docs/README.md [000000] .. [e918fa]

Switch to unified view

a b/docs/README.md
1
![](pnl-bwh-hms.png)
2
3
[![DOI](https://zenodo.org/badge/doi/10.5281/zenodo.3665739.svg)](https://doi.org/10.5281/zenodo.3665739) [![Python](https://img.shields.io/badge/Python-3.6-green.svg)]() [![Platform](https://img.shields.io/badge/Platform-linux--64%20%7C%20osx--64-orange.svg)]()
4
5
*CNN-Diffusion-MRIBrain-Segmentation* repository is developed by Senthil Palanivelu, Suheyla Cetin Karayumak, Tashrif Billah, Ryan Zurrin, Sylvain Bouix, and Yogesh Rathi, 
6
Brigham and Women's Hospital (Harvard Medical School).
7
8
Table of Contents
9
=================
10
11
* [Table of Contents](#table-of-contents)
12
* [Segmenting diffusion MRI brain](#segmenting-diffusion-mri-brain)
13
* [Citation](#citation)
14
* [Dependencies](#dependencies)
15
* [Installation](#installation)
16
   * [1. Python 3](#1-python-3)
17
   * [2. Conda environment](#2-conda-environment)
18
   * [3. ANTs](#3-ants)
19
   * [4. CUDA environment](#4-cuda-environment)
20
   * [5. Download models](#5-download-models)
21
   * [6. Run script](#6-run-script)
22
   * [7. mrtrix](#7-mrtrix)
23
   * [8. FSL](#8-fsl)
24
* [Singularity container](#singularity-container)
25
* [Running the pipeline](#running-the-pipeline)
26
   * [Prediction](#prediction)
27
   * [Training](#training)
28
      * [1. B0 extraction](#1-b0-extraction)
29
      * [2. Registration](#2-registration)
30
      * [3. Preprocessing](#3-preprocessing)
31
      * [4. Deep learning](#4-deep-learning)
32
* [Method](#method)
33
   * [1. Model Architecture](#1-model-architecture)
34
   * [2. Multi View Aggregation:](#2-multi-view-aggregation)
35
   * [3. Clean up](#3-clean-up)
36
* [Troubleshooting](#troubleshooting)
37
* [Issues](#issues)
38
* [Reference](#reference)
39
40
<!-- Created by https://github.com/ekalinin/github-markdown-toc -->
41
42
43
# Segmenting diffusion MRI brain
44
45
A challenging problem in neuroscience is to create brain mask of an MRI. The problem becomes more challenging when 
46
the MRI is a diffusion MRI (dMRI) since it has less contrast between brain and non-brain regions. Researchers all over the 
47
world have to spent many hours drawing dMRI brain masks manually. In order to automate that process, we come up with a 
48
convolutional neural network (CNN) based approach for creating dMRI brain mask automatically. The CNN architecture is 
49
obtained from Raunak Dey's [work](https://github.com/raun1/MICCAI2018---Complementary_Segmentation_Network-Raw-Code). 
50
On top of that, we applied a multi-view aggregation step to obtain final brain mask.
51
52
The software is capable of both training and predicton. We also provide trained models so it can be used off the shelf.
53
Psychiatry NeuroImaging Laboratory has a large number of dMRI brain masks drawn by research assistants. 
54
The models were trained on 1,500 such brain masks. We acchieved an accuracy of 97% in properly segmenting the brain 
55
from a dMRI b0 volume.
56
57
58
59
# Citation
60
61
If you use this software, please cite all of the following:
62
63
* Palanivelu, S., Cetin Karayumak, S., Billah, T., Zurrin, R., Bouix, S., Rathi, Y.; CNN based diffusion MRI brain segmentation tool, 
64
https://github.com/pnlbwh/CNN-Diffusion-MRIBrain-Segmentation, 2020, DOI: 10.5281/zenodo.3665739
65
66
* Cetin-Karayumak, S., Zhang, F., Zurrin, R. et al. Harmonized diffusion MRI data and white matter measures from the
67
Adolescent Brain Cognitive Development Study. Sci Data 11, 249 (2024). https://doi.org/10.1038/s41597-024-03058-w
68
69
70
# Dependencies
71
72
* Python 3
73
* ANTs
74
75
# Installation
76
77
## 1. Python 3
78
79
Download [Miniconda Python 3 bash installer](https://docs.conda.io/en/latest/miniconda.html) (32/64-bit based on your environment):
80
    
81
    sh Miniconda3-latest-Linux-x86_64.sh -b # -b flag is for license agreement
82
83
Activate the conda environment:
84
85
    source ~/miniconda3/bin/activate # should introduce '(base)' in front of each line
86
87
The software is written intelligently to work with or without GPU support.
88
89
<details><summary>Old conda environment</summary>
90
91
<br>
92
  
93
We had provided two [*yml* files](../archive/) to facilitate the creation of `dmri_seg` conda environment.
94
95
* In the initial stage of this program development in 2020, the [CPU environment](../archive/environment_cpu.yml) built and worked fine.
96
  But it does not work anymore.
97
* The [GPU environment](../archive/environment_gpu.yml) works fine on older GPUs.
98
  But in 2023, it did not produce a valid mask on modern GPUs e.g. NVIDIA RTX 4000, A6000, A100.
99
100
</details>
101
102
In 2024, we recommend the below step-by-step instructions to build an environment that
103
  successfully generated valid masks on both GPU and CPU devices.
104
   
105
## 2. Conda environment
106
107
Step-by-step instructions:
108
109
```
110
conda create -y -n dmri_seg python=3.11 --override-channels
111
conda activate dmri_seg
112
pip install 'tensorflow[and-cuda]==2.15.1'
113
pip install scikit-image gputil git+https://github.com/pnlbwh/conversion.git
114
```
115
116
Tensorflow installation is inspired by https://www.tensorflow.org/install/pip.
117
118
## 3. ANTs
119
120
You need to have a working `antsRegistrationSyNQuick.sh` in your `PATH`.
121
You can build ANTs following their [GitHub](https://github.com/ANTsX/ANTs).
122
Or you can use our pre-built ANTs:
123
124
> conda install -y pnlbwh::ants
125
126
Either way, you need to set the environment variable `ANTSPATH`. For the latter method, it is:
127
128
> export ANTSPATH=${CONDA_PREFIX}/bin
129
130
## 4. CUDA environment
131
132
To run this program on GPU, you must set `LD_LIBRARY_PATH`:
133
134
> export LD_LIBRARY_PATH=${CONDA_PREFIX}/lib/:${LD_LIBRARY_PATH}
135
136
Your NVIDIA driver should be compatible with CUDA.
137
138
<details><summary>System's CUDA</summary>
139
<br>
140
  
141
In some servers, a matched CUDA may be available in `/usr/local/cuda-*/`
142
directory. You can also use that CUDA instead of `${CONDA_PREFIX}/lib/`:
143
144
    # add cuda tools to your PATH
145
    export PATH=/usr/local/cuda-9.1/bin:${PATH}
146
147
    # add cuda libraries to your LD_LIBRARY_PATH
148
    export LD_LIBRARY_PATH=/usr/local/cuda-9.1/lib64:${LD_LIBRARY_PATH}
149
150
</details>
151
152
## 5. Download models
153
154
Download model architecture, weights and IIT mean b0 template from 
155
https://github.com/pnlbwh/CNN-Diffusion-MRIBrain-Segmentation/releases as follows:
156
157
    cd CNN-Diffusion-MRIBrain-Segmentation
158
    wget https://github.com/pnlbwh/CNN-Diffusion-MRIBrain-Segmentation/releases/download/v0.3/model_folder.tar.gz
159
    tar -xzvf model_folder.tar.gz
160
    cd model_folder
161
    wget https://www.nitrc.org/frs/download.php/11290/IITmean_b0_256.nii.gz
162
163
164
They will be extracted to `CNN-Diffusion-MRIBrain-Segmentation/model_folder` directory.
165
166
167
## 6. Run script
168
169
To avoid the need for setting `LD_LIBRARY_PATH` and providing Python interpreter every time `dwi_masking.py` is run,
170
we wrote a run script `dwi_masking.sh`. If you would like, you can configure it for your environment as:
171
172
```
173
cd CNN-Diffusion-MRIBrain-Segmentation/pipeline
174
sed -i "s+PREFIX+${CONDA_PREFIX}+g" dwi_masking.sh
175
```
176
177
Afterward, you can use `dwi_masking.sh` instead of `dwi_masking.py` in all the examples shown in this manual.
178
179
180
## 7. mrtrix
181
182
This software makes use of only one binary from mrtrix, `maskfilter`. If you already have mrtrix installed on your 
183
system, you can use it. Moreover, you can follow [this](https://mrtrix.readthedocs.io/en/latest/installation/linux_install.html) instruction to install mrtrix.
184
However, if you are unable to use/install mrtrix, just set `-filter scipy` argument and the software will 
185
use a Python translated version of the above binary.
186
187
See [Clean up](#3-clean-up) for details.
188
189
190
## 8. FSL
191
192
This software marginally depends on FSL in the sense that you need FSL's `slicesdir` executable
193
to generate snapshots of image-mask for being able to QC it. Hence, we do not recommend
194
sourcing the entire FSL environment. Instead, you should just put `slicesdir` in your PATH:
195
196
> export PATH=/path/to/fsl-6.0.7/share/fsl/bin:$PATH
197
198
In additon, you should set:
199
200
```
201
export FSLDIR=/path/to/fsl-6.0.7/
202
export FSLOUTPUTTYPE=NIFTI_GZ
203
```
204
205
206
# Singularity container
207
208
The dependencies in [environment_cpu.yml](../environment_cpu.yml) have been found to be volatile. So we have archived 
209
the dependencies of this software in a [Singularity](../Singularity) container. After you have downloaded the models, 
210
you can download and use the it as follows:
211
212
    # download the container
213
    cd CNN-Diffusion-MRIBrain-Segmentation
214
    wget https://www.dropbox.com/s/rpc8ighcxqvzete/dmri_seg.sif
215
    
216
    # verify that it works
217
    singularity run dmri_seg.sif dwi_masking.py --help
218
    
219
    # use it to generate brain masks
220
    singularity run \
221
    --bind /path/to/data:/data \
222
    --bind model_folder:/home/pnlbwh/CNN-Diffusion-MRIBrain-Segmentation/model_folder \
223
    dmri_seg.sif \
224
    dwi_masking.py -i /data/dwi_list.txt -f /home/pnlbwh/CNN-Diffusion-MRIBrain-Segmentation/model_folder
225
226
227
Please continue reading below to learn more about `dwi_list.txt`. Also note that, the container supports only `-filter scipy` option.
228
    
229
230
# Running the pipeline
231
232
233
## Prediction
234
235
Prediction refers to creation of masks based on pre-trained model. This is the common use of this software.
236
    
237
    pipeline/dwi_masking.py -i dwi_list.txt -f model_folder
238
    pipeline/dwi_masking.py -i dwi_list.txt -f model_folder -nproc 16
239
    pipeline/dwi_masking.py -i b0_list.txt -f model_folder
240
    pipeline/dwi_masking.py -i b0_list.txt -f model_folder -qc 1
241
242
243
* `dwi_list.txt` and `b0_list.txt` must contain full paths to diffusion and b0 volumes respectively
244
245
* Each created mask is saved in the directory of input volume with name `dwib0_{PREFIX}-multi_BrainMask.nii.gz`
246
247
248
## Training
249
250
However, you can train a model on your own data using this software. 
251
Data refers to a set of b0 images and their corresponding masks.
252
Training is performed in four steps:
253
254
### 1. B0 extraction
255
256
In the training stage, we train a CNN to predict b0 brain mask with reasonable accuracy. For the CNN to learn masking, 
257
we have to provide a set of b0 images and their corresponding masks. So the first step in training would extracting 
258
b0 image given a DWI. You can use your favorite tool, bvalue threshold, and method (first b0 or average over all b0s) 
259
to obtain b0 image. Then, researchers usually draw masks by hand or edit FSL `bet` produced masks. 
260
Once you have a set of b0s and their corresponding masks, you can start the next steps.
261
262
263
### 2. Registration
264
265
This software uses `IITmean_b0_256.nii.gz` image as the reference that you can download 
266
from https://www.nitrc.org/frs/?group_id=432 . If your b0s and corresponding masks are already in the space of 
267
`IITmean_b0_256.nii.gz` (ICBM-152/MNI152), you do not need to register again.
268
269
Example:
270
271
    src/registration.py -b0 b0_list.txt -mask mask_list.txt -ref model_folder/IITmean_b0_256.nii.gz
272
    
273
274
The registered images are stored in the directory of given images as `{Prefix}-Warped.nii.gz` and 
275
`{Prefix}-Warped-mask.nii.gz`.
276
277
### 3. Preprocessing
278
279
Whether you register the images or not, create a text file for with their absolute paths:
280
281
> b0_list.txt
282
283
    /path/to/case1_b0.nii.gz
284
    /path/to/case2_b0.nii.gz
285
    ...
286
    
287
288
> mask_list.txt
289
290
    /path/to/case1_mask.nii.gz
291
    /path/to/case2_mask.nii.gz
292
    ...
293
    
294
Make sure to account for registered paths if have have done so.
295
296
Then, we normalize b0 image to the range [0,1] with respect to its highest intensity. 
297
Same clipping is performed to the mask. 
298
299
    src/preprocess_b0.py -i b0_list.txt
300
    src/preprocess_mask.py -i mask_list.txt
301
302
Upon preprocessing, b0 data are appended to:
303
    
304
    sagittal-traindata-dwi.npy
305
    coronal-traindata-dwi.npy
306
    axial-traindata-dwi.npy
307
308
Similarly, mask data are appended to files with `-mask.npy` suffix. The above `*.npy` files are saved in 
309
the directory of the first b0 image given in `b0_list.txt`.
310
311
312
### 4. Deep learning
313
314
Now we come to the actual machine learning stage. We want to train our CNN varying `principal_axis` 
315
over the set `{axial,coronal,saggittal}` one at a time. 
316
317
Copy the `src/settings.ini` to a different directory and edit relevant fields:
318
319
```ini
320
[COMMON]
321
save_model_dir = "project/model_dir/"
322
log_dir = "project/log_dir"
323
324
[DATA]
325
data_dir = "project/dir/of/first/case/"
326
train_data_file = "sagittal-traindata-dwi.npy"
327
train_label_file = "sagittal-traindata-mask.npy"
328
329
[TRAINING]
330
principal_axis = "sagittal"
331
learning_rate = 1e-3
332
train_batch_size = 4
333
validation_split = 0.2
334
num_epochs = 1
335
shuffle_data = "True"
336
```
337
338
At the very least, you should edit `save_model_dir`, `log_dir`, `data_dir`, and `num_epochs` fields. Now define 
339
environment variable `COMPNET_CONFIG` so your own `settings.ini` could be found by `src/train.py` program:
340
341
    export COMPNET_CONFIG=/path/to/your/settings.ini
342
    
343
    
344
Finally, you can use the same `settings.ini` file for training three models corresponding to three 
345
principal axes- `sagittal`, `coronal`, and `axial`. For each of the axes, run:
346
347
    src/train.py  # for sagittal, num_epochs=9
348
    src/train.py  # for coronal, num_epochs=8
349
    src/train.py  # for axial, num_epochs=8
350
351
352
**NOTE** The above `num_epochs` were found suitable for the models distributed with this software.
353
354
`src/train.py` will save each epoch `*-compnet_final_weight-*.h5` models in `save_model_dir` that you defined in `settings.ini`.
355
You can use this `save_model_dir` for [prediction](#prediction). Note that, the final epoch models are used in prediction by default. 
356
If you want to use another epoch models other than the final ones created above, save them in a different directory along with 
357
`CompNetBasicModel.json` and `IITmean_b0_256.nii.gz` and use that directory as `save_model_dir` for prediction. As you probably 
358
realized already, the three models pertinent to three views would not have to be of the same epoch number.
359
360
361
# Method
362
363
## 1. Model Architecture
364
365
The code is written by Raunak Dey available at 
366
367
https://github.com/raun1/MICCAI2018---Complementary_Segmentation_Network-Raw-Code. 
368
369
In summary, his proposed architecture is designed in the framework of encoder-decoder networks and have three pathways.
370
371
* Segmentation Branch - learns what is the brain tissue and to generate a brain mask 
372
373
* Complementary Branch - learns what is outside of the brain and to help the other
374
branch generate brain mask
375
376
* Reconstruction Branch - It provides direct feedback to the segmentation and
377
complementary branche and expects reasonable predictions from them as input to reconstruct the original input image.
378
379
![](CompNet.png)
380
**Illustration kindly provided by Raunak Dey**
381
382
383
## 2. Multi View Aggregation:
384
385
The approach is to train three separate networks for three principal axes ( Sagittal, Coronal and axial ) and 
386
to perform multi-view aggregation step that combines segmentations from models trained on 2D slices along three principal axes: 
387
coronal, sagittal and axial. The final segmentation is obtained by combining the probability maps from all three segmentation.
388
389
![](Multiview.png)
390
391
392
See [Reference #2](#reference) for details of this method.
393
394
395
## 3. Clean up
396
397
The aggregated mask can be cleaned up using a [maskfilter from mrtrix](https://mrtrix.readthedocs.io/en/latest/reference/commands/maskfilter.html) by `-filter mrtrix` argument. 
398
In brief, there remain islands of non brain region in the aggregated mask. The above filter applies a series of morphological 
399
operation i.e. erosion and dilation to clean up the mask. If mrtrix installation is a problem for you, 
400
you can use [Python translated version](../src/maskfilter.py) of maskfilter provided with this software by `-filter scipy` argument. If `-filter` is not used, then the aggregated mask is returned.
401
402
403
# Troubleshooting
404
405
1. If you get any error while building `dmri_seg` conda environment, updating conda first and retrying should be helpful:
406
407
```    
408
conda update -y -n base -c defaults conda
409
```
410
411
See https://github.com/conda/conda/issues/8661 for details.
412
413
# Issues
414
415
If you experience any issue with using this software, please open an issue [here](https://github.com/pnlbwh/CNN-Diffusion-MRIBrain-Segmentation/issues).
416
We shall get back to you as soon as we can.
417
418
419
# Reference
420
421
1. Dey, Raunak and Hong, Yi. "CompNet: Complementary segmentation network for brain MRI extraction." 
422
International Conference on Medical Image Computing and Computer-Assisted Intervention. Springer, Cham, 2018.
423
424
2. Roy, Abhijit Guha, et al. "QuickNAT: A fully convolutional network for quick and accurate segmentation of neuroanatomy." 
425
NeuroImage 186 (2019): 713-727.
426
427
3. Billah, Tashrif; Bouix, Sylvain; Rathi, Yogesh; Various MRI Conversion Tools, https://github.com/pnlbwh/conversion, 2019, 
428
DOI: 10.5281/zenodo.2584003
429
430
4. Zhang S, Arfanakis K. "Evaluation of standardized and study-specific diffusion tensor imaging templates of 
431
the adult human brain: Template characteristics, spatial normalization accuracy, and detection of small 
432
inter-group FA differences." Neuroimage 2018;172:40-50.
433