Skip to content

afzalsayed96/client_side_ml

Repository files navigation

Client Side ML

This project was developed as a side project at difference-engine.ai

Directory Structure

client_side_ml
β”œβ”€β”€ convert.py
β”œβ”€β”€ css
β”œβ”€β”€ extension
β”‚Β Β  β”œβ”€β”€ pets_classification
β”‚Β Β  β”œβ”€β”€ pets_classification.crx
β”‚Β Β  β”œβ”€β”€ pets_classification.pem
β”‚Β Β  β”œβ”€β”€ style_transfer
β”‚Β Β  β”œβ”€β”€ style_transfer.crx
β”‚Β Β  └── style_transfer.pem
β”œβ”€β”€ images
β”œβ”€β”€ pets_classification.html
β”œβ”€β”€ style_transfer.html
β”œβ”€β”€ mobileNet.html
β”œβ”€β”€ models
β”œβ”€β”€ README.md
β”œβ”€β”€ sketch.js
β”œβ”€β”€ sketch_mobileNet.js
└── style_transfer_demo
    β”œβ”€β”€ convert.py
    β”œβ”€β”€ custom_layer.js
    └── demo2.html

Model

pets-nasnetmobile-all-nontrainable-30-0.91.hdf5

Steps to convert keras model to tensorflowjs format

https://www.tensorflow.org/js/tutorials/conversion/import_keras?hl=kn

  • Install tensorflowjs pip install tensorflowjs

  • Using bash

tensorflowjs_converter --input_format keras \
                       path/to/my_model.h5 \
                       path/to/tfjs_target_dir
  • Using python
import tensorflowjs as tfjs

def train(...):
    model = keras.models.Sequential()   # for example
    ...
    model.compile(...)
    model.fit(...)
    tfjs.converters.save_keras_model(model, tfjs_target_dir)

Note: The model generated with bash command method gives an error while loading. Use convert.py for our model. GH issue: tensorflow/tfjs#755

Quantization

We quantize our model in order to reduce it's size

import tensorflowjs as tfjs
import keras
import numpy as np
model = keras.models.load_model('./models/pets-nasnetmobile-all-nontrainable-30-0.91.hdf5')
tfjs.converters.save_keras_model(model, "./models/quantized", quantization_dtype=np.uint8)

Using TensorflowJS for inference

Loading the model

classifier = await tf.loadLayersModel('model.json');

Inference

prediction = await classifier.predict(image)

Note: First inference is very slow in browser because time of first call also includes the compilation time of WebGL shader programs for the model. Refer.

Extensions

Pet Classification

extension-file extension/pet_classification.crx

Style Transfer

extension-file extension/style_transfer.crx

Installing extension

  • Clone the repo
  • In chrome go to chrome://extensions/
  • Enable developer mode on the top right.
  • Click on load unpacked on top left
  • Navigate to extension/pets_classification or extension/style_transfer and click open

Note:

  • Disable the extension when not using
  • Do not enable both extensions at once

About

Client Side ML

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published