Medical Image Analysis with Deep Learning , Part 3
In this article we will focus — basic deep learning using Keras and Theano. We will do 2 examples one using keras for basic predictive analytics and other a simple example of image analysis using VGG.
By Taposh Roy, Kaiser Permanente.
In the last article we will talk about basics of deep learning from the lens of Convolutional Neural Nets. In this article we will focus — basic deep learning using Keras and Theano. We will do 2 examples one using keras for basic predictive analytics and other a simple example of image analysis using VGG.
I have realized that this topic is broad and deep and will need a few more articles. In the next few articles we will discuss difference between DICOMand NIFTI formats for medical imaging , expand our learning further and discuss how to use deep learning for 2D lung segmentation analysis. We then move to analyze 3D lung segmentation. We will also discuss how medical image analysis was done prior deep learning and how we can do it now. I would also like to welcome and thank my new partners who will help me with putting this all together — Flavio Trolese, Partner at 4Quant, Kevin Mader, Co-founder of 4Quant and Lecturer at ETH Zurich and Cyriac Joshy.
In this article we will discuss Keras and use two examples one showing how to use keras for simple predictive analysis tasks and other doing a image analysis.
What is Keras?
Keras api running on top of theano and tensorflow.
What is Theano and Tensor flow?
Theano as stated by Dr. James Bergstra et.al. at Scipy 2010, is a CPU and GPU math expression compiler. It is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. Theano is a joint work done by some high profile researchers such as Yoshua Bengio and others Montereal Institute for Learning Algorithms (MILA). A very good tutorial on Theano at scipy 2010 is here. The picture below shows a comparison of Theano on GPU and CPU vs a few other tools as of 2010. This was published in the original paper “Theano: A CPU and GPU Math Compiler in Python”
Theano: A CPU and GPU Math Compiler in Python
James Bergstra, Olivier Breuleux, Frédéric Bastien, Pascal Lamblin, Razvan Pascanu, Guillaume Desjardins,
Joseph Turian, David Warde-Farley, Yoshua Bengio
TensorFlow was developed by researchers and engineers working on the Google Brain Team within Google’s Machine Intelligence research organization. It was developed for conducting machine learning and deep neural networks research, but the system is general enough to be applicable in a wide variety of other domains as well. TensorFlow™, as stated from their website is an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) communicated between them. Visually the code would like the figure shown below [Source]
TensorFlow: Large-Scale Machine Learning on Heterogeneous Distributed Systems
Example of Predictive Analytics with Keras
In this example we will use Sonar data set from UCI website to do a simple predictive model. In the code below, we get data directly from the UCI website and split it in 60:40 ratio for training vs testing. We use keras for predictive modeling and sklearn for label encoding.
In the next snippet of code we read the data-set and see the data using the functions we defined above. We print the data-set and find out that our dependent variable needs encoding.
We use LabelEncoder from scikit-learn to do label encoding to covert R and M into numbers 0 and 1. The generic process of doing this is called one-hot encoding. One hot encoding transforms categorical features to a format that works better with algorithms. In this example our Y variable was categorical with values “R” and “M”. Using the Label encoder we converted this into “1” and “0”.
Label encoder from Scikit-learn
We then create a model using Keras.
The accuracy using simplistic model without any pre-processing is 81.64%
Example Image Analysis with Keras
In order to explain image processing with keras, we will use data from Kaggle competition — dogs and cats. The goal of this competition is to develop an algorithm to classify whether images contain either a dog or a cat. This task is easy for humans, dogs, and cats but not for computers. In this challenge there are 25,000 labelled dog and cat photos available for training, and 12,500 in the test set that we have to try to label for this competition. According to the Kaggle web-site, when this competition was launched (end of 2013):
“State of the art: The current literature suggests machine classifiers can score above 80% accuracy on this task”. So if we can beat 80%, then we will be at the cutting edge as at 2013!
I highly recommend MOOC from Fast.ai for more details, learning next steps and cutting edge research on the deep-learning. I have referred fast.ai for the code below as well as it serves an excellent starting point.
Step 1: Setting it up
Download the data from Kaggle website for the dogs and cats, and save it on your laptop. For the example in this article I am running the code on my mac.
Jeremy Howard, in his class has provided a utility python file that helps to get the basic functions encapsulated. For the initial part we will use this utility file. The file is available here. As we go into more details we will unpack this file and see whats behind it.
Step 2: Using VGG
Our first step is simply to use a model that has been fully created for us, which can recognize a wide variety (1,000 categories) of images. We will use ‘VGG’, which won the 2014 Imagenet competition, and is a very simple model to create and understand. The VGG Imagenet team created both a larger, slower, slightly more accurate model (VGG 19) and a smaller, faster model (VGG 16). We will be using VGG 16 since the much slower performance of VGG19 is generally not worth the very minor improvement in accuracy.
We have created a python class, Vgg16, which makes using the VGG 16 model very straightforward. Vgg16 is also available from fast.ai’s github details are here.
Step 3: Instantiate VGG
Vgg16 is built on top of Keras (which we will be learning much more about shortly!), a flexible, easy to use deep learning library that sits on top of Theano or Tensorflow. Keras reads groups of images and labels in batches, using a fixed directory structure, where images from each category for training must be placed in a separate folder.
Let’s grab batches of data from our training folder:
Step 4: Predict Dog vs Cats
Step 5: Summing it up and code files…
To sum this up for this article, the approach for dogs and cats classification that I recommend is —
If you are with me this far, then you have taken the theory we discussed in last article and done some practical programming. If you follow the instructions above and do the two examples, you have done your first predictive model using Keras and done image analysis as well. Due to the length of the code, I have not discussed the details in here, but have linked them. If you review the links and have questions, please feel to reach out to me or fast.ai (for dogs-cats image analysis)
- Keras Documentation
- Theano 0.9.0 Documentation
- Develop Your First Neural Network in Python With Keras Step-By-Step
- Theano: A CPU and GPU Math Compiler in Python
- One Hot Encoding for Machine learning
- Fast.ai utils.py file
- Deep MRI brain extraction: A 3D convolutional neural network for skull stripping
Bio: Taposh Roy leads innovation team in Kaiser Permanente's Decision Support group. He works with research, technology and business leaders to derive insights from data.
Original. Reposted with permission.