Making Predictions with a Tensorflow/Keras Model on a Webpage

 After your TF/Keras model is built how do you deploy it to a webpage so people can make predictions?

TLDR: You need to convert your model to tensorflow.js so you can run it on your browser.


Pre-Req

-Your finished model

-A way to load files to an http/https page (ex: github)

- - - - - - - - - -

Steps

1. Import tensorflowjs through your cmd or whatever you use: 

    import tensorflowjs as tfjs

2. Wherever you've made your Keras model, convert your model to a tensorflow.js layers format and export it:

    tfjs.converters.save_keras_model(<your_model>, <dir path and name of your save location>)

    This will save a .json file with the model architecture/graph (description of the layers and their connections), and a .bin file with the sharded model weight files in binary format

3. On your HTML page, include tensorflowjs

    <script type = "text/javascript" src = "https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script>

4. You will need to put your tfjs model files on a http/https page. Perhaps with github pages? And get the link to the .json file.

5. Load your model using tf.loadLayersModel

var model;

async function loadModel(){
    model = await tf.loadLayersModel(<wherever your json file is>);
}

6. Make a prediction

    prediction = model.predict(<whatever your input var is>);

- - - - - - - - - -

Use that prediction for whatever your case is and you're set!

Comments

Popular posts from this blog

SQL Syntax Notes

Basic Python Functions