Full-stack Deep Learning Application Using AWS Fargate Serverless Infrastructure

Fargate

This post showcases full-stack Deep Learning Application Using AWS Fargate Serverless Infrastructure. The project we optimise a model for Utilisation of hospital beds – COVID-19. It is motivated by the overcrowding of hospital beds due to COVID-19. Another reason is to avoid the repeat of the events which happened at the beginning of the pandemic. Scarce healthcare resources due to COVID-19 require carefully made policies ensuring optimal bed utilization and quality healthcare service. An unsupervised deep learning algorithm is applied to decide the assignment of resources to patients. Further, developing and deploying AI applications is a challenging endeavour requiring a scalable infrastructure of hardware, software, and intricate workflows. This project shows an end-to-end Machine Learning workflow and aims to help for both policy-makers and the public.

What it does

Full-stack Deep Learning application uses AWS Fargate Serverless Infrastructure, data visualization and clustering algorithm. Clustering shows a new way to use the data to assess the impact of COVID-19. Modelling is applied to Geospatial data in order to solve the use case of optimizing the utilization of hospital beds. It uses the number of licensed, staffed, unstaffed, ICU, adult ICU, Paediatrics ICU beds, bed utilization, a potential increase in capacity, and average ventilator usage. This allows us to take into account both the number of resources and the intensity of care needed. It provides the means to optimize bed-occupancy management and evaluate geographical hospital resource allocation.

The data and model are deployed as a web app. The machine learning pipeline is deployed using AWS Fargate which is a serverless compute for containers. This enables us to build and host a fully functional containerized web app on AWS without provisioning any server infrastructure.

How I built it

I used Python for the project. The steps involved are as follows:

Import packages, read data, create business features.

Data Analysis and visualization with the presentation of the data on the map with folium and geopy.

Applying deep learning clustering algorithm called Self Organizing Map (SOM) with ‘minisom’ package. A SOM is a type of artificial neural network that is trained using unsupervised learning to produce a low-dimensional representation of the input space, called a “map”. We use the “clusters” from the algorithm to visualize on the Geospatial plot.

Build a front-end web application using Streamlit. It is an open-source Python library that makes it easy to build beautiful custom web-apps for machine learning and data science. The code is available here

To containerize our application for deployment we need a docker image that becomes a container at runtime. A docker image is created using a Dockerfile. A Dockerfile is just a file with a set of instructions. Dockerfile is case-sensitive and is in the project folder with the other project files. Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers are used to package up an application with all of its necessary components, such as libraries and other dependencies, and ship it all out as one package.

Deploy on AWS Fargate

Follow these simple steps to deploy app on AWS Fargate

Install Docker Toolbox on EC2

Installing and running Docker on AWS EC2 instance using Amazon Linux AMI

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/docker-basics.html

Create a Repository in Elastic Container Registry (ECR)

Build and push a Docker image onto Amazon Elastic Container Registry. Amazon Elastic Container Service (Amazon ECS) is a container orchestration platform. The idea behind ECS is similar to Kubernetes (both of them are orchestration services.

(a) Login to your AWS console and search for Elastic Container Registry

(b) Create New Repository on Amazon Elastic Container Registry

Click on “Create Repository”.

(c) Click on “View push commands”:

Execute push commands

Navigate to your project folder using Anaconda Prompt and execute the commands you have copied in the step above. You must be in the folder where the Dockerfile and the rest of your code reside before executing these commands.

These commands are for building docker image and then uploading it on AWS ECR.

Check your uploaded image

Click on the repository you created and you will see an image URI of the uploaded image in the step above. Copy the image URI

Create and Configure a Cluster

Deploy web app using serverless infrastructure i.e. AWS Fargate. It is a serverless compute engine for containers that work with both Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS). It removes the need to provision and manage servers, we specify and pay for resources per application, and improves security through application isolation by design. The application is stateless (A stateless app is an application that does not save client data generated in one session for use in the next session with that client).

(a) Click on “Clusters” on left-side menu: Create Cluster

(b) Select “Networking only” and click Next step:

Image for post

Select Networking Only Template

(c) Configure Cluster (Enter cluster name) and click on Create: Configure Cluster

Click on “Create”.

(d) Cluster Created: Cluster Created

Create a new Task definition

A task definition is required to run Docker containers in Amazon ECS. Some of the parameters you can specify in a task definition include the Docker image to use with each container in your task. How much CPU and memory to use with each task or each container within a task.

(a) Click on “Create new task definition”:

Image for post

Create a new task definition

(b) Select “FARGATE” as launch type:

Image for post

Select Launch Type Compatibility

(c) Fill in the details:

Configure Task and container definitions (part 1)

Configure Task and container definitions (part 2)

(d) Click on “Add Containers” and fill in the details:

Adding Container in task definitions

Click “Create Task” on the bottom right.

Execute Task Definition

In last step we created a task that will start the container. Now we will execute the task by clicking “Run Task” under Actions.

(a) Click on “Switch to launch type” to change the type to Fargate:

(b) Select the VPC and Subnet from the dropdown:

Click on “Run Task” on bottom right.

Allow inbound port 8501 from Network settings

One last step before we can see our application in action on Public IP address is to allow port 8501 (used by Streamlit) by creating a new rule. In order to do that, follow these steps:

(a) Click on Task

(b) Click on ENI Id:

(c) Click on Security groups

(d) Scroll down and click on “Edit inbound rules”

Image for post

(e) Add a Custom TCP rule of port 8501

Use public IP address with port 8501 to access the application

Click to view the application.

Fargate

Leave a Reply