本文介绍了tf.browser.fromPixel(video) 导致内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 tf.browser.fromPixels(video) 从视频中获取当前帧数据,但是每次调用此函数时,都会出现内存泄漏.TensorFlow.js 版本

I wanted to get the current frame data from a video using tf.browser.fromPixels(video), but every time this function is called, there is a memory leak.TensorFlow.js version

1.3.1浏览器版本Chrome 版本 75.0.3770.142这是导致内存泄漏的代码.

async function drawScreen () {

    console.log(tf.memory());

    var frame = tf.tidy( () => {

    return  tf.browser.fromPixels (videoElement, 3).expandDims(0).toFloat().div(tf.scalar(255))

    });

    console.log(tf.memory());

    hr_image_0 = tf.tidy( () => {

    return net.execute(frame).squeeze().clipByValue(0,1);

    });

    tf.dispose(frame);

    console.log(tf.memory());

    tf.browser.toPixels(hr_image_0, ccanvas);

    await tf.tidy( () =>  {

     tf.browser.toPixels(hr_image_0, ccanvas);

    });

    console.log(tf.memory());

}

我用过

请求动画帧()

调用 drawScreen 函数.在Chrome上运行代码,控制台打印如下

to call the drawScreen function.Running the code on Chrome, the following is printed on the console

对象{不可靠:错误,numBytesInGPU:210113944,numTensors:172,numDataBuffers:172,numBytes:211660580}

CH6EX8.html:129:11 对象 {不可靠:假,numBytesInGPU:210344344,numTensors:173,nnumDataBuffers:173,numBytes:211833380 }

CH6EX8.html:129:11 Object {unreliable: false, numBytesInGPU:210344344, numTensors: 173,nnumDataBuffers: 173, numBytes: 211833380 }

CH6EX8.html:135:11 对象 {不可靠:假,numBytesInGPU:212187544, numTensors: 173, numDataBuffers: 173, numBytes: 213215780 }

CH6EX8.html:135:11 Object {unreliable: false, numBytesInGPU:212187544, numTensors: 173, numDataBuffers: 173, numBytes: 213215780 }

CH6EX8.html:141:11 对象 { 不可靠:假,numBytesInGPU:213745048,numTensors:173,numDataBuffers:173,numBytes:213215780 }

CH6EX8.html:141:11 Object { unreliable: false, numBytesInGPU:213745048, numTensors: 173, numDataBuffers: 173, numBytes: 213215780 }

有什么办法可以消除泄漏.

Is there any way to remove the leak.

推荐答案

根据文档:

执行提供的函数 fn 并在执行后清除 fn 分配的所有中间张量,除了 fn 返回的那些张量

tf.tidy 确实删除了中间张量.但在这种情况下,没有任何中间张量要删除.当您不断调用 drawscreen 方法时,缓冲区将不断增加,该方法将始终返回 tf.browser.fromPixels (videoElement, 3).expandDims(0).toFloat().div(tf.scalar(255)) 从而将缓冲区分配给内存中的张量创建.

tf.tidy does remove intermediate tensor. But in this case there is not any intermediate tensor to remove. And the buffer will keep increasing as you keep calling the drawscreen method which will always return tf.browser.fromPixels (videoElement, 3).expandDims(0).toFloat().div(tf.scalar(255)) thus keeping the buffer allocated to the creation of the tensor in memory.

要清理未使用的张量,调用 drawScreen 的方法应包含在 tf.tidy 中,以处理从图像创建张量时使用的张量.

To clean the unused tensor, the method calling drawScreen should be enclosed in tf.tidy to dispose the tensor used when creating the tensor from image.

由于 drawScreen 是由 requestAnimation 调用的,所以 tf.tidy 可以是其中的一个顶层函数.

Since drawScreen is called by requestAnimation, tf.tidy can be a toplevel function inside it.

async function drawScreen () {
  tf.tidy(() => {
   // do all operations here
  })
}

另一种选择是使用 tf.dispose 在图像被绘制到屏幕后处理 framehr_image_0>

The other option would be to use tf.dispose to dispose frame and hr_image_0 after the image has been drawn to the screen

await tf.browser.toPixels(hr_image_0, ccanvas);
tf.dispose(hr_image_0)

这篇关于tf.browser.fromPixel(video) 导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:01