Deep Learning with R + Keras
Keras has grown in popularity and supported on a wide set of platforms including Tensorflow, CNTK, Apple’s CoreML, and Theano. It is becoming the de factor language for deep learning.
By Rajiv Shah, Data Scientist, Professor.
For R users, there hasn’t been a production grade solution for deep learning (sorry MXNET). This post introduces the Keras interface for R and how it can be used to perform image classification. The post ends by providing some code snippets that show Keras is intuitive and powerful.
Last January, Tensorflow for R was released, which provided access to the Tensorflow API from R. This was signficant, as Tensorflow is the most popular library for deep learning. However, for most R users, the Tensorflow for R interface was not very R like. Take a look at this code chunk for training a model:
Unless you are familiar with Tensorflow, it’s not readily apparent what is going on. A quick search on Github finds less than a 100 code results using Tensorflow for R. ????
All this is going to change with Keras and R! ☺️
For background, Keras is a high-level neural network API that is designed for experimentation and can run on top of Tensorflow. Keras is what data scientists like to use. ???? Keras has grown in popularity and supported on a wide set of platforms including Tensorflow, CNTK, Apple’s CoreML, and Theano. It is becoming the de factor language for deep learning.
As a simple example, here is the code to train a model in Keras:
Image Classification with Keras
So if you are still with me, let me show you how to build deep learning models using R, Keras, and Tensorflow together. You will find a Github repo at https://github.com/rajshah4/image_keras/ that contains the code and data you will need. Included is an R notebook (and Python notebooks) that walk through buiding an image classifier (telling ???? from ????), but can easily be generalized to other images. The walk through includes advanced methods that are commonly used for production deep learning work including:
- augmenting data
- using the bottleneck features of a pre-trained network
- fine-tuning the top layers of a pre-trained network
- saving weights of models
Code Snippets of Keras
The R interface to Keras truly makes it easy to build deep learning models in R. Here are some code snippets to illustrate how intuitive and useful Keras for R is:
To load ???? from a folder:
To define a simple convolutional neural network:
To augment data:
To load a pretrained network:
To save model weights:
I believe the Keras for R interface will make it much easier for R users and the R community to build and refine deep learning models with R. This means you don’t have to force everyone to use Python to build, refine, and test your models. I really think this will open up deep learning to a wider audience that was a bit apprehensive on using python. So for now, give it a spin!
Grab my repo, fire up RStudio (or your IDE of choice), and go build a simple classifier using Keras.
Bio: Rajiv Shah has a passion for understanding the dynamics between technology and people. He was educated as an electrical engineer, went to law school, but found in communications a home for his research. Rajiv left academia a few years ago and now works as a data scientist. He is always looking for new projects that engage me.
Original. Reposted with permission.