vladmandic / face-api

FaceAPI: AI-powered Face Detection & Rotation Tracking, Face Description & Recognition, Age & Gender & Emotion Prediction for Browser and NodeJS using TensorFlow/JS
https://vladmandic.github.io/face-api/demo/webcam.html
MIT License
824 stars 149 forks source link

How to use faceapi in nodejs with tfjs-backend-cpu? #141

Closed zczcyc0201 closed 1 year ago

zczcyc0201 commented 1 year ago

Dear author, hello, i want to use your face-api in nodejs with tfjs-backend-cpu, i don't want to install tfjs-node, it's too large,

example:

const tfjs = require('@tensorflow/tfjs'); const faceapi = require('@vladmandic/face-api/dist/face-api.js');

I got this error:

TypeError: this.util.TextEncoder is not a constructor

Do you have any good suggestions? Thanks very much , very good project!

vladmandic commented 1 year ago

that's why there are different versions in /dist - you're loading browser package and that doesn't work in node.

also, if you cannot or don't want to use tfjs-node, i strongly suggest to use wasm backend
its 100x faster than cpu backend (which is not supported for anything but testing).

for example, see https://github.com/vladmandic/face-api/blob/master/demo/node-wasm.js

but...if you really want, you can - to use tfjs-backend-cpu, simply remove tfjs-backend-wasm from demo and set backend to cpu.

zczcyc0201 commented 1 year ago

Thank you very much for your help. I solved my problem!

zczcyc0201 commented 1 year ago

One more question , when I use tfjs-wasm , a lot of memory is used , about 500m used when i run the face detector, if i set

await tfjs.setBackend('cpu');

Even the same usage scenario, same images, it uses only 80m memory,

do you know why?

vladmandic commented 1 year ago

how are you measuring memory usage?

if i compare cpu to wasm backends, difference is not huge:

CPU:  { rss: 346742784, heapTotal: 61120512, heapUsed: 32789072, external: 111902914, arrayBuffers: 100428394 }
WASM: { rss: 497676288, heapTotal: 60334080, heapUsed: 35357856, external: 269790206, arrayBuffers: 10196390 }

wasm uses more external memory while cpu uses more buffers - both make total sense. at the end, its not a huge difference.

and if i try to limit js heap space allocation which triggers more aggresive garbage collection by v8 engine inside nodejs, heap memory usage goes down a little bit but the rest is pretty constant

CPU: { rss: 325632000, heapTotal: 42246144, heapUsed: 26836512, external: 112103618, arrayBuffers: 100629098 }
WASM: { rss: 462467072, heapTotal: 34119680, heapUsed: 28830160, external: 271162929, arrayBuffers: 10196889 }

also, running with 100 iterations results in extrernal memory growth due to internal buffering, but it stabilizes relatively early and there is no leak.