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?










share|improve this question
























  • 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

















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?










share|improve this question
























  • 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















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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 at 13:14









        zeros and ones

        112




        112






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?