tensorflowjs的WebGL后端很晚才加载模型(blazeface)



我正在尝试将blazeface模型添加到我的应用程序中。我刚刚发现,当我使用WebGL后端时,estimateFaces((的第一个调用

console.log("estimate started") 
const predictions = await model.estimateFaces(
this.refVideo.current,
returnTensors,
flipHorizontal,
annotateBoxes
);
console.log("estimate finished") // first time it is very slow with WebGL

大约需要18秒。WASM或CPU后端的情况并非如此。你也可以在运动面演示中感受到不同。从右上角菜单将后端设置为WebGL,然后打开您的摄像头。人脸检测的第一个掩码加载要比WASM或cpu后端加载晚得多。你知道为什么会这样吗?

张量如下:

if (predictions.length > 0) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < predictions.length; i++) {
if (returnTensors) {
predictions[i].topLeft = predictions[i].topLeft.arraySync();
predictions[i].bottomRight = predictions[i].bottomRight.arraySync();
if (annotateBoxes) {
predictions[i].landmarks = predictions[i].landmarks.arraySync();
}
}
try {
} catch (err) {
console.log(err.message);
}
this.portraitMode = false;
if (videoWidth < videoHeight && window.matchMedia("(orientation: portrait)").matches) {
this.portraitMode = true;
}
drawRoiOnCanvas(canvas, ctx, predictions[i], this.portraitMode, this.setDisableButtons)

}
}
requestAnimationFrame(this.renderPrediction);
}
};

我认为这个问题与Blazeface的权重加载到GPU所需的时间有关。我在导入Facemesh时遇到了同样的问题。每当我用wasm后端加载它,然后将后端更改为webgl时,它都会延迟大约5秒才能正常工作。对我有用的是在加载到Facemesh模型之前将后端设置为webgl

最新更新