A scalable Keras + deep learning REST API

A scalable Keras + deep learning REST API

In today’s blog post we are going to create a deep learning REST API that wraps a Keras model in an efficient, scalable manner.

Our Keras + deep learning REST API will be capable of batch processing images, scaling to multiple machines (including multiple web servers and Redis instances), and round-robin scheduling when placed behind a load balancer.

To accomplish this we will be using:

  • Keras
  • Redis (an in-memory data structure store)
  • Flask (a micro web framework for Python)
  • Message queuing and message broker programming paradigms

This blog post is a bit more advanced than other tutorials on PyImageSearch and is intended for readers:

  • Who are familiar with the Keras deep learning library
  • Who have an understanding of web frameworks and web services (and ideally coded a simple website/web service before)
  • Who understand basic data structures, such as hash tables/dictionaries, lists, along with their associated asymptotic complexities

 

A scalable Keras + deep learning REST API

Today’s tutorial is broken into multiple parts.

We’ll start with a brief discussion of the Redis data store and how it can be used to facilitate message queuing and message brokering.

From there, we’ll configure our Python development environment by installing the required Python packages to build our Keras deep learning REST API.

Once we have our development environment configured we can implement our actual Keras deep learning REST API using the Flask web framework. After implementing, we’ll start the Redis and Flask servers, follow by submitting inference requests to our deep learning API endpoint using both cURL and Python.

Finally, we’ll end with a short discussion on the considerations you should keep in mind when building your own deep learning REST API.

A short introduction to Redis as a REST API message broker/message queue

Figure 1: Redis can be used as a message broker/message queue for our deep learning REST API

Redis is an in-memory data store. It is different than a simple key/value store (such as memcached) as it can can store actual data structures.

Today we’re going to utilize Redis as a message broker/message queue. This involves:

  • Running Redis on our machine
  • Queuing up data (images) to our Redis store to be processed by our REST API
  • Polling Redis for new batches of input images
  • Classifying the images and returning the results to the client

To read more about Redis, I encourage you to review this short introduction.

Configuring and installing Redis for our Keras REST API

Redis is very easy to install. Below you’ll find the commands to download, extract, and install Redis on your system:

To start the Redis server, use the following command:

Leave this terminal open to keep the Redis data store running.

In another terminal, you can validate Redis is up and running:

Provided that you get a PONG  back from Redis, you’re ready to go.

Configuring your Python development environment to build a Keras REST API

I recommend that you work on this project inside of a Python virtual environment so that it does not impact system level Python and projects.

To do this, you’ll need to install pip, virtualenv, and virtualenvwrapper (provided you haven’t already):

You’ll also need to edit your ~/.bashrc  (or ~/.bash_profile  on macOS) to include the following lines:

Then, simply source the file in the terminal depending on your OS:

Ubuntu

macOS

From there, you can create a Python virtual environment specifically for this project:

And once your environment is ready and activated, let’s install the necessary packages for our Keras REST API into the environment:

That’s it — and notice that we don’t actually need OpenCV for this project because we’ll be making use of PIL/Pillow.

Implementing a scalable Keras REST API

Figure 2: Our deep learning Keras + Redis + Flask REST API data flow diagram

Let’s get started building our server script. For convenience I’ve implemented the server in a single file, however it can be modularized as you see fit.

For best results and to avoid copy/paste errors, I encourage you to use the “Downloads”section of this blog post to grab the associated scripts and images.

Let’s open up run_keras_server.py  and walk through it together:

There are quite a few imports listed above, notably ResNet50 , flask , and redis .

For the sake of simplicity, we’ll be using ResNet pre-trained on the ImageNet dataset. I’ll point out where you can swap out ResNet for your own models.

The flask  module contains the Flask library (used to build our web API). The redis  module will enable us to interface with the Redis data store.

From there, let’s initialize constants which will be used throughout run_keras_server.py :

We’ll be passing float32  images to the server with dimensions of 224 x 224 and containing 3  channels.

Our server can handle a BATCH_SIZE = 32 . If you have GPU(s) on your production system, you’ll want to tune your BATCH_SIZE  for optimal  performance.

I’ve found that setting both SERVER_SLEEP  and CLIENT_SLEEP  to 0.25  seconds (the amount of time the server and client will pause before polling Redis again, respectively) will work well on most systems. Definitely adjust these constants if you’re building a production system.

Let’s kick off our Flask app and Redis server:

Here you can see how easy it is to start Flask.

I’ll assume that before you run this server script that your Redis server is running. Our Python script connect to the Redis store on our localhost  on port 6379  (the default host and port values for Redis).

Don’t forget to initialize a global Keras  model  to None here as well.

From there let’s handle serialization of images:

Redis will act as our temporary data store on the server. Images will come in to the server via a variety of methods such as cURL, a Python script, or even a mobile app.

Furthermore, images could come in only every once in awhile (a few every hours or days) or at a very high rate (multiple per second). We need to put the images somewhere as they queue up prior to being processed. Our Redis store will act as the temporary storage.

In order to store our images in Redis, they need to be serialized. Since images are just NumPy arrays, we can utilize base64 encoding to serialize the images. Using base64 encoding also has the added benefit of allowing us to use JSON to store additional attributes with the image.

Our base64_encode_image  function handles the serialization and is defined on Lines 35-37.

Similarly, we need to deserialize our image prior to passing them through our model. This is handled by the  base64_decode_image  function on Lines 39-51.

Let’s pre-process our image:

On Line 53, I’ve defined a prepare_image  function which pre-processes our input image for classification using the ResNet50 implementation in Keras.. When utilizing your own models I would suggest modifying this function to perform any required pre-processing, scaling, or normalization.

From there we’ll define our classification method:

The classify_process  function will be kicked off in its own thread as we’ll see in __main__  below. This function will poll for image batches from the Redis server, classify the images, and return the results to the client.

Line 72 loads the model . I’ve sandwiched this action with terminal print  messages — depending on the size of your Keras model, loading be instantaneous or it could take a few seconds.

Loading the model happens only once when this thread is launched — it would be terribly slow if we had to load the model each time we wanted to process an image and furthermore it could lead to a server crash due to memory exhaustion.

After loading the model, this thread will continually poll for new images and then classify them:

Here we’re first using the Redis database’s lrange  function to get, at most, BATCH_SIZE  images from our queue (Line 79).

From there we initialize our imageIDs  and batch  (Lines 80 and 81) and begin looping over the queue  beginning on Line 84.

In the loop, we first decode the object and deserialize it into a NumPy array, image  (Lines 86-88).

Next, on Lines 90-96, we’ll add the image  to the batch  (or if the batch  is currently None  we just set the batch  to the current image ).

We also append the id  of the image to imageIDs  (Line 99).

Let’s finish out the loop and function:

In this code block, we check if there are any images in our batch (Line 102).

If we have a batch of images, we make predictions on the entire batch by passing it through the model (Line 105).

From there, we loop over a the imageIDs  and corresponding prediction  results  (Lines 110-122). These lines append labels and probabilities to an output list and then store the output in the Redis database using the imageID  as the key (Lines 116-122).

We remove the set of images that we just classified from our queue using ltrim  on Line 125.

And finally, we sleep for the set SERVER_SLEEP  time and await the next batch of images to classify.

Let’s handle the /predict  endpoint of our REST API next:

As you’ll see later, when we POST to the REST API, we’ll be using the /predict  endpoint. Our server could, of course, have multiple endpoints.

We use the @app.route  decorator above our function in the format shown on Line 130 to define our endpoint so that Flask knows what function to call. We could easily have another endpoint which uses AlexNet instead of ResNet and we’d define the endpoint with associated function in a similar way. You get the idea, but for our purposes today, we just have one endpoint called /predict .

Our predict  method defined on Line 131 will handle the POST requests to the server. The goal of this function is to build the JSON data  that we’ll send back to the client.

If the POST data contains an image (Lines 137 and 138) we convert the image to PIL/Pillow format and preprocess it (Lines 141-143).

While developing this script, I spent considerable time debugging my serialization and deserialization functions, only to figure out that I needed Line 147 to convert the array to C-contiguous ordering (which is something you can read more about here). Honestly, it was a pretty big pain in the ass to figure out, but I hope it helps you get up and running quickly.

If you were wondering about the id  mentioned back on Line 99, it is actually generated here using uuid , a universally unique identifier, on Line 151. We use a UUID to prevent hash/key conflicts.

Next, we append the id  as well as the base64  encoding of the image  to the d  dictionary. It’s very simple to push this JSON data to the Redis db  using rpush  (Line 153).

Let’s poll the server to return the predictions:

We’ll loop continuously until the model server returns the output predictions. We start an infinite loop and attempt to get the predictions Lines 157-159.

From there, if the output  contains predictions, we deserialize the results and add them to data  which will be returned to the client.

We also delete  the result from the db  (since we have pulled the results form the database and no longer need to store them in the database) and break  out of the loop (Lines 163-172).

Otherwise, we don’t have any predictions and we need to sleep and continue to poll (Line 176).

If we reach Line 179, we’ve successfully got our predictions. In this case we add a success  value of True  to the client data (Line 179).

Note: For this example script, I didn’t bother adding timeout logic in the above loop which would ideally add a success  value of False  to the data. I’ll leave that up to you to handle and implement.

Lastly we call flask.jsonify  on data  and return it to the client (Line 182). This completes our predict function.

To demo our Keras REST API, we need a __main__  function to actually start the server:

Lines 186-196 define the __main__  function which will kick off our classify_process  thread (Lines 190-192) and run the Flask app (Line 196).

Starting the scalable Keras REST API

To test our Keras deep learning REST API, be sure to download the source code + example images using the “Downloads” section of this blog post.

From there, let’s start the Redis server if it isn’t already running:

Then, in a separate terminal, let’s start our REST API Flask server:

Additionally, I would suggest waiting until your model is loaded completely into memory before submitting requests to the server.

Now we can move on to testing the server with both cURL and Python.

Using cURL to access our Keras REST API

Figure 3: Using cURL to test our Keras REST API server. Pictured is my family beagle, Jemma. She is classified as a beagle with 94.6% confidence by our ResNet model.

The cURL tool is available pre-installed on most (Unix-based) operating systems. We can POST an image file to our deep learning REST API at the /predict  endpoint by using the following command:

You’ll receive the predictions back in JSON format right in your terminal:

Let’s try passing another image, this time a space shuttle:

The results of which can be seen below:

Figure 4: Submitting an input image to our Keras REST API and obtaining the prediction results.

Once again our Keras REST API has correctly classified the input image.

Using Python to submit requests to the Keras REST API

As you can see, verification using cURL was quite easy. Now let’s build a Python script that will POST an image and parse the returning JSON programmatically.

Let’s review simple_request.py :

We use Python requests  in this script to handle POSTing data to the server.

Our server is running on the localhost  and can be accessed on port 5000  with the endpoint /predict  as is specified by the KERAS_REST_API_URL  variable (Line 6). If the server is running remotely or on a different machine, be sure to specify the appropriate domain/ip, port, and endpoint.

We also define an IMAGE_PATH (Line 7). In this case, jemma.png  is in the same directory as our script. If you want to test with other images, be sure to specify the full path to your input image.

Let’s load the image and send it off to the server:

We read the image on Line 10 in binary mode and put the it into a payload dictionary.

The payload is POST’ed to the server with requests.post  on Line 14.

If we get a success  message, we can loop over the predictions and print them to the terminal. I made this script simple, but you could also draw the highest prediction text on the image using OpenCV if you want to get fancy.

Running the simple request script

Putting the script to work is easy. Open up a terminal and execute the following command (provided both our Flask server and Redis server are running, of course).

Figure 5: Using Python to programmatically consume the results of our Keras deep learning REST API.

For the space_shuttle.png , simply modify the IMAGE_PATH  variable:

And from there, run the script again:

Figure 6: A second example of programmatically consuming our Keras deep learning REST API. Here a space shuttle is classified with 99% confidence by ResNet + Keras REST API.

Considerations when scaling your deep learning REST API

If you anticipate heavy load for extended periods of time on your deep learning REST API you may want to consider a load balancing algorithm such as round-robin scheduling to help evenly distribute requests across multiple GPU machines and Redis servers.

Keep in mind that Redis is an in-memory data store so we can only store as many images in the queue we have available memory.

A single 224 x 224 x 3 image with a float32  data type will consume 60,2112 bytes of memory.

Assuming a server with a modest 16GB of RAM, this implies that we can hold approximately 26,500 images in our queue, but at that point we likely would want to add more GPU servers to burn through the queue faster.

However, there is a subtle problem…

Depending on how you deploy your deep learning REST API, there is a subtle problem with keeping the classify_process  function in the same file as the rest of our web API code.

Most web servers, including Apache and nginx, allow for multiple client threads.

If you keep classify_process  in the same file as your predict  view, then you may load multiple models if your server software deems it necessary to create a new thread to serve the incoming client requests — for every new thread, a new view will be created, and therefore a new model will be loaded.

The solution is to move classify_process  to an entirely separate process and then start it along with your Flask web server and Redis server.


小工蚁承诺为每一个客户量身打造新零售平台,提供全渠道电商平台、新零售平台系统和智慧无人零售平台。让企业软件像“中国制造”的商品一样物美价廉为使命。
了解小工蚁产品技术架构成功案例,想更好的了解我们的产品?联系我们,欢迎免费使用点击这里在线开通

原创文章,转载请注明: 转载自小工蚁科技

本文链接地址: A scalable Keras + deep learning REST API

沪公网安备 31010702002009号