简体   繁体   中英

Memory leak while using posenet in tensorflow JavaScript

I am trying to experiment tensorflow using javascript. I want to run the posenet model over a folder of images. With some googling I have written the HTML below. When I run the HTML shown below the system is running out of memory soon, indeed there is a memory leak. I don't know where and how the memory leak is happening. Can community please explain how the memory leak is happening and how to get around it

<html>
  <head>
    <script src="https://unpkg.com/@tensorflow/tfjs"></script>
    <script src="https://unpkg.com/@tensorflow-models/posenet"</script>
 </head>

  <body>
    <img id='faceImg' src='1.0.jpg'/>

  <script>
      var i = 1;
      async function makeTensors(){
            var psData;
           var imageScaleFactor = 0.5;
           var outputStride = 16;
           var flipHorizontal = false;
          var iterator;
           var imageElements = document.getElementById('faceImg');

       console.log(imageElements);

      let result = await posenet.load().then(function(net){
        return net.estimateSinglePose(imageElements, imageScaleFactor, flipHorizontal, outputStride)
      }).then(function(pose){
        console.log(pose);
        psData = pose;
      })

       var im_tensor_pos = []
       for (iter = 0; iter < 17; iter++) {
         im_tensor_pos.push(psData.keypoints[iter].position.x);
         im_tensor_pos.push(psData.keypoints[iter].position.y);
       }
       for (iter = 0; iter < 17; iter++) {
         im_tensor_pos.push(psData.keypoints[iter].score);
       }
       im_tensor_pos.push(psData.score);
      //  tensor_vals.push(im_tensor_pos);
       psData = null;
       delete result;
      // console.log(tensor_vals);
    }

    async function test(){
      for (i = 1; i < 270; i++) {
        var ImageSrc = 'results/' + i + ".0.jpg";
        console.log(ImageSrc)
        var imageElements = document.getElementById('faceImg');
        imageElements.src = ImageSrc;

        result = await makeTensors();
        delete result;
      }
    }
    test().then(console.log('done'));

   </script>
  </body>
  </html>

This is my first attempt at js too. Please let me know if your thoughts on code too.

I had the exact same problem, but .dispose didn't work for me.

Don't know if you fixed it already, but for future references this worked:

tf.engine().startScope()

// do the Tensorflow / Posenet stuff

tf.engine().endScope()

This will destroy the tensor once finished

I'm not seeing any space where you're disposing your tensors. Remember if you're not calling .dispose on a tensor you're done with, it's still going to sit in memory.

Dispose logic: https://js.tensorflow.org/api/latest/#dispose

For debugging the memmory issue, please look at using tf.memory to see where the issue might be building: https://js.tensorflow.org/api/latest/#memory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM