Skip to content

hoon0624/garbage-classification-heroku

Repository files navigation

Garbage Classification

1. Machine-Learning

Training the model was done with google colab.

Organizing data & Pre-processing

  - data
    - test
    - train
        - cardboard
        - glass
        - metal
        - paper
        - plastic
        - trash
    -valid
        - cardboard
        - glass
        - metal
        - paper
        - plastic
        - trash

Then the data was pre-processed using Fastai library .

from fastai.vision import transform

tfms = get_transforms()
  • Fastai supports image augmentation on brightness, contrast, crop, crop_pad, dihedral, dihedral_affine, flip_lr, flip_affine, jitter, pad, perspective_warp, resize, rotate, rgb_randomize, skew, squish, symmetric_warp, tilt, zoom, cutout.
  • Learn more about it in this article by Sanyam Bhutani.

Then ImageDataBunch was created from the data

# data folder path
path = Path(os.getcwd())/"data"

tfms = get_transforms(do_flip=True, flip_vert=True)
data = ImageDataBunch.fom_folder(path, test="test", ds_tfms=tfms, bs=16)

Training model

A few different methods were attempted.

First, Keras was used to create a model with conv1 layer to conv4 layers. However, the model was not complex enough and it was underfitting.

conv4

  • conv4 layer model

conv4acc

  • Low validation accuracy compared to training accuracy

So pre-trained model ResNet34 was used with Fastai library.

learn = cnn_learner(data, models.resnet34, metrics=[accuracy])
# find learning rate
learn.lr_find(start_lr=1e-6, end_lr=1e1)
learn.recorder.plot(suggestion=True)
  • Find and pick a right learning rate to train the model
# train model
learn.fit_one_cycle(15, max_lr=1e-03)
  • Train the model

fataitrain

  • High training accuracy

The model got test accuracy of about 91%.

testacc

Confusion Matrix

confmat

Precision Recall Curve

precrecall

2. Deploying image classification model

Setting up Dependency & Virtual environment

  • First, there was some trouble trying to install fastai using pip. So Anaconda was used for virtual environment with pip. For some reason, with pip install there always occured some sort of CMake error.
  • Use conda create --name fastai to create virtual environment named 'fastai'
  • And conda install -c fastai fastai to install fastai

requirements.txt

fastai==1.0.60
https://download.pytorch.org/whl/cpu/torch-1.4.0%2Bcpu-cp36-cp36m-linux_x86_64.whl
Flask==1.1.2
torchvision==0.5.0
Werkzeug==1.0.1
gunicorn

a. Render

  • Render is very simple way to deploy image classification model online.

  • Used this template and forked github repository and twicked server.py and a javascript file.

  • Then the repository was pushed on the github.

  • On Render.com find the repository and create new web service and it's done!

Render

  • view full code here.

b. Heroku

  • There is a nice template here by Shankar Jha.

  • Modify main.js, main.css, index.html, requirements.txt, model as needed.

  • Initialize the repository, and add, commit push to Heroku Git (git push heroku master).

Successfully deployed.

demo1 demo2

Next Step

This was my first experience with machine learning/ image classification. Through some trial and error I manged to deploy my own ml web-app. The next step for this could be ...

  • Getting higher accuracy with better pre-processing image.
  • Trying to get nearly high accuracy with my own implemented model.
  • Make it into a web-app that classifies in real-time with web-cam.
  • Add CI/CD integration.
  • Add other features to web-app such as informational messages about each recycling category, and etc.

Helpful resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages