End-to-End Multi-label Classification
Tags: Artificial Intelligence, Machine Learning, Data Visualization, Data Science, Docker

Deployment

AWS based deployments are pretty simple sequential procedures. From previous steps will be already having a minimal prediction script (i.e. Recognize_Item class), which we’ll import to a flask app. Later we’ll dockerize the module with all the dependencies for better distribution, then host the docker image to EC2 instance. We’ll follow 3 step procedure for deployment:

STEP 1 : Flask Setup

Recognize_Item (from part 3) will load model and necessary functions to handle multi-label classification. Note that we need to catch the images as bytes array, so the load_image function reads the image as BytesIO.

import recognize_item_class as cf

import flask app = flask.Flask(__name__)

def home():

return "<h1> Image Recognition Module [ON]</h1>" @app .route("/")def home():return " Image Recognition Module [ON] "

def predict():

'''

Note: Ensure image through flask properly hit here

'''

if flask.request.method == "POST":

if flask.request.files.get("image"):

#

image = flask.request.files["image"].read()

#

image = reco.process_img(image)

#

result = reco.predict_all(model, image)

#

return result

return "<h1>No Result</h1>" @app .route("/predict", methods=["POST"])def predict():'''Note: Ensure image through flask properly hit here'''if flask.request.method == "POST":if flask.request.files.get("image"):image = flask.request.files["image"].read()image = reco.process_img(image)result = reco.predict_all(model, image)return resultreturn " No Result " if __name__ == "__main__":

print('* flask starting server...

* Loading model file..')

#

# Declare the Recognition object

reco = cf.Recognize_Item()

#

# Load Model (avoid-reloading)

global model

model = reco.load_model()

model._make_predict_function()

#

app.run('0.0.0.0', 5000, debug=True)

Whenever we receive a post (to 5000:/predict ) request to the Flask app it will pre-process the image, predict all labels and finally return the prediction in a structured json response.

STEP 2 : Docker Setup

(with patience!) Create Docker Image

You need to have Docker installed and setup. You can get instructions for setup from here. First, you will need to create a directory with only the required files and scripts for deployment. Create a requirements.txt file using pip freeze or manually writing the required packages. Robert has given a detailed article for the same. Now we need to make a file with the name Dockerfie (and no extension) in the folder where the flask app and script are located. The file should contain:

FROM python:3.6

WORKDIR /app

COPY . /app

RUN pip install -r requirements.txt

EXPOSE 8500

CMD [“python app.py”]

Few things to know: FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. RUN instruction will execute any commands in a new layer on top of the current image and commit the results. In our case, we want to install all the dependencies for our flask application to the docker image. EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. Here, the purpose of CMD is to provide defaults for an executing container. In our case, run the flask application. For more info on the docker commands, check the official docker documentation here. It is beautifully written and easy to understand.

5. After the Dockerfile is made, we need to create the docker image using the command: docker build -t item_recognition:latest . which means that we are building a docker image with the name item_recognition with tag latest and . represents that the Dockerfile is contained in this folder.

Now it might take some time to download the base image and install all the requirements. After Step 5 is done, you can view your docker image with the command docker images .

Saving the image to use it on EC2 instance

Now that we have the image in our system, we can run the image using the commands specified in the next step, but if we need to save the image and then use it on another system, follow these steps:

docker save item_recognition:latest > item_recognition.tar . This will create a .tar file containing the image. Now you can transfer it to any system or instance which has docker installed and load this image using the command: docker load < item_recognition.tar

Now once you list docker images on the new system or instance, you will find the item_recognition image there. This section would have not been possible without Arjun Muraleedharan

….

STEP 3 : AWS Setup

(This steps assumes that reader is familiar with EC2 instances)

Start an EC2 instance, recommended will be a t3.medium or t3.small machine. Once you have an instance ready, upload the Docker image from step 2 to the machine to the instance. Create a new screen and using following command run the docker image to an external port (say 8500):

$ screen -S item_recognition $ docker run -it -p 8500:5000 item_recognition

Remember the port number for posting the requests & that’s all folks.

Let’s check if everything is fine [POSTMAN]

API’s results in visualization Postman

….

The journey of this article started with Data explorations using pandas, then we built a customized ResNet type multi-label model using Keras (same procedure applicable with tf.keras as well). We efficiently packaged our model with necessary functions, this package could easily be called in our Flask app. We dockerized the environment and finally hosted it with AWS.

Thanks for your time, dear reader!