Model.predict() not a function

1,072 views
Skip to first unread message

subham bhutoria

unread,
Jun 14, 2018, 8:01:35 AM6/14/18
to TensorFlow.js Discussion
I am trying to do image segementation of cell images to identify cells in tensor flow using data provided in https://www.kaggle.com/c/data-science-bowl-2018/data i have build a keras model consisting of unet architechture but i am not able to predict as it gives following error:
Model.predict() not a function

code in html/JSS


<html>
<title> TensorFlow </title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tf...@0.11.6"> </script>
<img id='Cells' src='../static/fig.PNG' alt='BVI Cells'>

<script>
	
	const model = tf.loadModel ("{{ url_for('static', filename='model.json') }}");
	var cells = document.getElementById('Cells');
	console.log(cells.alt);
	document.write(cells);
	const img = tf.fromPixels(cells);
        const prediction = model.predict(img);
	
</script>
</head>
<body>
</body><>

Shanqing Cai

unread,
Jun 14, 2018, 9:13:52 AM6/14/18
to subham....@gmail.com, TensorFlow.js Discussion
Subham,

Is it possible for you to share the value of the URL you passed to tf.loadModel(), so we can try to reproduce the issue on our end?

Thanks,
Shanqing

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfjs/6d612bb8-8b35-452d-95b7-8ad6128312e5%40tensorflow.org.


--
---
Shanqing Cai
Software Engineer
Google

Daniel Smilkov

unread,
Jun 14, 2018, 9:26:56 AM6/14/18
to Shanqing Cai, subham....@gmail.com, TensorFlow.js Discussion
Hi Subham,

tf.loadModel returns a Promise of a Model, which is why it's missing the predict() function. To fix, use .then() like this:

tf.loadModel (...).then(function(model) {model.predict(...) });

Daniel


Shanqing Cai

unread,
Jun 14, 2018, 5:06:07 PM6/14/18
to TensorFlow.js Discussion, ca...@google.com, subham....@gmail.com
+1 to what Daniel wrote. I overlooked the fact that you weren't using `tf.loadModel` correctly as an async function.
Message has been deleted

subham bhutoria

unread,
Jun 18, 2018, 3:34:32 AM6/18/18
to TensorFlow.js Discussion
Thanks Daniel and Shanqing for the post.
I am having problems with the img.reshape function
I am getting the following error.
Size(49152) must match the product of shape 1
    at inferFromImplicitShape (tf...@0.11.6:1)
    at e.reshape (tf...@0.11.6:1)
    at tf...@0.11.6:1
    at e.tidy (tf...@0.11.6:1)
    at r.value (tf...@0.11.6:1)
    at e.reshape (tf...@0.11.6:1)
    at (index):16 


Code in jS :
<html>
<title> TensorFlow </title>
<head>
<img id='Cells' src='../static/rename.PNG' alt='BVI Cells' width="128" height="128">

<script async>

   
var cells = document.getElementById('Cells',1);
console.log(cells.alt);
const img = tf.fromPixels(cells);
img = img.reshape(1,128,128,3)
tf.loadModel ("{{ url_for('static', filename='model.json') }}").then(function(model) {model.predict(img) });
   
   
   
    </script>
</head>
<body>
</body>
</html 
And if i dont use the reshape function i get the following error.
expected input_6 to have 4 dimension(s), but got array with shape [128,128,3]

subham bhutoria

unread,
Jun 18, 2018, 8:20:59 AM6/18/18
to TensorFlow.js Discussion
I have solved the above issues now i am getting a output after prediction a tensor of shape [1,128,128,1] consisting values from 0 to 1 now i need to output a picture of shape [128,128]  from the following tensor masking such that at places where value greater than 0.5 be white and values less than 0.5 be black

Please help me with a direction to achieve this.

Shanqing Cai

unread,
Jun 18, 2018, 9:08:33 AM6/18/18
to TensorFlow.js Discussion
This is a common pitfall when using tf.Model.predict(). The method expects batched input. In your example, the inputShape of the model is [128, 128, 1], but the predict() method really expects a [n, 128, 128, 1] tensor as the input, n being a positive integer.

This is because predict() currently always operates on batches, i.e., groups of individual examples. The extra dimension in the beginning ('n' in the example above) is for the examples.

We are weighing whether we should make predict() accept non-batched individual examples. It is being tracked by https://github.com/tensorflow/tfjs/issues/317
Message has been deleted

subham bhutoria

unread,
Jun 19, 2018, 2:08:17 AM6/19/18
to TensorFlow.js Discussion
@Shanqing Cai  can i reshape my input image of array shape [128,128,3] into a batched image of size 1 [1,128,128,3] and then use model.predict function
Reply all
Reply to author
Forward
0 new messages