Getting Started with Amazon EC2: Deploying Deep Learning model using Tensorflow , Keras & Flask on EC2

Cersei Codes (Radhika Halde)
6 min readMar 5, 2019

--

A complete all in one guide for beginners who started exploring EC2 for deploying machine learning models.For the demo purpose I have used code on Neural Network to predict the death of the characters in Game of thrones also for I am crazy for the show :P.I am sharing the steps which are super simple and easy to replicate.
Come let’s begin.

Step by step Instructions begins

Step 1:Firstly we go to AWS account and Sign in /login to the account.If you do not have an account lets create it first.When you are creating AWS account for first time, they will ask you for the credit card /debit card details and will deduct 2 rs(it gets credited back into your bank within 24 hrs).Cool now you are proud owner of AWS account.

Login to the AWS console in Services tab and select EC2. Select the region in which you want to provision your server.

EC2 service on AWS management console

On the dashboard click on launch instance.Select the Amazon linux server of 64 bit which supports Python.

Go to the tab of Configure Security group. Click on add rule and Select type as HTTPS ,protocol as TCP ,port as 443 and source as 0.0.0.0/0 under custom tab.Security group rules control the inbound and outbound traffic too and fro from the instance.

Security group configuration

When we click on launch window pops up for creation of new key pair.This key pair is of private key and public key which we will use for the authentication.Create the key and download the key on your machine(.pem format file gets downloaded).Keep this file safe in separate folder.We are ready for launch now

Private and public key pair
The launch status is shown once the instance get created.

Step 2: For connecting to the Linux instance we need to download and install Putty from download page.We need to generate private key in .ppk format by loading .pem file using putty key generator.Click yes on warning and .ppk file gets downloaded.Keep this key safe its the key to the treasure which we are going to explore ahead.

Click yes on the warning and proceed
Private key converted to .ppk format

Go back to EC2 dashboard and click on the instance created and copy the public IP keep it handy.We pick the default username based on the type of the Amazon machine which we choose to create the instance.

Open PuTTy desktop application, Start →PuTTy →PuTTy Configuration tab.Add host name which is combination of default user name +public IP.

Further its time for SSH authentication,we add session first and save it.Load the saved session and go to SSH → Auth →Browse the private key in .ppk & hit open.

New session for the instance opens up where you can type bash commands to install all packages required for deploying code.

Step 3: Transferring files to the Linux Instance using WinSCP. Download and install WinSCP from here.Open WinSCP and for hostname add the public DNS hostname and the IPv4 address of your instance

Host name is combination of default username of AMI appended with the public DNS address

For Authentication click on advanced →Authentication →Provide the .ppk private key.Saved the session with proper name.

We can see the bash file present in our Instance.We can drag and drop files from remote machine to the Instance.

Step 4: We start installing all the packages with help of PIP the default installer in python.Linux machine needs yum to install PIP.Before that change to the super user mode.

sudo --i
sudo yum upgrade python-setuptools
sudo yum install python-pip python-wheel
sudo yum install python36 python.pip
export CUDA_HOME=/home/ec2-user/cuda
export CUDA_ROOT=/home/ec2-user/cuda
export PATH=$PATH:$CUDA_ROOT/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CUDA_ROOT/lib64
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

hhhhh

ddd

import flask
import numpy as npy
import tensorflow as tf
from keras.models import load_model
from sklearn.model_selection import train_test_split
from keras.models import load_model

import flask
import numpy as npy
import tensorflow as tf
from keras.models import load_model
from sklearn.model_selection import train_test_split
from keras.models import load_model
# load GOT characters death dataset
import pandas as pds
path='C:/Users/rhalde/Documents/blog/blog/character-predictions.csv'

Data_A = pds.read_csv(path, usecols= [7, 16, 17, 18, 19, 20, 25, 26, 28, 29, 30, 31])
Data_B = pds.read_csv(path, usecols=[32])

print (Data_A)
print (Data_B)

# Splitting the dataset into the Training set and Test set
train_dataA,test_dataA,train_dataB,test_dataB = train_test_split(Data_A.values, Data_B.values, test_size = 0.3)

print (train_dataA.shape)
print (test_dataA.shape)
print (train_dataB.shape)
print (test_dataB.shape)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
train_dataA = sc.fit_transform(train_dataA)
test_dataA = sc.transform(test_dataA)

# create model
from keras.models import Sequential
from keras.layers import Dense
neural_model = Sequential()
neural_model.add(Dense(15, input_dim=12, activation='relu'))
neural_model.add(Dense(15, activation='relu'))
neural_model.add(Dense(15, activation='relu'))
neural_model.add(Dense(1, activation='sigmoid'))
neural_model.summary()

# Compile model
neural_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# log for tensorboard graph purpose
import keras
tbCallBack = keras.callbacks.TensorBoard(log_dir='/tmp/keras_logs', write_graph=True)

# Fit the model
neural_model.fit(train_dataA, train_dataB, epochs=200, batch_size=50, verbose=1, callbacks=[tbCallBack])

# Predicting the Test set results
Y_pred = neural_model.predict(test_dataA)
Y_pred = (Y_pred > 0.6)

# Calculating Model Accuracy
from sklearn.metrics import accuracy_score
acs = accuracy_score(test_dataB, Y_pred)
print("\nAccuracy Score: %.2f%%" % (acs * 100))

from keras.models import model_from_json

"""# Save the model
neural_model.save('gotCharactersDeathPredictions.h5')
del neural_model"""

import pickle

pkl_filename = "got_model.pkl"
with open(pkl_filename, 'wb') as file:
pickle.dump(neural_model, file)

# Load from file
with open(pkl_filename, 'rb') as file:
pickle_model = pickle.load(file)
print("read file")

llll

from keras.models import load_model

import flask
import numpy as np
import tensorflow as tf
import tensorflow as tf
import pickle
import flask
import numpy as np
import requests
import pandas as pd
import json
from keras.preprocessing.sequence import pad_sequences

#initialize our Flask application and the Keras model
app = flask.Flask(__name__)
def init():
global model,graph
# load the pre-trained Keras model
# model = load_model(‘gotCharactersDeathPredictions.h5’)
graph = tf.get_default_graph()


with open(‘got_model.pkl’, ‘rb’) as file:
model = pickle.load(file)
print(“read file”)

# Getting Parameters
def getParameters():
parameters = []
parameters.append(flask.request.args.get(‘male’))
parameters.append(flask.request.args.get(‘book1’))
parameters.append(flask.request.args.get(‘book2’))
parameters.append(flask.request.args.get(‘book3’))
parameters.append(flask.request.args.get(‘book4’))
parameters.append(flask.request.args.get(‘book5’))
parameters.append(flask.request.args.get(‘isMarried’))
parameters.append(flask.request.args.get(‘isNoble’))
parameters.append(flask.request.args.get(‘numDeadRelations’))
parameters.append(flask.request.args.get(‘boolDeadRelations’))
parameters.append(flask.request.args.get(‘isPopular’))
parameters.append(flask.request.args.get(‘popularity’))
return parameters

# Cross origin support
def sendResponse(responseObj):
response = flask.jsonify(responseObj)
response.headers.add(‘Access-Control-Allow-Origin’, ‘*’)
response.headers.add(‘Access-Control-Allow-Methods’, ‘GET’)
response.headers.add(‘Access-Control-Allow-Headers’, ‘accept,content-type,Origin,X-Requested-With,Content-Type,access_token,Accept,Authorization,source’)
response.headers.add(‘Access-Control-Allow-Credentials’, True)
return response

# API for prediction
@app.route(“/predict”, methods=[“GET”])
def predict():
nameOfTheCharacter = flask.request.args.get(‘name’)
parameters = getParameters()
inputFeature = np.asarray(parameters).reshape(1, 12)
with graph.as_default():
raw_prediction = model.predict(inputFeature)[0][0]
if raw_prediction > 0.5:
prediction = ‘Is Alive’
else:
prediction = ‘Killed/Dead’
return sendResponse({nameOfTheCharacter: prediction})

# if this is the main thread of execution first load the model and then start the server
if __name__ == “__main__”:
print((“* Loading Keras model and Flask starting server…”
“please wait until server has fully started”))
init()
app.run(threaded=True)

--

--

Cersei Codes (Radhika Halde)
Cersei Codes (Radhika Halde)

Written by Cersei Codes (Radhika Halde)

Machine learning Engineer, Martial Artist

Responses (1)