Deploying Image classification model in Azure as a Web Service.

Source: Deep Learning on Medium


This blog is about how to create a simple image classification model using Keras framework and deploy it into Azure Cloud as a web Service.

By the end of the article, you will learn the way how to build an image classifier using Convolutional neural network in Keras framework and how to put into production your trained model.


Building an image classification model in Keras!

Convolutional neural networks (also referred to as CNN or ConvNet) are a class of deep neural network that have seen a widespread adoption in a number of computer vision and visual imagery applications. For this case, we are going to build a simple networks with 3 layers. The image dataset used in this example is gear image dataset (adventureworks).

First step is to data pre-processing. There are in total 12 categories, each having more then 100 images. The images have different shape, but the model requires that images to be at the same shape. The images are resize to shape (128,128,3) and equalized afterwards. Image equalization is a graphical image processing technique used to improve the contrast of the image.

After we have processed all the images, its time to create the train/ test dataset. As labels are strings, we encode them to unique int starting from 0 to 11. Then to_categorical function in Keras is used to apply one-hot encoding technique, representing the labels as a binary matrix. The train/validation/test sets are are divided in 70/10/20 % respectively.

Lastly, we can build the neural network model. As we previously mentioned, we are using 2 convolutional layers, and the last one dense (fully connected). the accuracy on validation set is 94% and in test set 95%. Based on this scores, the model is very accurate and it is not over-fitting. The model is saved into a json file, and weight into .h5 file as Keras have its own format of saving the weights.

Deploying the model as Web Service in Azure!

There are different ways to deploy a trained model in Azure. You can used tools like “Machine learning workbench”, or using Azure machine learning studio, but also by using dockers to create your own environment. In this blog how to use dockers or also known as “containerization”.

In order to build with web Api we are going to use Flask: a micro web framework for python and very easy to install. As you can see for the gist below, we have created 2 url to access our system, “/isAlive” to check whether the service is running, and “prediction/image” to predict the given image. at line 8 we preprocess the image in the same way we did for training ones. we can build the function from the code above. Then we load the Keras model and predict the category of the image at line 10. The service will be run at port 5000.

In order to install the library dependency into docker image, we have to create a file named requirements, specifying all the libraries and its version to be installed while creating the container. Moreover, a docker file is needed to specify the requirement and information to create the image. The docker file will look like:

Now its time to build the docker container and push it into docker hub. For doing so we have to run the following commands:

  1. docker build . -t {tag name} -f ./Dockerfile
  2. run locally: docker run -p 3000:5000 -d {tag name}
  3. docker push $Docker_Id_User/tag name

The docker image now is in your docker repository where we can just run a pull command to retrieve it into another system. Now it is time to deploy it into Azure cloud. You need to have an Azure description to finish successfully the deployment. At the Azure portal we are going to apply the following steps:

  1. Create a new resource called “Web App for Containers”.
  2. Select a name about the resource, the location, operating system of docker file, price tier etc…
  3. At configure container, you will see different options, like Azure container Registry, docker hub, etc. choose docker hub. Also you should choose the repository access of you docker image. Generally, the repository is public when created, but it can be change to private afterwards. In case of private, you should provide credentials to pull your image. At the image file you just write down “image:tag”. For example in my case will be “alidoku/predictionApi:1.3”. The name of the image is the same with the tag name when creating the docker image.
  4. The above command will make a pull command automatically, and set the environment in place automatically in a few minutes. you can perform a test request “your-resource-name.azurewebsites.net/isAlive” to check whether the service is running.

For more information about docker you can check the documentation online. You can find here an example of Flask and Docker.

Thanks for reading! If this article triggered your interest or left you with some unanswered questions, feel free to get in touch.