Giter Site home page Giter Site logo

Comments (3)

MarcoSteinke avatar MarcoSteinke commented on May 27, 2024

Hello,
here is the code for better readability, so people can read it without cloning and formatting 😄

curImg = 0;
imgCount = 1000;
featureExtractor = ml5.featureExtractor("MobileNet", function() {
    classifier = featureExtractor.classification();
    addSamplesEach();
});

function addSamplesEach() {
    if (curImg >= imgCount) {
        return;
    }
    let img = new Image();
    img.crossOrigin = "Anonymous";
    img.onload = function() {
        console.log('added...');
        const img = this;
        classifier.addImage(img, "label", function() {
            curImg++;
            addSamplesEach();
        });
    };
    img.src = "https://placekitten.com/100/100";
    img = null;
}

from ml5-library.

sproutleaf avatar sproutleaf commented on May 27, 2024

Hi! Thank you for opening the issue with us and sorry it took a while for us to get back to you.

WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost happens when a memory leak occurs because the browser has lost control of your GPU. This is technically not a ml5.js bug, but more performance-related. Given that the image is 100x100 (low resolution), even running it 1000 times should not be a big problem. The only suggestion I can think of is this: instead of running a loop, you're invoking addSamplesEach() in the middle of its execution, which might result in multiple executions at the same time. Rewriting your code in a straightforward loop might be helpful.

If not, please stick to the two solutions you listed.

Miaoye

from ml5-library.

ale24 avatar ale24 commented on May 27, 2024

The reason I was "invoking addSamplesEach() in the middle of its execution" exactly in step-by-step iteration with JS instead of "multiple executions at the same time".

So rewriting code in a straightforward loop also not works. The only solution still — using CPU as Backend.

I also tested same with Python tf + MobileNet and it works with GPU without any problems, so I can't figure out where is a problem in usage GPU with ml5.js.

But maybe I only person who got this "black-screen" issue and error with this test code, I don't know.

Screenshot

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/addons/p5.sound.min.js"></script>
    <script src="https://unpkg.com/ml5@latest/dist/ml5.min.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <meta charset="utf-8" />
  </head>
  <body>
    <main></main>
    <script>
      curImg = 0;
      imgCount = 1000;
      featureExtractor = ml5.featureExtractor("MobileNet", function() {
          classifier = featureExtractor.classification();
          for (let i = 0; i < imgCount; i++) {
              let img = new Image();
              img.crossOrigin = "Anonymous";
              img.onload = function() {
                  console.log('added...');
                  const img = this;
                  classifier.addImage(img, "label", function() {
                      curImg++;
                  });
              };
              img.src = "https://placekitten.com/100/100";
              img = null;
          }
      });
    </script>
  </body>
</html>

from ml5-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.