tf.loadModel is not a function

You're observing the error because the tf.loadModel API has changed in the recent versions of tensorflow.js. I could get the prediction working by doing the following changes in the project https://github.com/Gogul09/digit-recognizer-live:

In index.html, change the version to 0.10.0 instead of latest.

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>

After doing this change, I got one more error: "Argument 'b' passed to 'div' must be a Tensor, but got number."

To fix this, in app.js, function preprocessCanvas() change tensor.div(255.0) to

tensor.div(tf.scalar(255.0))

Depending on whether you're using MLP or CNN model make the change accordingly. Reload the page, once you change the js file.

For this project, https://github.com/Gogul09/mobile-net-projects, after hard-coding the tf js version, the prediction didn't work because the click event of the predict button was not getting triggered. Also, for Upload Image. After replacing these lines in mobile-net.js, prediction works.

Change

$("#predict-button").click(async function () {

to

$(document).on('click', '#predict-button', async function() { 

Change

$("#select-file-image").change(function() {

to

$(document).on('change', '#select-file-image', function() {

The function loadModel has been renamed to loadLayersModel.