Packt Hands-On Serverless Deep Learning With Tensorflow and Aws Lambda 2019
Packt Hands-On Serverless Deep Learning With Tensorflow and Aws Lambda 2019
Lambda
Rustem Feyzkhanov
BIRMINGHAM - MUMBAI
Hands-On Serverless Deep
Learning with TensorFlow and
AWS Lambda
Copyright © 2019 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any
means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or
reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the
information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or
its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this
book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this
book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
ISBN 978-1-83855-160-5
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over 5,000
books and videos, as well as industry leading tools to help you plan your
personal development and advance your career. For more information,
please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks
and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
At www.packt.com, you can also read a collection of free technical articles, sign
up for a range of free newsletters, and receive exclusive discounts and
offers on Packt books and eBooks.
Contributors
About the author
Rustem Feyzkhanov is a machine learning engineer at Instrumental. He
works on creating analytical models for the manufacturing industry. He is
also passionate about serverless infrastructures and AI deployment. He has
ported several packages on AWS Lambda, ranging from
TensorFlow/Keras/sklearn for machine learning to
PhantomJS/Selenium/WRK for web scraping. One of these apps was
featured on the AWS serverless repository's home page.
Packt is searching for authors like
you
If you're interested in becoming an author for Packt, please visit authors.packt
pub.com and apply today. We have worked with thousands of developers and
tech professionals, just like you, to help them share their insight with the
global tech community. You can make a general application, apply for a
specific hot topic that we are recruiting an author for, or submit your own
idea.
Table of Contents
Title Page
Copyright and Credits
Hands-On Serverless Deep Learning with TensorFlow and AWS Lambda
About Packt
Why subscribe?
Packt.com
Contributors
About the author
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
1. Beginning with Serverless Computing and AWS Lambda
What is serverless computing?
Why serverless deep learning?
Where serverless deep learning works and where it doesn't work?
Where serverless deep learning works?
Where serverless deep learning doesn't work?
Lambda function – AWS implementation of FaaS
Lambda triggers
Why deep learning on AWS Lambda?
Traditional versus Serverless architecture using Lambda
Sample projects
Deep learning API
Deep learning batch processing
Serverless deep learning workflow
Summary
2. Starting Deployment with AWS Lambda Functions
Technical Requirements
AWS Lambda functions
Pros, cons, and limitations of AWS Lambda
Getting started with AWS account
AWS Free Tier
Identity and Access Management (IAM)
Creating a Hello World AWS Lambda function
Using the Lambda function
AWS Lambda code
Introduction to the serverless framework
Features of the serverless framework
Installation of the serverless framework
Deploying AWS Lambda functions using the serverless framework
Configuration file
Deployment process
Summary
3. Deploying TensorFlow Models
Technical Requirements
Approaches for building algorithms
Why neural networks?
Pre-trained networks
Simple TensorFlow example
Training for MNIST
Repositories for pre-trained TensorFlow models
TensorFlow repository
TensorFlow Hub
GitHub
Image captioning example
Inception v3
TensorFlow code for Inception v3
Running the code
Summary
4. Working with TensorFlow on AWS Lambda
Technical Requirements
Architecture of the deploying TensorFlow with AWS Lambda
Model within the deployment package
Pros
Cons
Model on the S3 bucket
Pros
Cons
Model on the HTTP/FTP server
Pros
Cons
General issues with deploying Python frameworks on AWS Lambda
Solutions for issues with deploying Python frameworks on AWS Lambda
Deploying TensorFlow on AWS Lambda using the pre-existing pack
Deploying TensorFlow using a serverless framework
Creating a bucket
Index.py
Serverless.yml
Summary
5. Creating the Deep Learning API
Technical Requirements
RESTful API
AWS API Gateway
Features
Pricing
Creating the API Gateway
Creating an AWS Lambda instance
Creating the API Gateway using the serverless framework
Deploying the serverless framework
Creating a first API project
Summary
6. Creating a Deep Learning Pipeline
Technical Requirements
Message queue
Introduction to AWS SQS
AWS API gateway features
AWS SQS pricing
Creating an AWS SQS connection using an AWS Console
Creating an AWS Lambda instance
Creating an AWS SQS connection using the serverless framework
The Python file
Code
serverless.yml
index.py
Example project – deep learning pipeline
Code
Configuration file - serverless.yml
index.py
Summary
7. Creating a Deep Learning Workflow
Technical requirements
Introduction to the AWS Step Functions service
Processing workflow 
AWS Step Functions
AWS step function features 
AWS Step Functions pricing 
Step functions versus SQS
Creating an AWS Step Functions connection to AWS Lambda using the AWS Con
sole
Creating the AWS Lambda instance
Creating the step function
Creating AWS step functions for an AWS Lambda instance using the serverle
ss framework
Step functions
Serverless deployment
Example project – deep learning workflow
Creating the model
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
Preface
This book prepares you to use your own custom-trained models with AWS
Lambda to achieve a simplified serverless computing approach without
spending much time and money. By the end of the book, you will be able to
implement a project that demonstrates the use of AWS Lambda in serving
TensorFlow models.
Later on, we will build a number of applications that will utilize the
serverless deep learning approach. We will make a planning API and take a
look at the AWS API Gateway service, and explore how to deploy
everything in a convenient way. In the later stages, we will make a deep
learning pipeline and an AWS simple query service. We will explore how to
use it with AWS Lambda and showcase how to deploy the application.
Who this book is for
This course will benefit data scientists who want to learn how to deploy
models easily, and beginners who want to learn about deploying into the
cloud. No prior knowledge of TensorFlow or AWS is required.
What this book covers
Chapter 1, Beginning with Serverless Computing and AWS Lambda, goes
through all the examples that we are planning to look at. We will also
describe the serverless concept and how it has changed the current cloud
infrastructure environment. Finally, we will see how serverless deep
learning allows us to make projects that are much easier to implement than
traditional deployment techniques, while being just as scalable and cheap.
Chapter 4, Working with TensorFlow on AWS Lambda, digs into the specifics
of how to start working with serverless TensorFlow. Also, we will see small
details of how serverless TensorFlow is different from traditional
deployment from the perspective of cost, scale, and speed. We will also
look at how to start with using the standard AWS UI, and then understand
how the same can be done using the Serverless Framework.
Once the file is downloaded, please make sure that you unzip or extract the
folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/P
acktPublishing/Hands-On-Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda. In
case there's an update to the code, it will be updated on the existing GitHub
repository.
We also have other code bundles from our rich catalog of books and videos
available at https://github.com/PacktPublishing/. Check them out!
Download the color images
We also provide a PDF file that has color images of the
screenshots/diagrams used in this book. You can download it here: http://ww
w.packtpub.com/sites/default/files/downloads/9781838551605_ColorImages.pdf.
Conventions used
There are a number of text conventions used throughout this book.
Bold: Indicates a new term, an important word, or words that you see
onscreen. For example, words in menus or dialog boxes appear in the text
like this. Here is an example: "Next, go to the Users page and click on Add
user."
Warnings or important notes appear like this.
General feedback: If you have questions about any aspect of this book,
mention the book title in the subject of your message and email us at
[email protected].
Errata: Although we have taken every care to ensure the accuracy of our
content, mistakes do happen. If you have found a mistake in this book, we
would be grateful if you would report this to us. Please visit www.packt.com/sub
mit-errata, selecting your book, clicking on the Errata Submission Form link,
and entering the details.
Piracy: If you come across any illegal copies of our works in any form on
the Internet, we would be grateful if you would provide us with the location
address or website name. Please contact us at [email protected] with a link
to the material.
In this chapter, we will discuss serverless deep learning and you will
explore why serverless is so popular and the advantages of deploying
applications using serverless. Also, you will look into the data science
process and how serverless can enable an easy and convenient way to
deploy deep learning applications. You will also briefly look into the
sample projects that we will make in the forthcoming chapters.
You will also get to learn about the workings of the AWS implementation,
including the traditional and serverless ways of deploying deep learning
applications.
It is extremely useful for your project. If you train your model and
want to show it to the world, serverless deep learning will allow you to
do so without complicated deployment and any upfront costs. AWS
also provides free usage per month. It means that a number of
invocations in AWS Lambda will be completely free. For example, in
the image recognition project, which we will discuss in the following
chapters, the number of runs will be about 100,000 times.
Serverless deep learning is great for early-stage startups that want to
test their business hypotheses as early as possible. Simplicity and
scalability allows small teams to start without expertise in AWS. AWS
Lambda allows you to calculate the cost per customer in an easy way
and to understand your startup cost per user.
Serverless deep learning is extremely convenient if you already have
an existing infrastructure and want to optimize your costs. Serverless
architecture will be a lot simpler and cheaper than the cluster one and
it will be easier to maintain. Significantly, it reduces costs since you
don't need to retain the unused servers.
Serverless deep learning would be extremely useful for cases where
you have extremely high loads. For example, a lot of companies
struggle to maintain the system in cases where there are 1 million
requests during the first minute and zero requests in the next minute.
The cluster will either be too large or it will take a certain time to
scale. Serverless, on the other hand, has unmatched scalability, which
allows the system to work on high load without rolling.
Where serverless deep learning
doesn't work?
Deep learning will not work in the following situations:
These use cases mentioned above have shown us the landscape for the uses
of serverless learning and it will help us to make a decision as to whether to
use it or not. Finally, in a lot of cases, there isn't a definitive answer and it
makes sense to continue testing your model on serverless.
Code: This is what you want to run within the function. The code needs
to have an explicit declaration of the function, which you want for the
service to run.
Libraries: These enable us to run more complicated processes. You
will need to keep them inside the same package as the code itself.
Configurations: These are various parameters that dictate how Lambda
works.
There are a lot of different triggers, which means you can bind Lambda
with a lot of different services.
Why deep learning on AWS
Lambda?
Here, you will see the advantages of AWS Lambda :
Coding on an AWS Lambda is very easy. You will just need the
package code and libraries, not the Docker containers. It enables you
to start early and deploy the same code, which you would run locally.
This is therefore perfect for early stage projects.
AWS Lambda is extremely scalable and, more importantly, you don't
have to manage the scalability or write separate logic for it because
your data science application will be able to easily process a large
number of tasks or work with multiple users.
AWS Lambda is priced conveniently. You only need to pay for what
you're actually using and the price itself is very affordable. For
example, for the image recognition model, the cost will be $1 for
20,000 to 30,000 runs.
In the next section you will know the difference between traditional and
serverless architecture using Lamda.
Traditional versus Serverless
architecture using Lambda
Let's look at the difference between traditional and serverless architecture.
The following diagram represents the deep learning API through traditional
architecture:
In the above traditional architecture, you will not only have to handle the
cluster itself, but you will also have to handle all the balancing of API
requests. Also, you have to manage the Docker container with the code and
libraries and find a way to deploy it using the container registry.
In the preceding diagram, you can see that it looks a lot easier than
traditional architecture. you don't need to manage node balance scalability or
containers—you just need to put in your coated libraries and Lambda
manages everything else. Also, you can make a number of different
prototypes with it and you will only need to pay for invocations. This makes
Lambda the perfect way for making your deep learning model available to
users. In the next section, you briefly be introduced to the projects that you
will develop in this book.
Sample projects
In this section, you will cover projects, which you will develop during the
course of this book. you will create three projects:
In the next chapter, you will learn how to work with AWS Lambda and its
deployment.
Starting Deployment with AWS
Lambda Functions
In this chapter, we will understand more about AWS Lambda and learn how
it works. We will also discuss the different ways of deploying Lambda and
the things we need to consider while developing applications for AWS
Lambda.
AWS subscription
Python 3.6
AWS CLI
Serverless framework
You can find all the codes at https://github.com/PacktPublishing/Hands-On-Ser
verless-Deep-Learning-with-TensorFlow-and-AWS-Lambda
AWS Lambda functions
Let's go through the pre-requisites of AWS Lambda as shown here:
import sys, os
import mylib
...
def handler(event, context):
...
print("Hello world log")
return result
Pros: AWS Lambda is very easy to deploy. We don't have to know the
Docker or other container frameworks to manage it. It should only
contain libraries and code. AWS Lambda easily connects to triggers,
which means that we can easily integrate it with various other
infrastructures and it is very scalable as well, which makes it extremely
useful for production deployment. It is relatively inexpensive too.
Cons: It may be difficult hard to debug in the various Lambdas locally.
You have to recreate the entire environment and libraries and also be
able to check your memory consumption during timeout. Making good
estimations of how fast you will be able to produce high peak loads is
quite difficult with AWS Lambda. AWS Lambda is stateless and it
greatly effects how you organize your application. This means that the
stage should come from the trigger.
Limitations: There are certain limitations with RAM, disk, CPU, and
timeout which you have to consider during development.
The following diagram describes the pros, cons, and limitations in detail:
Now that we have a fair idea with the pros, cons, and limitations of AWS
Lambda, let us have a look at the basics of creating an AWS account.
Getting started with AWS account
In this section, you will learn about the AWS free tier and the IAM. We
need to have an AWS account to learn in depth.
If you do not have an AWS account, you can sign up here: https://aws.amazon.com/account/.
The Lambda API Gateway, the simple queue service, and functions are a
part of the AWS Free Tier, so you won't be charged for a small amount of
consumption.
AWS Free Tier
The Free Tier allows you to have free usage of AWS services. It covers a
number of different AWS services like:
AWS management control access: This allows you to use AWS web
services. The user needs to have a login ID and password, which the user
will use online.
Programmatic access: This allows you to use an AWS software
development key. The user needs two keys: the access key ID and a secret
access key.
Now, let's a create a user with programmatic access. It will allow us to use a
serverless framework, which will manage the deployment and orchestration of
AWS Lambdas.
1. First, you need to go to the AWS dashboard and choose the IAM service, as
shown in the following screenshot:
2. Next, go to the Users page and click on Add user, as shown here:
3. Now, set up the name, lambda, and select the Programmatic access checkbox:
4. Next, you will need to set permissions for the user to use the AWS services
discussed earlier. The easiest way to do that is by choosing Attach existing
policies directly and then choosing the AdministratorAccess policy, as
shown in the following screenshot:
5. Finally, you need to confirm all the choices:
6. It will show us the access key ID and secret access key. You can either copy
them from the web service or save them through the CSV file. You will need
these access keys later while setting up the serverless framework, as shown
here:
In the next section, we will create a hello word AWS Lambda function.
Creating a Hello World AWS
Lambda function
There are three primary ways to create a Hello World an AWS Lambda
from the AWS console:
You can use the inline editor in AWS Service Cloud 9. It can be very
helpful in cases where you don't want to install anything locally and
want to keep all your files in the cloud. The drawback is that it can be
pretty challenging to automate deployment with the inline editor, as
well as handling complex side libraries and large projects with
multiple files.
You can upload a ZIP file with libraries and code, either through the
web interface or through the command line. This is a very
straightforward process, which you can easily automate. The main
drawback of this approach is that the package has a 50 MB limitation
on size.
The best and the most popular way to upload the package is through
the S3 bucket. It doesn't have a 50 MB limitation for the package,
although, a 250 MB limit for our ZIP libraries and the code will still be
in place.
AWS Lambda code
Perform the following steps to create the Hello World code:
1. Log in with your lambda IAM user. Go to the AWS console and choose
the AWS Lambda service:
7. Edit the code and save it and you'll see that a Lambda has been
updated:
In the next section, we will learn about the serverless framework.
Introduction to the serverless
framework
In the previous sections, we learned about the different ways to deploy the
Lambda function. Although the Lambda package is just ZIP files with
libraries and codes, there are ways to deploy them as mentioned below. A
production usage deployment needs to have the following features:
We have code and libraries for a Lambda execution. These are the
same libraries and code that you would see if you uploaded your
package directly as a ZIP. In that sense, it is very easy to start
deploying existing Lambdas through the serverless framework.
We have a configuration file, which basically contains all the
information about how a Lambda will be deployed.
Configuration file
The Lambda configuration file consists of the same parameters, which we
have discussed in an earlier section (for example, name, runtime, memory,
and timeout). There are three primary differences.
You are not only setting up triggers but also setting up services, in which
the triggers depend on. You can set up specific roles and access. Some
accesses will be automatically set, but some will need to be configured by
you. Finally, you can set up additional plugins, which you will use during
deployment. We will look at them in more detail when we deploy the step
functions.
Deployment process
This section covers the process of deployment, which will be generated in
the custom cloud formation file based on our configuration file. Cloud
formation is an AWS service, which allows us to automatically deploy
multiple services. This is extremely convenient for handling the deployment
process, but its notation can be pretty difficult to start with. That is why we
are using the server's configuration file, which is more readable. Next, the
service framework packages libraries and code in a single package and
uploads which will then run the AWS cloud formation service for
deployment by using the file generated previously.
There are two files: index.py and serverless.yml. Let's look at index.py. The
following code will print the input event and return the Hello World message,
as shown in the following snippet:
def handler(event,context):
print('Log event',event)
return 'Hello world'
provider:
name: aws
region: us-east-1
runtime: python3.6
memorySize: 128
timeout: 10
functions:
main:
handler: index.handler
In the serverless.yml version, there is the name of the function, the available
resources, and the region. helloworld is the name of the service, which we
deploy. main is the name of the function and index.handler is the name of
the index.py file and the name of the function inside of their file handler.
One service may have different functions and that is why they are separated.
Before deploying them, we can run a Lambda locally. This is one of the
good features of the serverless framework, but it may not work with
complex libraries because of the differences in OS. We can see that we
printed an empty log and received the Hello World message as output:
Before deploying Lambda, you need to link your AWS account to serverless using
following command:
serverless config credentials --provider aws --key "Your access key" --secret "Your secret
access key"
You will deploy a Lambda using the serverless framework. Also, even if you
have a simple program, it may take some time for the serverless framework
to deploy the service:
Now, you can run the Lambda code and check the output:
As you can see, it is the same as the one we received earlier. There are two
great flags, which really helps when working with a Lambda. The first flag
helps to send custom events inside a Lambda, thereby, emulating triggers
from the custom service and the log allows you to see the log of the current
execution. This flag log will allow us to look at the log, and the data flag will
allow us to send a custom message.
Summary
In this chapter, we learned about AWS Lambda functions, along with
getting started with AWS account. We also learned about creating a Hello
World AWS Lambda function, along with an introduction to the serverless
framework and deployment of AWS Lambda functions.
First of all, there are deterministic algorithms that are very transparent
and predictable, but it may be very difficult to build a custom algorithm
for complex tasks, which will work in all cases.
Next, there's the machine learning technique, where we train the model
based on features we get from data. We don't need a lot of data to train
models in a reliable way, but we need to make a separate process for
training validation and testing.
Finally, there's the deep learning approach, where we train our own
neural networks. The main advantage of this is that we can use raw data
without predefined features. The downside is that we need a lot of data
and a lot of computing resources for training.
The machine learning approach varies greatly from the classic approach. The
classic approach uses rules and data as input and answers as output. In the
machine learning approach, we use data and answers as input and we
produce rules as output, as shown here:
Let's look into why neural networks have become so popular in recent years.
Why neural networks?
The reasons why neural networks have become popular in recent years are
as follows:
There are two instances where using pre-trained neural networks can be
extremely convenient:
The first case is when your task has already been solved. For example,
if you want to conduct image captioning with X classification, then
you can use already existing neural networks.
The second case is when your task is fairly different from what has
been done but it's close. Then, you can use pre-trained models to
generate features that you can use later with deterministic or simple
machine learning models.
Each image is labeled based on the digit that's written on the image. The
task, in this case, is to predict the label based on the image. This kind of
task is very difficult to implement using deterministic methods; as you can
see in the preceding image, there are a lot of different ways of writing the
same number. Therefore, you can't use a single template for prediction.
Training for MNIST
In this section, we'll discuss model training for MNIST:
1. First, we start with importing the tensorflow library. For this example,
we'll use the Keras deep learning framework, which makes it easy to
set up layers for the neural network. In simple terms, Keras acts as a
wrapper on top of TensorFlow, so everything is still based on
TensorFlow.
2. Next, we need to load data and present it in a binary format since the
original image pixel values are 0 and 255. We'll also divide the dataset
into training and test sets. This will allow us to measure the
performance of the neural network. A good practice for the machine
learning approach is to train the model on the training dataset and
measure the final score on the test dataset. It enables us to be sure that
the model doesn't see data points on which it'll be measured after
training. We'll see the explanation as follows:
import tensorflow as tf
mnist = tf.keras.datasets.mnist
3. Now, we need to set up the layers for our neural network. Basically,
each layer consists of a number of neurons and an activation
function. In this case, the first layer tries to get more useful data out of
the raw data. The second layer tries to use this data to assign the
probability of the image being one of 10 digits.
4. As part of the model, you need to choose three parameters for the
training process:
First is the loss function, which the network will use to optimize
its performance. The training process basically consists of
decreasing the value of the loss function and trying to find
weights for the neural network, so the loss function will be
minimal.
Next is the optimizer, which handles how the neural network will
iterate towards the most optimal solution, and how it'll change
weights after each iteration.
Finally, metrics allows us to measure neural network performance
over the dataset. For example, accuracy allows us to understand
which part of the dataset was correctly classified. This metric
doesn't participate in the training process directly and mainly
allows us to understand whether network performance has
improved or not. We can understand the preceding explanation
from the following code:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
5. Once everything is set up, we can just run training on the training part
of our dataset. It may take several minutes, depending on the
configurations of your computer. After that, we can evaluate model
performance and the test set. Our model will produce something
around 97% accuracy/test set, which is very impressive, and as
demonstrated, it can be achieved with even a simple neural network as
shown in the code below:
model.fit(x_train, y_train, epochs=2)
print('Evaluation:')
print(model.evaluate(x_test, y_test))
6. Finally, once the neural network has been trained, we can save it so
that we can use it later. As you can see, it's a pretty simple process.
The model file will contain the model architecture, a key composition
of layers, the layer's weights and training configuration, and the
optimizer state, which allows us to continue training on the already
trained model:
model.save('MNISTmodel.h5')
modelImported = tf.keras.models.load_model('MNISTmodel.h5')
print('Evaluation by imported model:')
print(modelImported.evaluate(x_test, y_test))
Let's discuss the available files. There's only one Python file, the testMNIST.py
file, which we're about to run. In this file, we can see the parts that we've
discussed, which include data transformation, model installation, model
training, model evaluation, model export, and model import.
Now, let's run the testMNIST.py file in the command line to see the results. By
running the code, we can see the process of training, which happens in
epochs. This kind of neural network doesn't require a GPU to train and we
can achieve very good results, even on the CPU:
python testMNIST.py
As you can see in the following screenshot, we achieved 97% accuracy with
just two epochs and were able to successfully export and import the model.
We can see the exported retrained model, which can now be used in
different code:
.In the next section, we talk about the repositories for pre-trained
TensorFlow models.
Repositories for pre-trained
TensorFlow models
The pre-trained models are pretty skilled when it comes to import and
export. In a nutshell, declaring deployment consists of importing the trained
model and transforming input data into the format that's acceptable by the
neural network. There are certain things that you need to keep in mind
during deployment:
We'll look into what we'll need to start using Inception v3 in our code. The
model consists of layers and weight values present
in classify_image_graph_def.pb.
Here's an example of the Panda image. First, we receive the IDs that score.
The highest score means that the model has high confidence in the image
having this label. After mapping IDs to label names, we can see that the
model correctly detected Panda. The following screenshot explains this:
TensorFlow code for Inception v3
Now, let's see how the code for any Inception v3 model will look like:
node_loolup.id_to_string(predictions)
Running the code
Let's look at the available files, inputimage.png and testinception.py, which we're
about to run. In this example, we'll be using the Panda image (inputimage.png).
1. As shown in the following code, there's the NodeLookup class, which will
help us to translate responses from the model to the label name:
class NodeLookup(object):
"""Converts integer node ID's to human readable labels."""
3. Then, this is the code that tells how we import the pre-trained model:
with tf.gfile.FastGFile('classify_image_graph_def.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
predictions = SESSION.run(softmax_tensor,{'DecodeJpeg/contents:0':
image_data})
top_k = predictions.argsort()[-5:][::-1]
strResult = '%s (score = %.5f)' % (node_lookup.id_to_string(top_k[0]),
predictions[top_k[0]])
print()
for node_id in top_k:
human_string = node_lookup.id_to_string(node_id)
score = predictions[node_id]
print('%s - %s (score = %.5f)' % (node_id, human_string, score))
6. Now, we can run the code and see the response. As you can see from
the following output, the model successfully detected a panda on the
image. The code will run fast since there's no training involved. You
can try the code on different images and get a sense of the possibilities
of this model:
Summary
In this chapter, we studied different approaches for building algorithms. We
discussed how to train TensorFlow models and repositories for pre-trained
Tenserflow models. We also learned about image captioning using an
Inception v3 TensorFlow example.
In the next chapter, we'll learn how to work with TensorFlow AWS
Lambda, where we'll learn more about using TensorFlow models with AWS
Lambda.
Working with TensorFlow on AWS
Lambda
In this chapter, we will learn about the architecture of deploying
TensorFlow with AWS, and we will deploy TensorFlow on AWS Lambda
using the pre-existing pack and the serverless framework.We will also look
into the various general issues with deploying the various Python
frameworks on AWS Lambda and then cover all of the solutions to the
same issues.
Keep the model within the deployment package alongside the code and
libraries
Keep the model on the S3 bucket and unload it in AWS Lambda
during execution
Keep the model on the FTP or HTTP server and unload it into AWS
Lambda during execution
Model within the deployment
package
This option means that the model is within the deployment package. The
code will import it from a local filesystem. This option has its own pros and
cons.
Pros
The advantages of the model within the deployment package are as follows:
We will get a very good start speed for our deployment since there is
no overhead on the loading model
We will have a single package to start with
We won't need any outside servers or AWS services as part of our
deployment
Cons
The disadvantages of the model within the deployment package are as
follows:
We will get a slower start than in previous cases, since Lambda will
need to download the model first
It should be noted that, though it happens only during cold start, during
warm start, the model will already be in memory
You will need to make the S3 bucket upload all of your models as a
part of your deployment, and add logic for managing the different
models within the code
Model on the HTTP/FTP server
This option is mostly useful for the case where you want to limit the use of
AWS services, memory or integrate with services outside of AWS. The
AWS Lambda downloads the model from HTTP or FTP server during
deployment.
Pros
The advantages of the model on the HTTP/FTP server are as follows:
TensorFlow
NumPy
These libraries are used for matrix calculations. As you may know, the
libraries by themselves are pretty big and they just wouldn't work on AWS
Lambda. As you have already seen in the previous section on deployment
that when we deploy them through S3, we don't have this limitation, and we
only have 250 MB limitation for the unzipped package. In this case to make
it work, we need to reduce the size of the package.
Solutions for issues with deploying
Python frameworks on AWS
Lambda
There are a number of ways as to how we can reduce the package size. Here
are the solutions for the issues in question:
We can compress the shared libraries; this usually enables us to get the
best reduction of size.
We can remove the .pyc files as they do not influence the library work.
Next, we can remove tests and visualization folders from the libraries as
they are not useful in production.
Next, we can remove libraries that already exist on AWS Lambda.
Finally, we can check and remove the libraries that aren't used during
execution, for example, wheel or PIP libraries.
Now, in the following code, there is the part that finds and compresses all
shared libraries. Then, we find and delete all .pyc files.
Next, we need to delete libraries that won't be used during execution, such
as .pip and wheel. Finally, we can also delete some folders from TensorFlow
library.
The following screenshot shows the different commands for the preceding
explanation:
The whole process of preparing a package for AWS Lambda can be done
through Docker. You don't need to use it for the project we will create, but it
is good to keep in mind how to prepare this kind of package.
To install Docker, you just need to run three comments in your comment
line:
1. You need to get the latest Amazon Linux image on which we will run
the script.
2. You need to start a Docker container with the managing output folder
inside the container.
3. You can run the script inside the container and it will assemble the
package for you. The following screenshot displays all of the
commands to install Docker:
Deploying TensorFlow on AWS
Lambda using the pre-existing pack
In this section, we will learn to deploy TensorFlow on AWS Lambda using
the pre-existing pack. In the project files, we have model files that are also
known as the model itself and files that enable us to translate model response
through labels. In Inception folder and Lambda package, which is also known
as the code and libraries in lambdapack folder.
We'll create S3 bucket where we will keep the model and upload the
model itself
Then,we'll modify code for the specific bucket and add the created
bucket name
Lastly, we can package it and upload to add the AWS Lambda
Now, we will create S3 bucket using the AWS Console and upload files
there. We will open the code and add the bucket that we have just created.
Then, let's package it and upload it to add AWS Lambda.
3. Once we have the bucket in place, we can upload files there. You just
need to click Upload and then choose files. So, here we just upload the
package with libraries, and it will start the upload process, along with
the package itself. We will need to upload model files, which are
present in the Inception folder:
4. You can see that now we have a package inside our S3 bucket:
5. Now, we have to create the role for our AWS Lambda, which we can do
from the IAM service:
6. We need to choose Lambda and click on Next: Permissions, which lies
at the bottom-right of your screen:
11. On the same screen, scroll down and, in the Basic setting tab, add
enough resources as presented in the following screenshot:
12. Pass the link with the URL of our package (S3 bucekt) and click on
Save in the top right corner:
13. You can see the function is created. To test the function, click on the
Test on the top- right corner:
14. After the function is tested, it will successfully produce the following
result:
Deploying TensorFlow using a
serverless framework
First, we will look at the project files. We have model files in the Inception
folder, and Lambda code with Serverless.yml, the configuration file in
the Lambdapack folder.
The flow of deployment will be the same as in the previous section. One of
the main differences will be that, instead of providing an AWS Lambda
admin role, we will provide access to bucket by serverless CML file. The
only thing we need to add is bucketname, and run the properties of access as
shown:
We will need to create an S3 bucket, upload files there, and then deploy
AWS Lambda. We will create an S3 bucket and upload files from the
command line: aws s3 sync.s3://<bucket>/.
Creating a bucket
We will first need to create a bucket, then we will need to upload model
files to the bucket, run serverless, and start the AWS Lambda.
Index.py
Let's look at the available files. We will look at the index.py file as shown:
import boto3
import numpy as np
import tensorflow as tf
import os.path
import re
from urllib.request import urlretrieve
import json
SESSION = None
strBucket = 'serverlessdeeplearning'
def handler(event, context):
global strBucket
if not os.path.exists('/tmp/imagenet/'):
os.makedirs('/tmp/imagenet/')
strFile = '/tmp/imagenet/inputimage.jpg'
The main difference is that we run the code inside the handler function and
we need to download the model files and image file from the S3 bucket:
if not os.path.exists('/tmp/imagenet/'):
os.makedirs('/tmp/imagenet/')
strFile = '/tmp/imagenet/inputimage.jpg'
downloadFromS3(strBucket,'imagenet/inputimage.jpg',strFile)
global SESSION
if SESSION is None:
downloadFromS3(strBucket,'imagenet/imagenet_2012_challenge_label_map_proto.pbtxt',
'/tmp/imagenet/imagenet_2012_challenge_label_map_proto.pbtxt')
downloadFromS3(strBucket,'imagenet/imagenet_synset_to_human_label_map.txt','/tmp/i
magenet/imagenet_synset_to_human_label_map.txt')
image = os.path.join('/tmp/imagenet/', 'inputimage.jpg')
strResult = run_inference_on_image(image)
return strResult
def run_inference_on_image(image):
Also, we can use one of the advantages of AWS Lambda. We can save
model files as global variables. Basically, we can define session as a global
variable. With these, if we start Lambda right after the previous Lambda
was executed, all model files will be in the RAM memory:
global SESSION
if SESSION is None:
downloadFromS3(strBucket,'imagenet/imagenet_2012_challenge_label_map_proto.pbtxt',
'/tmp/imagenet/imagenet_2012_challenge_label_map_proto.pbtxt')
downloadFromS3(strBucket,'imagenet/imagenet_synset_to_human_label_map.txt','/tmp/i
magenet/imagenet_synset_to_human_label_map.txt')
image = os.path.join('/tmp/imagenet/', 'inputimage.jpg')
strResult = run_inference_on_image(image)
return strResult
def run_inference_on_image(image):
image_data = tf.gfile.FastGFile(image, 'rb').read()
global SESSION
if SESSION is None:
SESSION = tf.InteractiveSession()
create_graph()
Serverless.yml
In the Serverless.yml file, we need to define access to the S3 bucket as that's
where we will keep our model. Other than that, it will look exactly as the
previously mentioned serverless CML files for other Lambdas:
service: deeplearninglambda
frameworkVersion: ">=1.2.0 <2.0.0"
provider:
name: aws
region: us-east-1
runtime: python3.6
memorySize: 1536
timeout: 60
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:ListBucket"
Resource:
- arn:aws:s3:::serverlessdeeplearning
- Effect: "Allow"
Action:
- "s3:GetObject"
Resource:
- arn:aws:s3:::serverlessdeeplearning/*
functions:
main:
handler: index.handler
There are two very convenient commands; one allows us to create a bucket,
and another allows us to easily upload files into the bucket:
Since we already have model files in this bucket, there is no need to hold it
now, but you can use this command to upload to your bucket. Next, we can
return back to the folder with our function and run serverless deploy
command.
Now, we will invoke the function with the following command:
serverless invoke --function main
As you can see, it successfully recognized the image. Also, if we invoke the
function one more time after that, it will work faster:
Summary
In this chapter, we learned about the architecture of the deploying
TensorFlow with AWS Lambda, in which we covered the possible options
of deploying TensorFlow with AWS Lambda along with each of its pros
and cons. We also discussed the general issues with deploying Python
frameworks in AWS Lambda along with its solutions. Lastly, we deployed
TensorFlow on AWS Lambda using the pre-existing pack and using a
serverless framework.
In the next chapter, we'll create deep learning API using AWS Lambda.
Creating the Deep Learning API
In the previous chapter we learned about working with TensorFlow on
AWS Lambda. This chapter briefs about the RESTful API along with the
AWS API Gateway. We will learn how to create the API Gateway using the
serverless framework.
API service
The AWS API Gateway
Creating a deep learning API project
Technical Requirements
The technical requirements in this chapter are as follows:
AWS subscription
Python 3.6
AWS CLI
Serverless framework
You can find all the codes at: https://github.com/PacktPublishing/Hands-On-Se
rverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda
RESTful API
The RESTful API has become extremely popular in recent years, with the
rising popularity of macro services as a universal way to communicate
between different services inside an application—for example, we can use
RESTful API services for different programming languages and different
hosting platforms together in a single application.
Easy to scale: The API service takes care of scaling, which means we
do not have to worry as to whether this is a good way to handle peak
loads, and whether it will over-provision resources.
Traffic management: The API Gateway has a way to control the
traffic. The main use case in traffic management is throttling when we
want to have a more uniform load on the backend. This is not an issue
with AWS Lambda, as it can scale almost indefinitely.
Authorization: This allows you to control user access to the API. This
means that we can either keep the API private or commercialize it.
This feature is essential for your application.
Monitoring: This feature enables us to get usage statistics at the user
level. This is important if we want to limit users based on their usage,
and if we need to troubleshoot our API.
Caching: This allows you to reduce the amount of requests, which go
to the backend by caching some of the responses. If your application
has to deal with the load of repetitive requests, then the switch would
significantly reduce the amount of usage at the backend.
Version control: This allows you to manage multiple versions of the
API, which is very important for the production of the application.
As you can see, it produced a result that is similar to the demo code, as seen
in the following screenshot:
Now we are ready with the AWS Lambda and the API Gateway, we will be
creating the connection using the serverless framework.
Creating the API Gateway using
the serverless framework
Before creating the API, let us look at our project files: the main Python file
(index.py) and the serverless configuration file (serverless.yml). Let's look at
the Python file as shown in the following code:
import json
def handler(event, context):
print('Log event', event)
return{
'status code': 200;
'body': json.dump('Hello world API!')
}
From the preceding code block, you can see that this file will be returning a
JSON response instead of the string to the API request. For this, we need to
change status code to 200 and change body to transform in the JSON format.
On successful completion of the deployment, it will return the URL for the
API Gateway. Now test the URL we got on the command line using the curl
command and URL:
We can also run the URL in the browser and test the deployment to find the
desired output.
Creating a first API project
Now let us start by creating the example of the deep learning API project.
Before we start the project, let's look at our files: the main Python file and
the service configuration file, along with some libraries files and the
inception model.
In this project, we will be adding one events section and the endpoint name.
In the Python file, we will be returning a JSON response to a request. Also,
we will make Lambda accept the link to the image in the request and then
apply it to a module. The we will be deploying the server and testing the
files in the command line and browser, as we checked out in the previous
section.
Now let's deploy the service using the serverless deploy command, which will
provide you with the URL of the API Gateway. Let's test this URL using the
curl command and from the browser and you will find the same response that
we had in the previous section:
We can also test the deployment by downloading the image. To test this, we
only have to add the URL of the image as a parameter to the API Gateway
URL.
Summary
In this chapter, we learned about the API Gateway services and the
RESTful API. We saw how the AWS Gateway is cost effective for our
application. Then we looked at how to create the AWS API Gateway and
AWS Lambda using the AWS console. We also created a connection
between AWS Lambda and the AWS API Gateway using the serverless
framework. Finally, we created the deep learning API project using the API
Gateway.
Message queue
Introducing AWS simple query service
Creating AWS SQS using AWS console and serverless framework
Sample projects—deep learning pipeline
Technical Requirements
The technical requirements in this chapter are as follows:
AWS subscription
Python 3.6
AWS CLI
Serverless framework
You can find all the codes at https://github.com/PacktPublishing/Hands-On-Ser
verless-Deep-Learning-with-TensorFlow-and-AWS-Lambda
Message queue
The message queue is an important additional method of interaction within
different services. While the RESTful API has a timeout limit, the message
queue doesn't have this kind of drawback. Therefore, it helps to handle
long-running processes or delayed messages. Also, it allows a more
uniform load on the backend. It does not have a critical feature for working
with AWS Lambda, since Lambda can easily scale, but it can be very useful
when dealing with clusters. Finally, the message queue allows retry logic,
meaning that failed tasks can be sent back multiple times. Now let's look
more into AWS SQS.
Introduction to AWS SQS
Basically, this is an AWS service that allows the sending, receipt, and
storage of messages. It can be connected to any processing backend. It has a
pay-as-you-go system, which makes it very convenient as a starting point.
AWS API gateway features
The different features of AWS API are as follows:
1. First, add the name under Name, choose Runtime as Python 3.6,
set Role as Choose an existing role, then from Existing role
choose lambda1, and click on Create function, as shown here:
5. From the Designer tab on the left, choose SQS as the trigger:
6. We will send some messages to the queue by selecting Send a Message
from the Queue Actions drop-down list:
7. As you can see, we have only one message available, meaning they all
were consumed by Lambda as shown in the following screenshot:
8. By clicking Monitoring, we get a detailed overview as shown:
The next section concerns the creation of an AWS SQS connection to AWS
Lambda using the serverless framework.
Creating an AWS SQS connection
using the serverless framework
To create the SQS connection, we have the main Python file and the
serverless configuration file. The configuration file will be a little complex.
We will need to define SQS in the resources section, edit as event source for
Lambda, and then enable Lambda to read from SQS.
The Python file
The main difference for the Python file will be that, instead of returning a
string, we will write to another SQS query, as shown:
Code
Before starting with the code, we will first need to deploy the serverless
framework, and then we need to check that it runs using the command-line
interface. We will run the code using the index.py file and the
serverless.yml configuration file.
serverless.yml
In the serverless.yml file, we can see all the sections that we discussed in the
previous chapters, especially the part where we define the role for accessing
the query from where we will read messages, and to which our Lambda will
write. The following code shows the explanation:
Effect: "Allow"
Action:
"sqs:ReceiveMessage"
"sqs:DeleteMessage"
"sqs:GetQueueAttributes"
Resource:
arn:aws:sqs:#{AWS::Region}:#{AWS::AccountId}:ReadSQS
We also have to define that one of the queries will act as an event source, as
shown:
events:
sqs:
arn:
Fn::GetAtt:
ReadSQS
Arn
Finally, we define the queries, which we can perform in the resource section
as shown in the code:
resources:
Resources:
WriteSQS:
Type: AWS::SQS::Queue
Properties:
QueueName: "WriteSQS"
ReadSQS:
Type: AWS::SQS::Queue
Properties:
QueueName: "ReadSQS"
Next is to use this plugin to access the ID of the region that we use and our
account ID as shown:
Resource:
- arn:aws:sqs:#{AWS::Region}:#{AWS::AccountId}:ReadSQS
Effect: "Allow"
Action:
- "sqs:SendMessage"
- "sqs:GetQueueUrl"
Resource:
- arn:aws:sqs:#{AWS::Region}:#{AWS::AccountId}:WriteSQS
You can access the account ID and the ID of a region without this plugin and just
manually find it as well.
index.py
The index.py file is very simple. We just read incoming messages and then
write them as SQS. The following shows the code for index.py:
import boto3
def handler(event,context):
for message in event['Records']:
client = boto3.client('sqs')
sqsAddress = client.get_queue_url(QueueName='WriteSQS')
response = client.send_message(QueueUrl=sqsAddress['QueueUrl'],
MessageBody=message['body'])
return
We will see the preceding index.py and serverless.yml files in the command
line:
As you can see, the plugin replaced the region with the actual base region
and account as shown in the following screenshot:
To send a message through the queue, first we need to find the queue URL
using the following command:
aws sqs get-queue-url --queue-name ReadSQS
With the queue URL, we can send messages. We see the command executed
successfully here:
Now we can read the same message from the SQS queue. Basically, here we
can check whether Lambda has received the message that we have sent,
which is Hello world1, and send it to write SQL. We see that Lambda works
successfully and we can see the resulting message, Hello world1, in the
following screenshot:
Example project – deep learning
pipeline
In the project files, we have the main Python file, the serverless
configuration file, libraries files, and an inception module. The
configuration file will be the same as the one we used in the previous
chapter. We will look at the Python file. The main difference for our Python
file will be that, instead of returning the string, we will send a message to
another SQS query. Also, we will make Lambda accept the link to the
image in the message, and then apply module to structure it. The
deployment will be similar to the one in the previous section.
We will skip the deployment of the model, since we have covered it before.
Code
First, we will need to deploy the serverless framework, and then we can
check that it runs using the command-line interface. We have the index.py
file and the serverless.yml configuration file. We also have libraries for
TensorFlow and pre-installed plugins for the serverless framework.
Configuration file - serverless.yml
We can see that the current configuration file is taken from the preceding
sections.
The following screenshot shows the part where we retrieve the image and
run our model on it, and hence, we write the result of the model to another
queue, as shown:
if ('Records' in event):
for message in event['Records']:
urlretrieve(message['body'].strip('\''), strFile)
vecResult = run_inference_on_image(strFile)
client = boto3.client('sqs')
sqsAddress = client.get_queue_url(QueueName='DLWriteSQS')
response = client.send_message(QueueUrl=sqsAddress['QueueUrl'],
MessageBody=vecResult[0])
else:
downloadFromS3(strBucket,'imagenet/inputimage.png',strFile)
strResult = run_inference_on_image(strFile)
We can read the sent message from another queue as shown here:
Summary
In this chapter, we were introduced to AWS SQS, which included the
features as well as a look at its pricing. We also created AWS SQS
connection using both the AWS console and the serverless framework.
AWS subscription
Python 3.6
AWS CLI
Serverless framework
You can find all the code at https://github.com/PacktPublishing/Hands-On-Serv
erless-Deep-Learning-with-TensorFlow-and-AWS-Lambda
Introduction to the AWS Step
Functions service
In this section, we will cover the AWS Step Functions service, including its
features and also the pricing for using this service.
Processing workflow
The processing workflow is an additional method for interaction between
the different services. If you want to build a multi-step process in deep
learning, it could mean loading the data, then pre-processing data and
running the model on it. While you can put queries between each of these
backend nodes, it would be very hard to monitor the processing for a single
task. This is where the workflow can be extremely convenient. Basically,
the workflow service takes care of the invocation of each node when
needed and handles the intermediate state of the processing job. It allows
you to have a high-level view of what is happening with each task, and
track task failures and timeouts. Finally, it allows you to have very flexible
component usage as part of the workflow.
AWS Step Functions
AWS Step Function is an AWS service that allows you to manage the
workflow as a state machine. It can be connected to any processing backend
and has a native integration with AWS Lambda. It has a pay-as-you-go
system, which makes it very convenient.
AWS step function features
We will now look at the specific features of step functions:
Step functions are extremely scalable and you don't have to manage
neither the scanning nor the workflow.
Step functions have a great visual UI, which enables easy monitoring
of the processing jobs.
State management allows you to add complex logic, such as choosing
the next node based on the results of previous nodes, and it also allows
for running several nodes in parallel.
Some functions have convenient logic for retrying tasks, which
enables you to ensure processing.
Built-in error handling allows you to handle cases. For example, when
you encounter some exception and you need a way to add logic for
error handling, such as marking a task as failed in the database.
Scheduling allows you to run delayed processing tasks, which is very
convenient for when you have to wait for another process to complete.
AWS Step Functions pricing
One of the main advantages of step functions is the pay-as-you go system.
It is very simple 25 cents per 10,000 requests, of which the first 4,000
requests are free per month. This makes it perfect for early projects.
Step functions versus SQS
If we dive deep into how the step functions are different from SQS in terms
of features and possible use cases, we can say that SQS is most suitable for
short, simple, low-priority tasks that can come in very large volumes
(millions of tasks per day). Step functions, on the other hand, are most
suitable for cases where we have complex multi-step processes, which can
take a lot of time and where we would like to guarantee delivery of every
task (thousands of tasks per day). In the next section, you will learn how to
create an AWS Step Functions connection to AWS Lamda using the AWS
Console.
Creating an AWS Step Functions
connection to AWS Lambda using
the AWS Console
In this section, we will create setp functions using the AWS Console. To
create a step function, there are two main steps:
7. For each execution, you can see the input and output of the whole step
function:
8. We can see the input and output of each node (this can be seen in the
Step details tab). If there is an error, we will be able to see it in
the Exception tab. Also, if you scroll down, you can also see the
timeline of execution:
9. We can also check logs by using the link present in the Resource tab
under the Step details tab by clicking on it. This will lead you to the
following page:
Therefore, we can see that step function is a very convenient service for
managing Lambda execution.
Creating AWS step functions for an
AWS Lambda instance using the
serverless framework
In this section, we'll create step functions using the serverless
framework. First, let's take a look at our project files: one Python file and
the serverless configuration file. In the configuration file, we will be adding
a few plugins and describing step functions, which is a complex thing.
So, let's explore the step functions in the serverless configuration file by
beginning with the live code:
Let's take a look at the configuration file and its two main parts here:
2. Next, we will add some plugins, which are required to work with step
functions:
plugins: - serverless-step-functions
- serverless-pseudo-parameters
3. Now let's take a look at the index.py file, where you can see all the parts
discussed so far:
def handlerMap(event,context):
return event
def handlerReduce(event,context):
return event
def handlerBranch(event,context):
return 'Hello world'
Serverless deployment
Now, let's deploy our service by executing the following command:
serverless deploy
As part of the deployment, it will give us the URL of the API Gateway, as
shown in the following screenshot:
Now let's test the URL in the command line:
curl https://dns1519ilj.execute-api.us-east-1.amazonaws.com/dev/startFunction
You can check with the results in your browser. In the following screenshot,
you can see our step function:
In the following screenshot, we can see that the latest execution went
successfully:
You can check the input and output of each node. If there are any errors, you
can check them in the Exception section.
Both branches returned hello world and the step node combines the result
and returns it as the result of the step function:
Here, let's check with the timeline of the execution. You can also see that the
branches have started almost at the same time.
The main difference with our Python file will be that we will have three
functions . One will map a list of URL links into separate things. Another
will run a model on the link. The final one will combine the results. The
structure of the deployment will be similar to the one in the previous
section. We will skip the deployment of the model to S3, since we covered
it in the previous chapter.
Creating the model
Here, we will need to deploy the serverless framework and then we can check
the trance using the CLI:
1. Let's look at the configuration file. You can see all the main parts and
roles that we need to define to access the model:
provider:
name: aws
region: us-east-1
runtime: python3.6
memorySize: 1536
timeout: 30
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:ListBucket"
Resource:
- arn:aws:s3:::serverlessdeeplearning
- Effect: "Allow"
Action:
- "s3:GetObject"
Resource:
- arn:aws:s3:::serverlessdeeplearning/*
2. Here are the functions, which we will use in different states of the step
function:
functions:
main:
handler: index.handler
map:
handler: index.map
reduce:
handler: index.reduce
3. map will enable the mapping of the incoming event into different Lambdas,
range will enable the processing of each link separately in parallel, and
reduce will help in combining them in one response. We have the state
machine definition, which is very similar to that previously discussed:
stepFunctions:
stateMachines:
DeepLearningWorkflow:
events:
- http:
path: gofunction
method: POST
name: DeepLearningWorkflow
definition:
StartAt: StartStepF
States:
StartStepF:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#
{AWS::AccountId}:function:${self:service}-${opt:stage}-map
Next: Parallel
Parallel:
Type: Parallel
Next: EndStepF
Branches:
- StartAt: Branch1
States:
Branch1:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#
{AWS::AccountId}:function:${self:service}-${opt:stage}-main
InputPath: '$.branch1.url'
ResultPath: '$.res'
End: True
- StartAt: Branch2
States:
Branch2:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#
{AWS::AccountId}:function:${self:service}-${opt:stage}-main
InputPath: '$.branch2.url'
ResultPath: '$.res'
End: True
- StartAt: Branch3
States:
Branch3:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#
{AWS::AccountId}:function:${self:service}-${opt:stage}-main
InputPath: '$.branch3.url'
ResultPath: '$.res'
End: True
EndStepF:
Type: Task
Resource: arn:aws:lambda:#{AWS::Region}:#
{AWS::AccountId}:function:${self:service}-${opt:stage}-reduce
End: true
5. The main difference in our index file from the previous section is that we
added the MapReduce logic. This will enable the processing of each URL
separately:
def map(event, context):
dictMap = {}
dictMap['branch1'] = {'url':event[0]}
dictMap['branch2'] = {'url':event[1]}
dictMap['branch3'] = {'url':event[2]}
return dictMap
6. Now we have our model ready, let's deploy the service using the
following command:
serverless deploy
8. Let's test this URL. We will need to use the curl command, which will be
a little different from its usages previously.
9. First of all, we will use POST requests instead of GET requests, and we will
provide a list of links as both of the request. As you can see, it returned
successfully. This execution indicates that it successfully sent the
command to the step function:
10. Now let's look at the graph in the browser. We can see that it has already
finished the execution:
11. We can see that each branch received a separate URL, and we can see that
the final note combined results from different branches as single, and that
it successfully recognized the images:
Also, we can check the timeline of the execution and see that almost all the
branches started at the same time.
This means that the parallelization really enabled us to process a list of links
faster.
Summary
In this chapter, we studied the AWS Step Functions service, including its
features and pricing. We also learned how to connect AWS Step
Functions to AWS Lambda using the AWS Console. We also saw how to
create a step function using the serverless framework and even created a
deep learning workflow.
Other Books You May Enjoy
If you enjoyed this book, you may be interested in these other books by
Packt:
ISBN: 9781786462169
ISBN: 9781789806199