Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tf.loadModel is not a function

I am Classifying images using Keras MobileNet and TensorFlow.js in Google Chrome.I followed a tutorial. when i run the code i got the tf.loadModel is not a function error. I load the model like this.

	model = await tf.loadModel('https://gogul09.github.io/models/mobilenet/model.json');
	model.predict();

error details

like image 468
ABCD Avatar asked Mar 11 '19 16:03

ABCD


2 Answers

The function loadModel has been renamed to loadLayersModel.

like image 63
anegru Avatar answered Oct 16 '22 10:10

anegru


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() {
like image 20
Manoj Mohan Avatar answered Oct 16 '22 09:10

Manoj Mohan