3 min read
The following extract is taken from the book Deep Learning with Keras, written by Antonio Gulli and Sujit Pal. It contains useful techniques to train effective deep learning models using the highly popular Keras library.

Keras is a deep learning library which can be used on the enterprise platform, by deploying it on a container. In this article, we see how to install Keras on Docker and Google’s Cloud ML.

Installing Keras on Docker

One of the easiest ways to get started with TensorFlow and Keras is running in a Docker container. A convenient solution is to use a predefined Docker image for deep learning created by the community that contains all the popular DL frameworks ( TensorFlowTheano, Torch, Caffe, and so on). Refer to the GitHub repository at https://github.com/saiprashanths/dl-docker for the code files. Assuming that you already have Docker up and running (for more information, refer to https://www.docker.com/products/overview), installing it is pretty simple and is shown as follows:

Setting Up Keras 1

The following screenshot, says something like, after getting the image from Git, we build the Docker image:

Setting up keras 2

In this following screenshot, we see how to run it:

Setting up keras 4

From within the container, it is possible to activate support for Jupyter Notebooks (for more information, refer to http://jupyter.org/):

Access it directly from the host machine on port:

Setting up keras 6

It is also possible to access TensorBoard (for more information, refer to https://www.tensorflow.org/how_tos/summaries_and_tensorboard/) with the help of the command in the screenshot that follows, which is discussed in the next section:

Setting up keras 7

After running the preceding command, you will be redirected to the following page:

Setting up keras 8

Installing Keras on Google Cloud ML

Installing Keras on Google Cloud is very simple. First, we can install Google Cloud (for the downloadable file, refer to https://cloud.google.com/sdk/), a command-line interface for Google Cloud Platform; then we can use CloudML, a managed service that enables us to easily build machine, learning models with TensorFlow. Before using Keras, let’s use Google Cloud with TensorFlow to train an MNIST example available on GitHub. The code is local and training happens in the cloud:

Setting up keras 9

In the following screenshot, you can see how to run a training session:

Setting up keras 10

We can use TensorBoard to show how cross-entropy decreases across iterations:

Setting up keras 11In the next screenshot, we see the graph of cross-entropy:

Setting up keras 12

Now, if we want to use Keras on the top of TensorFlow, we simply download the Keras source from PyPI (for the downloadable file, refer to https://pypi.python.org/pypi/Keras/1.2.0 or later versions) and then directly use Keras as a CloudML package solution, as in the following example:

Setting up keras 9

Here, trainer.task2.py is an example script:

from keras.applications.vgg16 import VGG16

from keras.models import Model

from keras.preprocessing import image

from keras.applications.vgg16 import preprocess_input

import numpy as np

# pre-built and pre-trained deep learning VGG16 model

base_model = VGG16(weights='imagenet', include_top=True)

for i, layer in enumerate(base_model.layers):

print (i, layer.name, layer.output_shape)

Thus we saw, how fairly easy it is to set up and run Keras on a Docker container and Cloud ML.

If this article interested you, make sure to check out our book Deep Learning with Keras, where you can learn to install Keras on other popular platforms such as Amazon Web Services and Microsoft Azure.

Deep learning with keras



Please enter your comment!
Please enter your name here