This project was developed as a side project at difference-engine.ai
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
pets-nasnetmobile-all-nontrainable-30-0.91.hdf5
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
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)- Pet Classification preview: https://afzalsayed96.github.io/client_side_ml/pets_classification.html
- Style Transfer preview: https://afzalsayed96.github.io/client_side_ml/style_transfer.html
classifier = await tf.loadLayersModel('model.json');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.
extension-file extension/pet_classification.crx
extension-file extension/style_transfer.crx
- 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_classificationorextension/style_transferand click open
Note:
- Disable the extension when not using
- Do not enable both extensions at once


