Part II — Deploying a Keras Computer Vision Model to AWS using an AWS EC2 Instance (Web App & API)

Rajeev Ratan
10 min readApr 23, 2019

--

Learn how to turn your Computer Vision (CV) Deep Learning Keras or TensorFlow Model into API or Web App!

In this section you’ll learn:

- How to sign up for AWS

- Selecting the appropriate and launching an EC2 Instance

- Using FileZilla to transfer your code to your Instance

= Testing your Web App and API (using Postman).

In Part I, we created ran our Computer Vision API and Web App locally, now let’s run this on AWS’s EC2 Instance so that anyone with internet access can use our Web App and API.

Setting up AWS

Step 1: Go to https://aws.amazon.com/ and click on Complete Sign Up

Step 2: Create a New Account

Step 3: Enter your main account details

Step 4: Enter Contact Info

Step 5: Enter your verification code

Step 6: Select the Free Basic Plan

Step 7: Sign up is now complete! Sign in to your account now.

Your AWS Account is Complete! Let’s now create our EC2 Instance.

Launching Your E2C Instance

Step 1: Your landing page upon logging in should be the AWS Management Console (see below). Click on the area highlighted in yellow that says “Launch a virtual Machine” with EC2.

What is EC2? Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS) cloud. Using Amazon EC2 eliminates your need to invest in hardware up front, so you can develop and deploy applications faster.

.Step 2: As a Basic Plan (FREE) User, you’re eligible to launch and use any of the “Free tier” AMIs. The one we’ll be using in this project is the Ubuntu Server 18.04 LTS (HVM), SSD Volume Type. See image below, you’ll have to scroll down a bit to find it amongst the many AMIs. We use this image as it’s quite easy to get everything up and running.

Step 3: Choosing an Instance Type — We already have chosen the type of OS we want to use (Ubuntu 18.04), now we have to choose a hardware configuration. We aren’t given much of a choice due to our Free Tier limitations. The t2.micro will suffice for our application needs for now. Click Review and Launch (highlighted in yellow)

Step 4: Reviewing and launching. No need to change anything here, the defaults are fine. Click Launch (highlighted in yellow)

Step 5A: Creating a new Key Pair. If this is your first time you will need to create a new Key Pair. Click on the drop-down highlighted in yellow below.

Step 5B: GIve you key a name e.g. my_aws_key and Download Key Pair (please don’t lose this file otherwise you won’t be able to log in to your EC2 instance).

Step 5C: Your Instance has been launched, it will take a few mins for AWS to create and have your instance up and running

Step 6A: Clicking View instances brings up the EC2 Dashboard.

Step 6B: To view the details of the individual instance you just launched, click on Instances under Instances on the left panel.

Step 7: Viewing your Instance public IP. Note your IP as you’ll need it later when connecting to your instance.

Step 8: Assuming you’re using Ubuntu on the VM (if not it’s highly recommended as I find using Putty and SSH keys over windows to be sometimes problematic). Note Windows isn’t always, so you’re welcome to try using the guide here: https://docs.aws.amazon.com/codedeploy/latest/userguide/tutorials-windows-launch-instance.html)

Mac instructions would be the same as the Ubuntu instructions.

Let’s chmod your downloaded key to protect it from overwriting. If you’ve never used ssh on your VM, create a ./ssh folder in the home director. The following commands also assume your key is in the downloaded directory.

mkdir ${HOME}/.ssh
cp ~/Downloads/my_aws_key.pem ~/.ssh
chmod 400 ~/.ssh/my_aws_key.pem

Step 9: Connecting to your EC2 instance via Terminal, by running this:

ssh -i ~/.ssh/my_aws_key.pem ubuntu@18.224.96.38

You’ll be prompted by the following, type yes and hit Enter.

You’ll now be greeted by this! Congratulations, you’ve connected to your EC2 Instance successfully!

Step 10: Installing the necessary packages by executing the following commands:

sudo apt update
sudo apt install python3-pip
sudo pip3 install tensorflow
sudo pip3 install keras
sudo pip3 install flask

Note: As of April 2019, sudo pip3 install opencv-python appears to be broken, if importing cv2 fails, run sudo apt install python3-opencv

sudo pip3 install opencv-python
sudo apt install python3-opencv
TensorFlow install
Keras install
Flask install

We’ve successfully installed all libraries needed for our API and Web App on our EC2 Instance!

In the next steps we’ll:

  • Setup Filezilla to transfer our code to your EC2
  • Setup our Security Group on AWS to allow TCP traffic.

Using FileZilla to Connect to Your EC2 Instance to Allow Transferring of Files

Step 1: Install FileZilla

  • Windows and Mac Users can go here and download and install — https://filezilla-project.org/
  • Ubuntu users can open the Terminal and install as follows:

sudo apt-get update sudo apt-get install filezilla

After installing you can find and launch FileZilla by simply searching for it:

Step 2: Let’s load our SSH Key into FileZilla. Open FileZilla, and click Preferences in the main toolbar (should be the same for Windows and Mac users)

Step 3A: In the left panel, select SFTP and then click Add key file…

Step 3B: To find your ssh key, navigate to your “./ssh” directory, as it is a hidden directory, in Ubuntu, you can hit CTRL+L which changes the Location bar to an address bar. Simply type “.ssh/” as shown below to find your ssh key directory.

Step 4A: Connecting to your EC2. Now that you’ve added your key we can create a new connection.

On the main toolbar again, click File and Site Manager

Step 4B: This brings up the Site Manager window. Click New Site as shown below

Step 4C: Enter the IP (IPv4 Public IP) of your EC2 Instance (can be found under Instances in your AWS Console Manager).

Step 4D: Change the Protocol to SFTP

Step 4E: Change the Logon Type to Normal

Step 4F: Put the Username as ubuntu. Leave the password field alone.

Step 4G: Check the “Always trust this host, add the key to the cache box” and click on OK.

Step 5: We’ve Connected!

The numbered boxes below show the main windows we’ll be using in FileZilla.

1. Is the connection status box that shows the logs as the system connects to your EC2 instance.

2. These are your local files on your system

3. These are the files on your EC2 VM. By default both these

Step 6: Navigate to your Flask python files you downloaded in the previous resource section and drag them accross to your EC2 home directory (default directory).

Great! You’re almost ready to have a fully working CV API live on the web!

Next, we need to change the security group of our EC2 to allow HTTP inbound traffic.

Running Our CV Web App

Step 1: Let’s go back to your terminal in our VM or local OS and connect to our EC2 system

ssh -i ~/.ssh/my_aws_key.pem ubuntu@18.224.96.38

Step 2: Check to see if the files we transferred using FileZilla are indeed located on our EC2 Instance. Running ls should display the following files:

Step 3: Launch our Web App by running this line:

sudo python3 webapp.py

If you see the above, Flask is running our CV Web App! Let’s go the our web browser and access it:

Step 4: Enter the EC2 IP in your web browser and your should see the following.

Congratulations! You’ve just launched a simple Computer Vision API for anyone in the world to use! Let’s upload some test images to see our Web App in action!

Awesome! Note:

  1. This isn’t a production-grade Web App, there are many things needed to make this ready for public consumption (mainly in terms of security, scalability, and appearance), however as of now, it’s perfect to demo friends, colleagues, potential investors etc.
  2. To keep our API running constantly launch the python code using this instead:

sudo nohup python3 forms.py & screen

Finally, let’s deploy our API version of this App and test using Postman.

Running your CV API on EC2

Postman is an extremely useful app used to test APIs. We’ll be using it to demonstrate our CV API

Step 1: Install Postman

Windows/Mac Users — https://www.getpostman.com/downloads/

Ubuntu — Open Ubuntu Software, search for Postman and install

Step 2: Setting up Postman for testing your EC2 API

Using Postman (see image above and the corresponding numbered steps below:

  1. Change the protocol to POST
  2. Enter the Public IP Address of your EC2 Instance: http://18.224.96.38
  3. Change Tab to Body
  4. Select the form-data radio button
  5. From the drop down, select Key type to be file
  6. For Value, select one of our test images
  7. Click send to send our image to our API
  8. Our response will be shown in the window below.

The output is JSON file containing:

{
"MainColor": "Red",
"catOrDog": "Cat"
}

The cool thing about using Postman is that we can generate the code to call this API in several different languages:

See blue box to bring up the code box:

Code Generator

Note: To keep our API running constantly launch the python code using this instead:

sudo nohup python3 forms.py & screen

Conclusion

So you’ve just successfully deployed a Computer Vision Web App and API that can be used by users anywhere in the world! This facilitates many possibilities from developing Mobile Apps, startup products and research tools. However, it should be noted there are many improvements that can be made:

  • Saving our uploaded image to as S3 Bucket. We don’t want to store all these images on our API server and it wasn’t designed for mass storage. It’s quite easy to configure and S3 bucket and store all uploaded images there. If privacy is a concern we can forget that entirely and use Python to delete the image after it’s been processed.
  • Using Nginx or Gunicorn or something similar to be the main server for our API
  • Using a more scalable design (this far from my expertise, but if you ever wanted to support simultaneous users, you’ll need to make some adjustments.

Want to learn more about Computer Vision? Check out my two courses cover OpenCV and Deep Learning using Keras in Python.

Computer Vision Intro™ OpenCV4 in Python with Deep Learning

Deep Learning Computer Vision™ CNN, OpenCV, YOLO, SSD & GANs

--

--