Repository containing code, models and tutorials for the paper "Deep neural networks learn general and clinically relevant representations of the ageing brain"
- Clone the github repo
git clone [email protected]:estenhl/pyment-public.git - Enter the folder
cd pyment-public - Create a conda environment
conda create --name pyment python=3.9 - Activate environment
conda activate pyment - Install required packages
pip install -r requirements.txt - Install Tensorflow
a. Tensorflow for GPU
pip install tensorflow-gpu
b. Tensorflow for CPU
pip install tensorflow - Source the package
conda develop .
While the models adhere to the Keras Model interface and can thus be used however one wants, we have provided Dataset/Generator-classes for nifti-files which are used in the tutorials. For these classes to work off-the-shelf the Nifti-data has to be organized in the following folder structure:
.
├── labels.csv
└── images
├── image1.nii.gz
├── image2.nii.gz
...
└── imageN.nii.gz
where labels.csv is a csv-file with column id (corresponding to image1, image2, etc) and column age.
Before training the models all images were ran through the following preprocessing pipeline:
- Extract brainmask with
recon-all -autorecon1(FreeSurfer) - Transform to *.nii.gz with
mri_convert(FreeSurfer) - Translate to FSL space with
fslreorient2std(FSL) - Register to MNI space with
flirt -dof 6(FSL, linear registration), and the standard FSL templateMNI152_T1_1mm_brain.nii.gz - Crop away borders of
[6:173,2:214,0:160]
A full example which downloads the IXI dataset and preprocesses it can be found in the Preprocessing tutorial
Estimating brain age using the trained brain age model from the paper consists of downloading the weights, instantiating the model with said weights, and calling Model.fit() with an appropriate generator. A full tutorial (which relies on having a prepared dataset) can be found in the Python prediction tutorial
If you want to use docker containers for predictions there are two options: 1. Configuring and building your own docker container 2. Using one of our prebuilt containers
We recommend only building your own docker container if you want to configure it yourself, e.g. by using a different model or your own trained weights. If so, there is a set of dockerfiles in the docker-folder that can be used as starting points. Building a container for estimating brain age using SFCN-reg with our pretrained weights can e.g. be done via
docker build \
--tag estenhl/sfcn-reg-predict-brain-age \
--file docker/Dockerfile.predict .
We have built a set of docker containers containing different models, weights and preprocessing schemes in our dockerhub account. Downloading one of these can be done via
docker pull estenhl/sfcn-reg-predict-brain-age
When you have a container with the model, it needs to be run to get the brain age estimates. If you are using one of our containers it needs access to two volumes (e.g. folders on your local computer) which is passed to the docker run command via the --mount-argument. One folder should contain the images and the labels as explained in the Preparing data-section. The second should be a folder where the predictions and the logs are written.
NOTE: If using a container which does not preprocess data (e.g. does not explicitly have preprocess in its name) the data needs to be preprocessed according to our Preprocessing pipeline beforehand
Running our prebuilt sfcn-reg-predict-brain-age-container can be done via e.g.
docker run \
--rm \
--name predict-brain-age \
--mount type=bind,source=<path-to-folder-with-images>,target=/images \
--mount type=bind,source=<path-to-folder-with-predictions>,target=/predictions \
estenhl/sfcn-reg-predict-brain-age
To build the freesurfer container you need a freesurfer 5.3.0 tar.gz-file available within the root folder of this project
docker build \
--tag estenhl/freesurfer:5.3 \
--file docker/Dockerfile.freesurfer \
--build-arg tarPath=freesurfer-Linux-centos6_x86_64-stable-pub-v5.3.0.tar.gz .
Test the freesurfer-container by copying in an image and running recon-all. Note that you need to copy in a valid free-surfer license for the container to run successfully
docker run \
-it \
--rm \
--mount type=bind,source=/Users/esten/freesurfer-license.txt,target=/usr/local/freesurfer/license.txt \
--mount type=bind,source=<path-to-example-image>,target=/tmp.nii.gz \
estenhl/freesurfer:5.3
mkdir subjects
recon-all -sd subjects -s tmp -i /tmp.nii.gz -autorecon1
docker build \
--tag estenhl/freesurfer_and_fsl:6.0 \
--file docker/Dockerfile.freesurfer_and_fsl .
Test the FSL-portion of the container by running flirt
flirt \
-in tmp.nii.gz \
-out flirted.nii.gz \
-ref /usr/local/fsl/data/linearMNI/MNI152lin_T1_1mm_brain.nii.gz