Git Product home page Git Product logo

deep_mri_brain_extraction's Introduction


"""
This software is an implementation of the paper

"Deep MRI brain extraction: A 3D convolutional neural network for skull stripping"

You can download the paper at http://dx.doi.org/10.1016/j.neuroimage.2016.01.024

If you use this software for your projects please cite:

Kleesiek and Urban et al, Deep MRI brain extraction: A 3D convolutional neural network for skull stripping,
NeuroImage, Volume 129, April 2016, Pages 460-469.

The MIT License (MIT)

Copyright (c) 2016 Gregor Urban, Jens Kleesiek

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme version 1.0 - October 2016

"""

I. Introduction

Brain extraction from magnetic resonance imaging (MRI) is crucial for many neuroimaging workflows. However, the anatomical variability and the difference in intensity distributions of MRI scans make the development of a one-fits-all solution very challenging. This work is based on a 3D convolutional deep learning architecture that deals with arbitrary MRI modalities (T1, T2, FLAIR,DWI, ...), contrast-enhanced scans and pathologically altered tissues, when trained appropriately, i.e. tuned to the specific needs and imaging criteria present at your institution.

The presented code is a modified version of the work used for the above mentioned publication. There have been some alterations that might affect performance and speed.

II. Prerequesites & Installation

We strongly suggest using a GPU if speed is of necessity, as speedups of ~ 40x and more over CPU mode are typical. Nevertheless, the code will run on both CPU or GPU without modification. If you chose to use a GPU, make sure to use a NVIDIA model (these are the only cards supporting CUDA).

Prerequisites

** Python 2: We recommend the use of Anaconda (https://www.anaconda.com/products/individual#Downloads), especially for Windows users.

** Theano --

This is a GPU-toolkit which our code uses to build and train convolutional neural networks. It is straight forward to get working with GPUs on Linux, but slightly harder on Windows

E.g. see: https://pypi.org/project/Theano/

A quick summary to install Theano for Windows users:

  1. Install Anaconda (Python 2.7+ but not 3.x, x64)
  2. [in console] conda update conda
  3. [in console] conda update --all
  4. [in console] pip install Theano
  5. [in console] conda install mingw libpython

for GPU on Windows:

  1. install MS Visual Studio (sadly the express version currently does not work)
  2. install CUDA
  3. add to Path:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin [or equivalent] C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin [or equivalent] C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\libnvvp [or equivalent]

It is still tricky to get it to work, as Theano doesn't properly support Windows

** Linux/Windows: [optional] install NVIDIA cuDNN -- better convolution implementation for GPU users, large speed-gain for training and testing (dowload is free but requires registration)

** nibabel -- allows python code to load nifti files: [in console] pip install nibabel

** h5py -- file format

  1. [in console] conda install h5py or pip install h5py

Configuring Theano

In order to configue theano to automatically use the GPU, create a file named .theanorc (.theanorc.txt for Windows users) in your user's home directory and add the following three lines to it -- this is a minimalistic setup, many more options are available:

[global] floatX=float32 device=gpu0

You can replace "gpu0" with e.g. "cpu", or select another gpu using "gpu1" (provided you have two GPUs), etc.

Where can I obtain the data used in the publication

https://www.nitrc.org/projects/ibsr http://www.oasis-brains.org/app/action/BundleAction/bundle/OAS1_CROSS http://loni.usc.edu/atlases/Atlas_Detail.php?atlas_id=12

If you use this data please cite the corresponding publications and comply with the indicated copyright regulations stated.

Please understand, that the brain tumor data set used in our publication cannot be made publicly available.

III. Data preprocessing

Data pre-processing is not required, as long as the data does not contain artifacts/NaN entries etc. but it will most likely improve the results. The provided code automatically standardizes the data (zero mean, unit variance per volume), thus only nonlinear data pre-processing operations have any effect.

If you use data from different scanners that produce data with varying orientation it might be necessary to transform all your data to a common orientation. For instance, this might be achieved using fslreorient2std [http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FSL].

IV. Examples

Brain mask prediction using an already trained CNN

python deep3Dpredict.py --help
usage: deep3Dpredict.py [-h] -i I [I ...] [-o O] [-n N] [-c C] [-f F]
                        [-prob PROB] [-gridsize GRIDSIZE]

Main module to apply an already trained 3D-CNN to segment data

optional arguments:
  -h, --help          show this help message and exit
  -i I [I ...]        Any number and combination of paths to files or folders
                      that will be used as input-data for training the CNN
  -o O                output path for the predicted brain masks
  -n N                name of the trained/saved CNN model (can be either a
                      folder or .save file)
  -c C                Filter connected components: removes all connected
                      components but the largest two (i.e. background and
                      brain) [default=True]
  -f F                File saving format for predictions. Options are "h5",
                      "nifti", "numpy" [default=nifti]
  -prob PROB          save probability map as well
  -gridsize GRIDSIZE  size of CNN output grid (optimal: largest possible
                      divisor of the data-volume axes that still fits into GPU
                      memory). This setting heavily affects prediction times:
                      larger values are better. Values that are too large will
                      cause a failure due to too little GPU-memory.


python deep3Dpredict.py -n OASIS_ISBR_LPBA40__trained_CNN.save -i /home/share/brain_mask/__NEW__/ibsr_data/02/IBSR_02_ana.nii.gz -gridsize 16
 

Train a new CNN (with your data)

python deep3Dtrain.py --help
usage: deep3Dtrain.py [-h] -data DATA [DATA ...] -labels LABELS [LABELS ...]
                      [-lr LR] [-name NAME] [-convert_labels CONVERTLABELS]

Main module to train a 3D-CNN for segmentation

optional arguments:
  -h, --help            show this help message and exit
  -data DATA [DATA ...]
                        Any number and combination of paths to files or
                        folders that will be used as input-data for training
                        the CNN
  -labels LABELS [LABELS ...]
                        Any number and combination of paths to files or
                        folders that will be used as target for training the
                        CNN (values must be 0/1)
  -lr LR               initial learning rate (step size) for training the CNN (default: 10^(-5))
  -name NAME           name of the model (affects filenames) -- specify the
                        same name when using deep3Dtest.py
  -convert_labels CONVERTLABELS
                        if labels are not binary: this will convert values >1
                        to 1
  -data_clip_range [LOWER UPPER]
                        [optional] specify two values



For each data file you have to supply an associated label file. The file names should indicate their relationship such that alphabetical ordering results in a correct matching of the corresponding data and label files,
e.g. [vol1_data.nii.gz, vol2_data.nii.gz, ...] <-> [vol1_label.nii.gz, vol2_label.nii.gz, ...]

python deep3Dtrain.py -data data/ -labels labels/

VI. FAQ

  • How is it possible that the results reported in your paper differ from the results I obtain?

This can be due to several reasons. The presented method is not deterministic, i.e. there are a lot of random steps like initialization of the weights and order of the data cubes presented during training. Therefor, every trained CNN is somewhat unique. Further, the hardware and software libraries used also have an impact on speed and performance. With this software code we also included a pre-trained net (OASIS_ISBR_LPBA40__trained_CNN.save) that exhibits very good results on these three data sets. The speed of training and prediction depends on several factors: the computer hardware (and GPU model), the used convolution backend (e.g. cuDNN and the specific version of it vs. the slower Theano convolution) and the "gridsize" parameter (higher values are more efficient for prediction but require more GPU memory, the default value of 16 is a conservative choice)

  • I have a problem running the code / found a bug, who should I contact?

send an email to: [email protected]

deep_mri_brain_extraction's People

Contributors

gur9000 avatar jensnrad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

deep_mri_brain_extraction's Issues

Some question about code

In Segmentation_predictor line 132
pred[c, x::CNET_stride[0], y::CNET_stride[1], z::CNET_stride[2]] = rr[0,:,c,:,:].reshape((pred_size[0], pred_size[1], pred_size[2]))

I really want to know why the pred's value is determined by this. I mean it jumps.
And at the top of this line I notice that you only shift the data one vixel but you can get twice output. I think it is overlap. Maybe I misunderstand your paper? I am really confused.

RuntimeError: error getting worksize: CUDNN_STATUS_BAD_PARAM

$ python deep3Dpredict.py -name OASIS_ISBR_LPBA40__trained_CNN.save -data TCGA-02-0046_1998.11.28_t2.nii.gz -output deep-mask.nii -gridsize 16 
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
Using cuDNN version 6021 on context None
Mapped name None to device cuda: GeForce GTX 1080 (0000:01:00.0)
/home/mingrui/anaconda3/envs/p2_3DUnetCNN/lib/python2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
using model-parameters: OASIS_ISBR_LPBA40__trained_CNN.save
loading...
Loaded... 100.0 % (240, 1, 240, 155)  ('TCGA-02-0046_1998.11.28_t2.nii.gz', None)
Total n. of examples: 1 images/volumes
Training on 0 images/volumes
Testing on  1 images/volumes
Building CNN...
Traceback (most recent call last):
  File "deep3Dpredict.py", line 200, in <module>
    main()
  File "deep3Dpredict.py", line 194, in main
    network_size_factor = float(args.CNN_width_scale))
  File "deep3Dpredict.py", line 116, in predict
    auto_threshold_labels = auto_threshold_labels)
  File "/mnt/960EVO/workspace/Deep_MRI_brain_extraction/utils/Segmentation_trainer.py", line 232, in Build3D
    dense_output_from_fragments = is_last_layer and use_fragment_pooling)
  File "/mnt/960EVO/workspace/Deep_MRI_brain_extraction/NNet_Core/NN_ConvNet.py", line 350, in addConvLayer
    output_stride = self.output_stride, verbose = self.verbose)
  File "/mnt/960EVO/workspace/Deep_MRI_brain_extraction/NNet_Core/NN_ConvLayer_3D.py", line 271, in __init__
    output_shape = list(  theano.function([input], self.output.shape, mode = self.mode)(numpy.zeros((1 if input_shape[0]==None else input_shape[0],)+input_shape[1:],dtype=numpy.float32)))
  File "/home/mingrui/anaconda3/envs/p2_3DUnetCNN/lib/python2.7/site-packages/theano/compile/function_module.py", line 917, in __call__
    storage_map=getattr(self.fn, 'storage_map', None))
  File "/home/mingrui/anaconda3/envs/p2_3DUnetCNN/lib/python2.7/site-packages/theano/gof/link.py", line 325, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
  File "/home/mingrui/anaconda3/envs/p2_3DUnetCNN/lib/python2.7/site-packages/theano/compile/function_module.py", line 903, in __call__
    self.fn() if output_subset is None else\
RuntimeError: error getting worksize: CUDNN_STATUS_BAD_PARAM
Apply node that caused the error: GpuDnnConv{algo='small', inplace=True, num_groups=1}(GpuContiguous.0, GpuContiguous.0, GpuAllocEmpty{dtype='float32', context_name=None}.0, GpuDnnConvDesc{border_mode='valid', subsample=(1, 1), dilation=(1, 1), conv_mode='conv', precision='float32', num_groups=1}.0, Constant{1.0}, Constant{0.0})
Toposort index: 26
Inputs types: [GpuArrayType<None>(float32, (False, True, False, False)), GpuArrayType<None>(float32, (False, True, False, False)), GpuArrayType<None>(float32, 4D), <theano.gof.type.CDataType object at 0x7ff807861bd0>, Scalar(float32), Scalar(float32)]
Inputs shapes: [(83, 1, 83, 83), (64, 1, 4, 4), (83, 64, 80, 80), 'No shapes', (), ()]
Inputs strides: [(27556, 27556, 332, 4), (64, 64, 16, 4), (1638400, 25600, 320, 4), 'No strides', (), ()]
Inputs values: ['not shown', 'not shown', 'not shown', <capsule object NULL at 0x7ff8079ad900>, 1.0, 0.0]
Outputs clients: [[GpuReshape{5}(GpuDnnConv{algo='small', inplace=True, num_groups=1}.0, TensorConstant{[83 16  4 80 80]})]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
  File "deep3Dpredict.py", line 200, in <module>
    main()
  File "deep3Dpredict.py", line 194, in main
    network_size_factor = float(args.CNN_width_scale))
  File "deep3Dpredict.py", line 116, in predict
    auto_threshold_labels = auto_threshold_labels)
  File "/mnt/960EVO/workspace/Deep_MRI_brain_extraction/utils/Segmentation_trainer.py", line 232, in Build3D
    dense_output_from_fragments = is_last_layer and use_fragment_pooling)
  File "/mnt/960EVO/workspace/Deep_MRI_brain_extraction/NNet_Core/NN_ConvNet.py", line 350, in addConvLayer
    output_stride = self.output_stride, verbose = self.verbose)
  File "/mnt/960EVO/workspace/Deep_MRI_brain_extraction/NNet_Core/NN_ConvLayer_3D.py", line 217, in __init__
    filters_shape=filter_shape, signals_shape = input_shape if input_shape[0]!=None else None

HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

I tried using tcia nifti data, this is the error

deep3Dpredict.py does not recognize .save file

There is a bug in deep3Dpredict.py:

python Deep_MRI_brain_extraction/deep3Dpredict.py -n Deep_MRI_brain_extraction/OASIS_ISBR_LPBA40__trained_CNN.save -i  $target -gridsize 16
ValueError: The provided save file/directory does not contain any saved model (file ending in .save)

I tracked the error to the line candidates = findall(path_or_file) which returns

['D', 'e', 'e', 'p', '_', 'M', 'R', 'I', '_', 'b', 'r', 'a', 'i', 'n', '_', 'e', 'x', 't', 'r', 'a', 'c', 't', 'i', 'o', 'n', '/.autofsck', 'O', 'A', 'S', 'I', 'S', '_', 'I', 'S', 'B', 'R', '_', 'L', 'P', 'B', 'A', '4', '0', '_', '_', 't', 'r', 'a', 'i', 'n', 'e', 'd', '_', 'C', 'N', 'N', ...

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.