a b/README.md
1
# SynthSeg
2
3
4
In this repository, we present SynthSeg, the first deep learning tool for segmentation of brain scans of
5
any contrast and resolution. SynthSeg works out-of-the-box without any retraining, and is robust to:
6
- any contrast
7
- any resolution up to 10mm slice spacing
8
- a wide array of populations: from young and healthy to ageing and diseased
9
- scans with or without preprocessing: bias field correction, skull stripping, normalisation, etc.
10
- white matter lesions.
11
\
12
\
13
![Generation examples](data/README_figures/segmentations.png)
14
15
16
\
17
SynthSeg was first presented for the automated segmentation of brain scans of any contrast and resolution.
18
19
**SynthSeg: Segmentation of brain MRI scans of any contrast and resolution without retraining** \
20
B. Billot, D.N. Greve, O. Puonti, A. Thielscher, K. Van Leemput, B. Fischl, A.V. Dalca, J.E. Iglesias \
21
Medical Image Analysis (2023) \
22
[ [article](https://www.sciencedirect.com/science/article/pii/S1361841523000506) | [arxiv](https://arxiv.org/abs/2107.09559) | [bibtex](bibtex.bib) ]
23
\
24
\
25
Then, we extended it to work on heterogeneous clinical scans, and to perform cortical parcellation and automated 
26
quality control.
27
28
**Robust machine learning segmentation for large-scale analysis of heterogeneous clinical brain MRI datasets** \
29
B. Billot, M. Colin, Y. Cheng, S.E. Arnold, S. Das, J.E. Iglesias \
30
PNAS (2023) \
31
[ [article](https://www.pnas.org/doi/full/10.1073/pnas.2216399120#bibliography) | [arxiv](https://arxiv.org/abs/2203.01969) | [bibtex](bibtex.bib) ]
32
33
\
34
Here, we distribute our model to enable users to run SynthSeg on their own data. We emphasise that 
35
predictions are always given at 1mm isotropic resolution (regardless of the input resolution). The code can be run on
36
the GPU (~15s per scan) or on the CPU (~1min).
37
38
39
----------------
40
41
### New features and updates
42
43
\
44
01/03/2023: **The papers for SynthSeg and SynthSeg 2.0 are out! :open_book: :open_book:** \
45
After a long review process for SynthSeg (Medical Image Analysis), and a much faster one for SynthSeg 2.0 (PNAS), both
46
papers have been accepted nearly at the same time ! See the references above, or in the citation section.
47
48
\
49
04/10/2022: **SynthSeg is available with Matlab!** :star: \
50
We are delighted that Matlab 2022b (and onwards) now includes SynthSeg in its Medical Image
51
Toolbox. They have a [documented example](https://www.mathworks.com/help/medical-imaging/ug/Brain-MRI-Segmentation-Using-Trained-3-D-U-Net.html)
52
on how to use it. But, to simplify things, we wrote our own Matlab wrapper, which you can call in one single line. 
53
Just download [this zip file](https://liveuclac-my.sharepoint.com/:u:/g/personal/rmappmb_ucl_ac_uk/EctEe3hOP8dDh1hYHlFS_rUBo80yFg7MQY5WnagHlWcS6A?e=e8bK0f),
54
uncompress it, open Matlab, and type `help SynthSeg` for instructions.
55
56
\
57
29/06/2022: **SynthSeg 2.0 is out !** :v: \
58
In addition to whole-brain segmentation, it now also performs **Cortical parcellation, automated QC, and intracranial 
59
volume (ICV) estimation** (see figure below). Also, most of these features are compatible with SynthSeg 1.0. (see table).
60
\
61
\
62
![new features](data/README_figures/new_features.png)
63
64
![table versions](data/README_figures/table_versions.png)
65
66
\
67
01/03/2022: **Robust version** :hammer: \
68
SynthSeg sometimes falters on scans with low signal-to-noise ratio, or with very low tissue contrast. For this reason, 
69
we developed a new model for increased robustness, named "SynthSeg-robust". You can use this mode when SynthSeg gives 
70
results like in the figure below:
71
\
72
\
73
![Robust](data/README_figures/robust.png)
74
75
\
76
29/10/2021: **SynthSeg is now available on the dev version of
77
[FreeSurfer](https://surfer.nmr.mgh.harvard.edu/fswiki/DownloadAndInstall) !!** :tada: \
78
See [here](https://surfer.nmr.mgh.harvard.edu/fswiki/SynthSeg) on how to use it.
79
80
----------------
81
82
### Try it in one command !
83
84
Once all the python packages are installed (see below), you can simply test SynthSeg on your own data with:
85
```
86
python ./scripts/commands/SynthSeg_predict.py --i <input> --o <output> [--parc --robust --ct --vol <vol> --qc <qc> --post <post> --resample <resample>]
87
```
88
89
90
where:
91
- `<input>` path to a scan to segment, or to a folder. This can also be the path to a text file, where each line is the
92
path of an image to segment.
93
- `<output>` path where the output segmentations will be saved. This must be the same type as `<input>` (i.e., the path 
94
to a file, a folder, or a text file where each line is the path to an output segmentation).
95
- `--parc` (optional) to perform cortical parcellation in addition to whole-brain segmentation.
96
- `--robust` (optional) to use the variant for increased robustness (e.g., when analysing clinical data with large space
97
spacing). This can be slower than the other model.
98
- `--ct` (optional) use on CT scans in Hounsfield scale. It clips intensities to [0, 80].
99
- `<vol>` (optional) path to a CSV file where the volumes (in mm<sup>3</sup>) of all segmented regions will be saved for all scans 
100
(e.g. /path/to/volumes.csv). If `<input>` is a text file, so must be `<vol>`, for which each line is the path to a 
101
different CSV file corresponding to one subject only.
102
- `<qc>` (optional) path to a CSV file where QC scores will be saved. The same formatting requirements as `<vol>` apply.
103
- `<post>` (optional) path where the posteriors, given as soft probability maps, will be saved (same formatting 
104
requirements as for `<output>`).
105
- `<resample>` (optional) SynthSeg segmentations are always given at 1mm isotropic resolution. Hence, 
106
images are always resampled internally to this resolution (except if they are already at 1mm resolution). 
107
Use this flag to save the resampled images (same formatting requirements as for `<output>`).
108
109
Additional optional flags are also available:
110
- `--cpu`: (optional) to enforce the code to run on the CPU, even if a GPU is available.
111
- `--threads`: (optional) number of threads to be used by Tensorflow (default uses one core). Increase it to decrease 
112
the runtime when using the CPU version.
113
- `--crop`: (optional) to crop the input images to a given shape before segmentation. This must be divisible by 32.
114
Images are cropped around their centre, and their segmentations are given at the original size. It can be given as a 
115
single (i.e., `--crop 160`), or several integers (i.e, `--crop 160 128 192`, ordered in RAS coordinates). By default the
116
whole image is processed. Use this flag for faster analysis or to fit in your GPU.
117
- `--fast`: (optional) to disable some operations for faster prediction (twice as fast, but slightly less accurate). 
118
This doesn't apply when the --robust flag is used.
119
- `--v1`: (optional) to run the first version of SynthSeg (SynthSeg 1.0, updated 29/06/2022).
120
121
122
**IMPORTANT:** SynthSeg always give results at 1mm isotropic resolution, regardless of the input. However, this can 
123
cause some viewers to not correctly overlay segmentations on their corresponding images. In this case, you can use the
124
`--resample` flag to obtain a resampled image that lives in the same space as the segmentation, such that they can be 
125
visualised together with any viewer.
126
127
The complete list of segmented structures is available in [labels table.txt](data/labels%20table.txt) along with their
128
corresponding values. This table also details the order in which the posteriors maps are sorted.
129
130
131
----------------
132
133
### Installation
134
135
1. Clone this repository.
136
137
2. Create a virtual environment (i.e., with pip or conda) and install all the required packages. \
138
These depend on your python version, and here we list the requirements for Python 3.6 
139
([requirements_3.6](requirements_python3.6.txt)) and Python 3.8 (see [requirements_3.8](requirements_python3.8.txt)).
140
The choice is yours, but in each case, please stick to the exact package versions.\
141
A first solution to install the dependencies, if you use pip, is to run setup.py (with and activated virtual 
142
environment): `python setup.py install`. Otherwise, we also give here the minimal commands to install the required 
143
packages using pip/conda for Python 3.6/3.8.
144
145
```
146
# Conda, Python 3.6:
147
conda create -n synthseg_36 python=3.6 tensorflow-gpu=2.0.0 keras=2.3.1 h5py==2.10.0 nibabel matplotlib -c anaconda -c conda-forge
148
149
# Conda, Python 3.8:
150
conda create -n synthseg_38 python=3.8 tensorflow-gpu=2.2.0 keras=2.3.1 nibabel matplotlib -c anaconda -c conda-forge
151
152
# Pip, Python 3.6:
153
pip install tensorflow-gpu==2.0.0 keras==2.3.1 nibabel==3.2.2 matplotlib==3.3.4
154
155
# Pip, Python 3.8:
156
pip install tensorflow-gpu==2.2.0 keras==2.3.1 protobuf==3.20.3 numpy==1.23.5 nibabel==5.0.1 matplotlib==3.6.2
157
```
158
159
3. Go to this link [UCL dropbox](https://liveuclac-my.sharepoint.com/:f:/g/personal/rmappmb_ucl_ac_uk/EtlNnulBSUtAvOP6S99KcAIBYzze7jTPsmFk2_iHqKDjEw?e=rBP0RO), and download the missing models. Then simply copy them to [models](models).
160
161
4. If you wish to run on the GPU, you will also need to install Cuda (10.0 for Python 3.6, 10.1 for Python 3.8), and 
162
CUDNN (7.6.5 for both). Note that if you used conda, these were already automatically installed.
163
164
That's it ! You're now ready to use SynthSeg ! :tada:
165
166
167
----------------
168
169
### How does it work ?
170
171
In short, we train a network with synthetic images sampled on the fly from a generative model based on the forward
172
model of Bayesian segmentation. Crucially, we adopt a domain randomisation strategy where we fully randomise the 
173
generation parameters which are drawn at each minibatch from uninformative uniform priors. By exposing the network to 
174
extremely variable input data, we force it to learn domain-agnostic features. As a result, SynthSeg is able to readily 
175
segment real scans of any target domain, without retraining or fine-tuning. 
176
177
The following figure first illustrates the workflow of a training iteration, and then provides an overview of the 
178
different steps of the generative model:
179
\
180
\
181
![Overview](data/README_figures/overview.png)
182
\
183
\
184
Finally we show additional examples of the synthesised images along with an overlay of their target segmentations:
185
\
186
\
187
![Training data](data/README_figures/training_data.png)
188
\
189
\
190
If you are interested to learn more about SynthSeg, you can read the associated publication (see below), and watch this
191
presentation, which was given at MIDL 2020 for a related article on a preliminary version of SynthSeg (robustness to
192
MR contrast but not resolution).
193
\
194
\
195
[![Talk SynthSeg](data/README_figures/youtube_link.png)](https://www.youtube.com/watch?v=Bfp3cILSKZg&t=1s)
196
197
198
----------------
199
200
### Train your own model
201
202
This repository contains all the code and data necessary to train, validate, and test your own network. Importantly, the
203
proposed method only requires a set of anatomical segmentations to be trained (no images), which we include in 
204
[data](data/training_label_maps). While the provided functions are thoroughly documented, we highly recommend to start 
205
with the following tutorials:
206
207
- [1-generation_visualisation](scripts/tutorials/1-generation_visualisation.py): This very simple script shows examples
208
of the synthetic images used to train SynthSeg.
209
210
- [2-generation_explained](scripts/tutorials/2-generation_explained.py): This second script describes all the parameters
211
used to control the generative model. We advise you to thoroughly follow this tutorial, as it is essential to understand
212
how the synthetic data is formed before you start training your own models.
213
214
- [3-training](scripts/tutorials/3-training.py): This scripts re-uses the parameters explained in the previous tutorial
215
and focuses on the learning/architecture parameters. The script here is the very one we used to train SynthSeg !
216
217
- [4-prediction](scripts/tutorials/4-prediction.py): This scripts shows how to make predictions, once the network has 
218
been trained.
219
220
- [5-generation_advanced](scripts/tutorials/5-generation_advanced.py): Here we detail more advanced generation options, 
221
in the case of training a version of SynthSeg that is specific to a given contrast and/or resolution (although these
222
types of variants were shown to be outperformed by the SynthSeg model trained in the 3rd tutorial).
223
224
- [6-intensity_estimation](scripts/tutorials/6-intensity_estimation.py): This script shows how to estimate the 
225
Gaussian priors of the GMM when training a contrast-specific version of SynthSeg.
226
227
- [7-synthseg+](scripts/tutorials/7-synthseg+.py): Finally, we show how the robust version of SynthSeg was 
228
trained.
229
230
These tutorials cover a lot of materials and will enable you to train your own SynthSeg model. Moreover, even more 
231
detailed information is provided in the docstrings of all functions, so don't hesitate to have a look at these !
232
233
234
----------------
235
236
### Content
237
238
- [SynthSeg](SynthSeg): this is the main folder containing the generative model and training function:
239
240
  - [labels_to_image_model.py](SynthSeg/labels_to_image_model.py): contains the generative model for MRI scans.
241
  
242
  - [brain_generator.py](SynthSeg/brain_generator.py): contains the class `BrainGenerator`, which is a wrapper around 
243
  `labels_to_image_model`. New images can simply be generated by instantiating an object of this class, and call the 
244
  method `generate_image()`.
245
  
246
  - [training.py](SynthSeg/training.py): contains code to train the segmentation network (with explanations for all 
247
  training parameters). This function also shows how to integrate the generative model in a training setting.
248
  
249
  - [predict.py](SynthSeg/predict.py): prediction and testing.
250
   
251
  - [validate.py](SynthSeg/validate.py): includes code for validation (which has to be done offline on real images).
252
 
253
- [models](models): this is where you will find the trained model for SynthSeg.
254
 
255
- [data](data): this folder contains some examples of brain label maps if you wish to train your own SynthSeg model.
256
 
257
- [script](scripts): contains tutorials as well as scripts to launch trainings and testings from a terminal.
258
259
- [ext](ext): includes external packages, especially the *lab2im* package, and a modified version of *neuron*.
260
261
262
----------------
263
264
### Citation/Contact
265
266
This code is under [Apache 2.0](LICENSE.txt) licensing. 
267
268
- If you use the **cortical parcellation**, **automated QC**, or **robust version**, please cite the following paper:
269
270
**Robust machine learning segmentation for large-scale analysisof heterogeneous clinical brain MRI datasets** \
271
B. Billot, M. Colin, Y. Cheng, S.E. Arnold, S. Das, J.E. Iglesias \
272
PNAS (2023) \
273
[ [article](https://www.pnas.org/doi/full/10.1073/pnas.2216399120#bibliography) | [arxiv](https://arxiv.org/abs/2203.01969) | [bibtex](bibtex.bib) ]
274
275
276
- Otherwise, please cite:
277
278
**SynthSeg: Segmentation of brain MRI scans of any contrast and resolution without retraining** \
279
B. Billot, D.N. Greve, O. Puonti, A. Thielscher, K. Van Leemput, B. Fischl, A.V. Dalca, J.E. Iglesias \
280
Medical Image Analysis (2023) \
281
[ [article](https://www.sciencedirect.com/science/article/pii/S1361841523000506) | [arxiv](https://arxiv.org/abs/2107.09559) | [bibtex](bibtex.bib) ]
282
283
If you have any question regarding the usage of this code, or any suggestions to improve it, please raise an issue or 
284
contact us at: bbillot@mit.edu