face-api.js - Why is browser's faceapi.detectAllFaces() is faster than server's?
Asked Answered
S

3

6

I want to use face detection on my server-side. Therefore, I found face-api.js for this task. I discovered that each call of faceapi.detectAllFaces() lasts for ~10 seconds. But when I start the browser-example, only the first function lasts 10 seconds and all the next lasts less than one second.

My server-side code (you can see a similar code in ageAndGenderRecognition.ts):

import * as faceapi from 'face-api.js';
import { canvas, faceDetectionNet, faceDetectionOptions, saveFile } from './commons';
await faceDetectionNet.loadFromDisk('../../weights')
await faceapi.nets.faceLandmark68Net.loadFromDisk('../../weights')
await faceapi.nets.ageGenderNet.loadFromDisk('../../weights')

const img = await canvas.loadImage('../images/bbt1.jpg')

console.time();
const results = await faceapi.detectAllFaces(img, faceDetectionOptions);
// ~10 seconds.
console.timeEnd();

console.time();
const results2 = await faceapi.detectAllFaces(img, faceDetectionOptions);
// ~10 seconds again.
console.timeEnd();

Why faceapi.detectAllFaces() (except first call) is faster in browser-example than in ageAndGenderRecognition.ts? And which similar thing I can to do to my faceapi.detectAllFaces()-function has the same speed?

Swayne answered 21/5, 2019 at 20:29 Comment(0)
S
7

There might be some reasons why your nodejs sample code runs for 10s:

  1. You are not importing @tensorflow/tfjs-node at all, in this case tfjs does not use the native Tensorflow CPU backend and operations will take much longer on the CPU.

  2. You are importing @tensorflow/tfjs-node but there is a version mismatch between the tfjs-core version that face-api.js and the version of @tensorflow/tfjs-node your have installed via npm. In this case tfjs will display a warning message.

  3. Everything is set up correctly, but your CPU is just tremendously slow. In this case you can either try to use @tensorflow/tfjs-node-gpu (if you have a CUDA compatible nvidia GPU) or you can change the faceDetectionOptions to new faceapi.TinyFaceDetectorOptions(), which will run the TinyFaceDetector instead of the default SSD Mobilenet v1 model, which is much faster.

The reason why the first call in the browser takes that long is not due to the actual prediction time. It is because using the WebGL backend of tfjs, on the first run (warm up run) all the shader programs are compiled, which takes so long. Afterwards these are cached. The prediction in the browser takes only a few miliseconds because the WebGL backend is GPU accelerated. The 10s warm up time in the browser and the prediction time you are seeing in nodejs are not related at all.

Standin answered 27/5, 2019 at 7:54 Comment(0)
J
2

Tensorflow.js will generally perform better when using a GPU (instead of a CPU).

So one thing that can explain the performance difference is that on the browser side, tensorflow will run on the GPU (via WebGL), whereas on node, it will run on the CPU (unless you are using @tensorflow/tfjs-node-gpu).

It seems that by default, the face-api.js library uses @tensorflow/tfjs-node (https://github.com/justadudewhohacks/face-api.js#face-apijs-for-nodejs). So maybe you can try to replace the import with @tensorflow/tfjs-node-gpu.

In order to use the GPU on node, check the tfjs-node github : https://github.com/tensorflow/tfjs-node

Josuejosy answered 24/5, 2019 at 6:51 Comment(2)
I have tried, but it's not working for me. I think because I haven't GPU on my computer.Swayne
I want to notice: only FIRST call of detectAllFaces lasts 10 sec in browser-example. And ALL NEXT - less than 0.3 sec. But ALL calls in nodejs-example lasts 10 sec.Swayne
A
1

As @justadudewhohacks mentioned , my Node.js code was running very slow and what solved it for me was simply adding this line to the top of the file:

import '@tensorflow/tfjs-node';

It is faster by a huge margin. If you have a NVIDIA graphics card you could also try importing

   import '@tensorflow/tfjs-node-gpu';
Antherozoid answered 24/9, 2019 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.