Giter VIP home page Giter VIP logo

cats-vs-dogs-classifier-pruned-and-served's Introduction

Cats vs. Dogs Classifier with Model Pruning and Deployment

In this repo, we:

  • Finetune a Squeezenet trained on Imagenet to specialise in classifying cats versus dogs (using a novel implementation of L1 regularisation),

  • Iteratively prune this finetuned model, reducing its size from 2.9MiB to 1.7MiB while still maintaining the 98 per cent accuracy rate achieved before any pruning,

  • Serve this pruned model for prediction using Flask and Docker.

All of these steps are reproducible following the instructions below.

Training the model

You will need unzip and jhead installed to unzip and prepare the cats vs. dogs dataset.

sudo apt-get update && sudo apt-get install -y unzip jhead

Then to train the model:

cd /path/to/repo/train_model

# Make a  virtual environment with the required packages (or just `pip install -r requirements.txt`)
mkvirtualenv -r requirements.txt -p python3.6 catsdogs

# Ensure you are in the correct virtualenv, then run the training script
python train.py

You should get around 98% final validation accuracy.

Serving the model

Preparation

After training the model, the final weights will be saved to final_pruned_model.pth in the repo root. You need to copy these to the serve_model folder:

cd /path/to/repo/train_model
cp final_pruned_model.pth serve_model/

Once that's done, you just need to build the image and run it.

You will need nvidia-docker installed if you want to use GPU, otherwise just docker. If you want to use just CPU instead of GPU, replace FROM ufoym/deepo:pytorch-py36-cu100 with FROM ufoym/deepo:pytorch-py36-cpu in the first line of serve_model/Dockerfile.

Last step is to build and run the image:

cd /path/to/repo/serve_model
nvidia-docker build -t cats-vs-dogs .
nvidia-docker run -p 5000:5000 cats-vs-dogs

/upload classification UI endpoint

Point your browser to http://localhost:5000/upload where you can upload an image to classify through the model.

If you are seeing the wrong images after uploading a few, try hard-refreshing the /uploader page (Ctrl+Shift+R) or clearing your browser cache.

/classify POST endpoint

You can also use localhost:5000/classify endpoint to POST data to be classified. It accepts JSON format data and binary data.

Here is an example of how you might post some binary data in Python:

import requests

with open('dog.jpeg', 'rb') as f:
	data = f.read()
r = requests.post(
    url='http://localhost:5000/classify',
    data=data,
    headers={'Content-Type': 'application/octet-stream'}
)
r.json()

Out[1]: {'predictions': [[3.103691051364876e-05, 0.9999690055847168]],
	     'classes': ['Cat', 'Dog']}

Here is an example of how you might post some JSON data in Python:

import torch
from torchvision import transforms
from PIL import Image
import requests

img = Image.open('dog.jpeg')
t = transforms.Compose([
    transforms.Resize(224),
    transforms.ToTensor(),
])
tensor = t(img)

headers = 'Content-Type: application/json'
payload = {'tensor': tensor.tolist()}

r = requests.post("http://localhost:5000/classify",
                  json=payload)
r.json()

Out[1]: {'predictions': [[3.103691051364876e-05, 0.9999690055847168]],
	     'classes': ['Cat', 'Dog']}

This also supports batches of tensors (sent as JSON 4D lists).

You could also use curl, for instance (with some fake data):

curl localhost:5000/classify -d '{"tensor": [[[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]], [[0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]]}' -H 'Content-Type: application/json'

{"predictions": [[0.7564941048622131, 0.24350589513778687]], "classes": ["Cat", "Dog"]}

cats-vs-dogs-classifier-pruned-and-served's People

Contributors

nlml 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.