GithubHelp home page GithubHelp logo

imageclassification-docker-fastapi's Introduction

About the Project

The target of the project is to build a image classification model and move the model to production. The approaches which are being embraced are:

  1. Building a deeplearning classification model from freely available datset. In this case This dataset is being used.
  2. Set up a communication URL.
  3. Accept input from a wide variety of environments/formats when it is sent to the URL.
  4. Convert every form of input into the exact format that the machine learning model needs as input.
  5. Make predictions with the trained deep learning-based model.
  6. Convert predictions into the right format and respond to the client's request with the prediction.
  7. Create a Docker file for classification and deploying it to cloud instance.
  8. Deploying the FMNIST classifier on an Amazon Web Services (AWS) Elastic Compute Cloud (EC2)

Building a VGG16 Model

For training this model I am using pytorch library. It provides torchvision module which will be used to download the pretrained model on Imagenet. I will describe the main steps in subsequents points. Find the notebook here

  1. The pretrained model is downloaded, as I am having 10 classes finetuning is done to classify the desired 10 classes, it can be seen that at the very last layer it has 10 FC layers. The classes are dog,horse,elephant,butterfly,gallina,chicken,cat,cow,sheep,spider,squirrel.
    getmodel
  2. Data augmentation like: RandomRotation, RandomResizedCrop, RandomHorizontalFlip is being used to create data loader. The images are resized into 224,224 as height and width. For training set 80% of the images being used. And for training and validation set 10% per each is being used.
  3. Loss function CrossEntropyLoss is used. Adam optimizer is used to update learning parameters of each neural network parameter during during. CosineAnnealingLR is used to set learning rate accoring to the validation loss, if the loss increases the model assigns bigger step to gradient descent, for lower loss vice versa.
    training
  4. For gradient updates in each iteration of training opt.zero_grad(), loss.backward(), opt.step() is being used. We are training our model for five epochs.
  5. Few things are important in pytorch. When the model is traing, the datset and model should be sent to the device (cpu/gpu). And before training model should be set to train model with model.train(). For evaluation and test model.eval(). When the evaluation and test phase happens it should be run with torch.no_grad(), as it obstructs the model to update the gradients at that time.
  6. Softmax function is used for probability of multiclass classification.
  7. The model has achieved 96% accuracy with test set, which dataset model has not seen during training.
  8. The best model according to the best validation accuracy is being saved in directory.

Creating an API and making prediction on a local server

We will use fastapi to make a classification on local server, then we will build docker image out of it and make a prediction with curl command.
  1. A Classification module in classifcation.py file is being set up. Where the pretrained model is being downloaded one time and the required finetuning is done according to the model's architecture. Every image being sent as a request it will be resized to our model's desired input.
  2. In server.py the get and post method is built. The requested image will be read as bytes format and for every request our predict function will called to return the prediction as well as confidence.
    uvicorn
    fastapiinfernec

Creating Docker

The creating docker image is discussed here.
  1. The requirements file is created with pip freeze > requirements.txt
  2. Few libraries are seperately installed as the docker hub provides larger files and it is avoided for that. The libraries are numpy, torch, torchvision.
  3. The docker is built with this: docker build -t imageclf:latest .
    dockerbuild
  4. The port is forwarded to 5000 to get the response from the image. To send the request this shell command is being used: "curl -X POST "http://127.0.0.1:5000/predict" -H "accept:application/json" -H "Content-Type: multipart/form-data" -F "file=@/home/kz/Downloads/elephant.jpg;type=image/jpg" ". From the image it can be seen that the response is two values, one is class name and second is confidence score.
    dockerinft2o

Shipping and running Docker container in the cloud.

I don't have AWS account. But here the basic steps has been discussed of running the container on the cloud.

  1. Configure AWS on the local machine. The AWS CLI can be installed from the official website.
  2. Create a Docker repository on AWS ECR and push the imageclf:latest image.
  3. Create an EC2 instance and install the dependencies in it.
  4. Create and run the Docker image on the EC2 instance. A public ip address can be set up to send api request and get response with curl in local machine.

imageclassification-docker-fastapi's People

Contributors

kazishawon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.