Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Hands-On Deep Learning for Images with TensorFlow

You're reading from  Hands-On Deep Learning for Images with TensorFlow

Product type Book
Published in Jul 2018
Publisher Packt
ISBN-13 9781789538670
Pages 96 pages
Edition 1st Edition
Languages
Author (1):
Will Ballard Will Ballard
Profile icon Will Ballard

An Image Classification Server

In this chapter, we're going to be taking the machine learning models we've learned about and turn them into REST servers for image classification.

In this chapter, we'll be covering the following topics:

  • Making a REST API definition with OpenAPI or Swagger
  • Creating a Docker container to create a repeatable build environment
  • Making predictions with our API and posting images over HTTP

REST API definition

Let's begin by defining the REST API. This is comprised of four activities: getting the project source code from GitHub with git; installing the necessary packages and reviewing the packages that will be needed in order to run our server; editing and creating the OpenAPI or Swagger definition file in YAML; and then finally handling a POST-ed image in that code that the REST API takes to turn an actual image file into a tensor.

First, we need to clone the repository that we've provided in order to have a REST service. I'm getting this over HTTPS and cloning it with the command line:

$ git clone https://github.com/wballard/kerasvideo-server/tree/2018.git

You can put it in any directory you like. Afterwards, we'll be able to use this source code for the rest of this section, and in the remaining chapters of this book:

Files in Kerasvideo...

Trained models in Docker containers

In the last section, we looked at creating a REST server for classifying images. In this section, we're going to look at preparing a Docker container to create a reasonable runtime environment for that server. As we look into this, we're going to ask the question: why use Docker to package up our machine learning models? Then, we'll actually investigate model training and then save a trained model for use in the Docker container followed by our server Dockerfile, which will package this all together. Finally, we'll build the Docker container for the reusable runtime of our REST service.

So, why Docker? Fundamentally, it makes your trained model portable. Unlike most of the programs you've created, which are mostly code with a separate database, a machine learning model is typically going to have a relatively large set...

Making predictions

In the previous section, we set up our Docker container, and now, in this section, we'll be using our Docker container to run a REST server and make predictions. We're going to be running our Docker container that we just created and then look at the connected built-in user interface to test our REST service. Finally, we'll post an image with that REST service so that we can see a prediction come back. We'll also see how you can call through to your service with curl, a command-line program that can post files.

Now, we're going to be starting up our Docker container. We'll be mapping the local port 5000 through to the container port 5000, which is the default in our REST service. Then, we'll start the service up. The kerasvideo-server container is the one we just created, and this container will take a second to start up and...

Summary

We've actually used a Swagger API definition to create a REST API model that then declaratively generates the Python framework in order for us to serve that API. We just had to put in a very small amount of code in order to get it running. Then, we created a Docker container that captures not only our running code that is our service, but our pre-trained machine learning model, which then forms a package so that we are able to deploy and use our container. Finally, we used this container in order to serve and make predictions.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Hands-On Deep Learning for Images with TensorFlow
Published in: Jul 2018 Publisher: Packt ISBN-13: 9781789538670
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}