Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

How to Install Keras on Docker and Cloud ML

Save for later
  • 3 min read
  • 20 Dec 2017

article-image

[box type="note" align="" class="" width=""]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.[/box]

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 (TensorFlow, Theano, 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:

how-to-install-keras-on-docker-and-cloud-ml-img-0

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

how-to-install-keras-on-docker-and-cloud-ml-img-1

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

how-to-install-keras-on-docker-and-cloud-ml-img-2

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

how-to-install-keras-on-docker-and-cloud-ml-img-3

Access it directly from the host machine on port:

how-to-install-keras-on-docker-and-cloud-ml-img-4

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:

how-to-install-keras-on-docker-and-cloud-ml-img-5

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

how-to-install-keras-on-docker-and-cloud-ml-img-6

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at AU $24.99/month. Cancel anytime

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:

how-to-install-keras-on-docker-and-cloud-ml-img-7

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

how-to-install-keras-on-docker-and-cloud-ml-img-8

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

how-to-install-keras-on-docker-and-cloud-ml-img-9In the next screenshot, we see the graph of cross-entropy:

how-to-install-keras-on-docker-and-cloud-ml-img-10

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:

how-to-install-keras-on-docker-and-cloud-ml-img-11

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.

how-to-install-keras-on-docker-and-cloud-ml-img-12