TensorFlow.js speed in the browser
up vote
2
down vote
favorite
I've trained a simple bidirectional LSTM network in Keras (20 units) and exported the model via
tfjs.converters.save_keras_model(model, 'myModel')
The model is 53kb large. In my JavaScript app, I load the model like this
var model;
async function loadModel() {;
model = await tf.loadModel('https://example.com/myModel.json');
}
and afterwards I run my predictions with
async function predict(input) {
var pred = model.predict(input);
...
}
It takes 5-6 seconds till the model is loaded, this is fine. But what bothers me is that every call of predict() also takes 5-6 seconds. Every time. For my use case, I'd need the prediction to be extremely fast, 1 second or less.
My question is: Is this normal? Or is something wrong with my code?
Edit: Here is a codepen: https://codepen.io/anon/pen/XygXRP
BTW, model.predict is blocking the UI - how can I prevent that?
javascript performance tensorflow tensorflow.js
|
show 2 more comments
up vote
2
down vote
favorite
I've trained a simple bidirectional LSTM network in Keras (20 units) and exported the model via
tfjs.converters.save_keras_model(model, 'myModel')
The model is 53kb large. In my JavaScript app, I load the model like this
var model;
async function loadModel() {;
model = await tf.loadModel('https://example.com/myModel.json');
}
and afterwards I run my predictions with
async function predict(input) {
var pred = model.predict(input);
...
}
It takes 5-6 seconds till the model is loaded, this is fine. But what bothers me is that every call of predict() also takes 5-6 seconds. Every time. For my use case, I'd need the prediction to be extremely fast, 1 second or less.
My question is: Is this normal? Or is something wrong with my code?
Edit: Here is a codepen: https://codepen.io/anon/pen/XygXRP
BTW, model.predict is blocking the UI - how can I prevent that?
javascript performance tensorflow tensorflow.js
On which device and in which browser do you run the model?
– Sebastian Speitel
Nov 14 at 22:56
It's a Surface Book 2, Intel i7 with 1.90GHz 2.11 GHz, 16GB RAM on Chrome (latest version)
– Simon Nobel
Nov 14 at 23:12
And the speed is vastly different when predicting in python?
– Sebastian Speitel
Nov 15 at 0:28
I just measured it, with model.predict() in python it takes roughly 300 milliseconds
– Simon Nobel
Nov 15 at 9:01
Could you please make a working snippet on codepen.io or share a gist ?
– edkeveked
Nov 15 at 10:04
|
show 2 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I've trained a simple bidirectional LSTM network in Keras (20 units) and exported the model via
tfjs.converters.save_keras_model(model, 'myModel')
The model is 53kb large. In my JavaScript app, I load the model like this
var model;
async function loadModel() {;
model = await tf.loadModel('https://example.com/myModel.json');
}
and afterwards I run my predictions with
async function predict(input) {
var pred = model.predict(input);
...
}
It takes 5-6 seconds till the model is loaded, this is fine. But what bothers me is that every call of predict() also takes 5-6 seconds. Every time. For my use case, I'd need the prediction to be extremely fast, 1 second or less.
My question is: Is this normal? Or is something wrong with my code?
Edit: Here is a codepen: https://codepen.io/anon/pen/XygXRP
BTW, model.predict is blocking the UI - how can I prevent that?
javascript performance tensorflow tensorflow.js
I've trained a simple bidirectional LSTM network in Keras (20 units) and exported the model via
tfjs.converters.save_keras_model(model, 'myModel')
The model is 53kb large. In my JavaScript app, I load the model like this
var model;
async function loadModel() {;
model = await tf.loadModel('https://example.com/myModel.json');
}
and afterwards I run my predictions with
async function predict(input) {
var pred = model.predict(input);
...
}
It takes 5-6 seconds till the model is loaded, this is fine. But what bothers me is that every call of predict() also takes 5-6 seconds. Every time. For my use case, I'd need the prediction to be extremely fast, 1 second or less.
My question is: Is this normal? Or is something wrong with my code?
Edit: Here is a codepen: https://codepen.io/anon/pen/XygXRP
BTW, model.predict is blocking the UI - how can I prevent that?
javascript performance tensorflow tensorflow.js
javascript performance tensorflow tensorflow.js
edited Nov 15 at 20:47
asked Nov 14 at 22:02
Simon Nobel
72110
72110
On which device and in which browser do you run the model?
– Sebastian Speitel
Nov 14 at 22:56
It's a Surface Book 2, Intel i7 with 1.90GHz 2.11 GHz, 16GB RAM on Chrome (latest version)
– Simon Nobel
Nov 14 at 23:12
And the speed is vastly different when predicting in python?
– Sebastian Speitel
Nov 15 at 0:28
I just measured it, with model.predict() in python it takes roughly 300 milliseconds
– Simon Nobel
Nov 15 at 9:01
Could you please make a working snippet on codepen.io or share a gist ?
– edkeveked
Nov 15 at 10:04
|
show 2 more comments
On which device and in which browser do you run the model?
– Sebastian Speitel
Nov 14 at 22:56
It's a Surface Book 2, Intel i7 with 1.90GHz 2.11 GHz, 16GB RAM on Chrome (latest version)
– Simon Nobel
Nov 14 at 23:12
And the speed is vastly different when predicting in python?
– Sebastian Speitel
Nov 15 at 0:28
I just measured it, with model.predict() in python it takes roughly 300 milliseconds
– Simon Nobel
Nov 15 at 9:01
Could you please make a working snippet on codepen.io or share a gist ?
– edkeveked
Nov 15 at 10:04
On which device and in which browser do you run the model?
– Sebastian Speitel
Nov 14 at 22:56
On which device and in which browser do you run the model?
– Sebastian Speitel
Nov 14 at 22:56
It's a Surface Book 2, Intel i7 with 1.90GHz 2.11 GHz, 16GB RAM on Chrome (latest version)
– Simon Nobel
Nov 14 at 23:12
It's a Surface Book 2, Intel i7 with 1.90GHz 2.11 GHz, 16GB RAM on Chrome (latest version)
– Simon Nobel
Nov 14 at 23:12
And the speed is vastly different when predicting in python?
– Sebastian Speitel
Nov 15 at 0:28
And the speed is vastly different when predicting in python?
– Sebastian Speitel
Nov 15 at 0:28
I just measured it, with model.predict() in python it takes roughly 300 milliseconds
– Simon Nobel
Nov 15 at 9:01
I just measured it, with model.predict() in python it takes roughly 300 milliseconds
– Simon Nobel
Nov 15 at 9:01
Could you please make a working snippet on codepen.io or share a gist ?
– edkeveked
Nov 15 at 10:04
Could you please make a working snippet on codepen.io or share a gist ?
– edkeveked
Nov 15 at 10:04
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
Your UI is being blocked because you have not asked the thread to await the results of the prediction, this means that it is running synchronously instead of asynchronously. You can fix this by using the await keyword e.g. var pred = await model.predict(input).
The rest of your code appears to be fine and so it looks like the delay is coming from your actual model as I saw my CPU was barely taxed to run your model.
It is worth reading the tensorflowjs blog post as they give you examples of how you can improve the efficiency of models to make faster inferences in the browser.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Your UI is being blocked because you have not asked the thread to await the results of the prediction, this means that it is running synchronously instead of asynchronously. You can fix this by using the await keyword e.g. var pred = await model.predict(input).
The rest of your code appears to be fine and so it looks like the delay is coming from your actual model as I saw my CPU was barely taxed to run your model.
It is worth reading the tensorflowjs blog post as they give you examples of how you can improve the efficiency of models to make faster inferences in the browser.
add a comment |
up vote
0
down vote
Your UI is being blocked because you have not asked the thread to await the results of the prediction, this means that it is running synchronously instead of asynchronously. You can fix this by using the await keyword e.g. var pred = await model.predict(input).
The rest of your code appears to be fine and so it looks like the delay is coming from your actual model as I saw my CPU was barely taxed to run your model.
It is worth reading the tensorflowjs blog post as they give you examples of how you can improve the efficiency of models to make faster inferences in the browser.
add a comment |
up vote
0
down vote
up vote
0
down vote
Your UI is being blocked because you have not asked the thread to await the results of the prediction, this means that it is running synchronously instead of asynchronously. You can fix this by using the await keyword e.g. var pred = await model.predict(input).
The rest of your code appears to be fine and so it looks like the delay is coming from your actual model as I saw my CPU was barely taxed to run your model.
It is worth reading the tensorflowjs blog post as they give you examples of how you can improve the efficiency of models to make faster inferences in the browser.
Your UI is being blocked because you have not asked the thread to await the results of the prediction, this means that it is running synchronously instead of asynchronously. You can fix this by using the await keyword e.g. var pred = await model.predict(input).
The rest of your code appears to be fine and so it looks like the delay is coming from your actual model as I saw my CPU was barely taxed to run your model.
It is worth reading the tensorflowjs blog post as they give you examples of how you can improve the efficiency of models to make faster inferences in the browser.
answered Nov 25 at 13:14
zeros and ones
112
112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309394%2ftensorflow-js-speed-in-the-browser%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
On which device and in which browser do you run the model?
– Sebastian Speitel
Nov 14 at 22:56
It's a Surface Book 2, Intel i7 with 1.90GHz 2.11 GHz, 16GB RAM on Chrome (latest version)
– Simon Nobel
Nov 14 at 23:12
And the speed is vastly different when predicting in python?
– Sebastian Speitel
Nov 15 at 0:28
I just measured it, with model.predict() in python it takes roughly 300 milliseconds
– Simon Nobel
Nov 15 at 9:01
Could you please make a working snippet on codepen.io or share a gist ?
– edkeveked
Nov 15 at 10:04