Face-api.js: @tensorflow/tfjs-node is not correctly recognised when running examples

Created on 28 Apr 2019  路  7Comments  路  Source: justadudewhohacks/face-api.js

I am trying to run the NodeJS examples. The examples run, but seem to not properly utilize the TensorFlow C++ binding.

Steps to reproduce

cd face-api.js/examples/examples-nodejs
npm i

tsc faceDetection.ts
node faceDetection.js

Expected outcome:

Examples runs without warnings.

Actual outcome:

============================
Hi there 馃憢. Looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, which binds to TensorFlow C++, by running npm i @tensorflow/tfjs-node, or npm i @tensorflow/tfjs-node-gpu if you have CUDA. Then call require('@tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of your program. Visit https://github.com/tensorflow/tfjs-node for more details.
============================

Environment

Node: v11.14.0
face-api.js: 0.19.0
@tensorflow/tfjs-node: 1.0.2

Additional Informations

I tried running the examples with older versions to exclude the possibility of a problem with my setup. I managed to run the examples for face-api.js versions 0.17.0 and 0.17.1 with face-api.js picking up the TensorFlow bindings correctly.

Most helpful comment

face-api.js does not use tfjs-node at all. face-api.js is depending on tfjs-core. So in order to run face-api.js with the tfjs-node backend, you have to install a tfjs-node version, which is compatible with the tfjs-core version that face-api.js uses.

Ok, I think I got it now. However, this is valuable information and might be important for newcomers that try to get started with face-api.js in a Node.js environment.

Suggestion

Should we ...

  1. pin the tfjs-node version in the Node.js example, such that the example will run correctly on a freshly cloned repo and
  2. add a small comment somewhere in the Node.js example explaining that a compatible tfjs-node version is needed?

If no objections, I am happy to send PR for the above mentioned.

All 7 comments

Strange, can you provide the output of the following:

import tfnode from '@tensorflow/tfjs-node';
console.log(tfnode.version)
import tfnode from '@tensorflow/tfjs-node';
console.log(tfnode.version)

fails with

faceDetection.ts:5:8 - error TS1192: Module '"<some path>/face-api.js/examples/examples-nodejs/node_modules/@tensorflow/tfjs-node/dist/index"' has no default export.

I instead ran

import * as tfjsnode from '@tensorflow/tfjs-node';
console.log(tfjsnode.version)

Which yields

{ 'tfjs-core': '1.1.2',
  'tfjs-data': '1.1.2',
  'tfjs-layers': '1.1.2',
  'tfjs-converter': '1.1.2',
  tfjs: '1.1.2',
  'tfjs-node': '1.1.2' }

Edit: I had to rerun the Steps to reproduce and apparently it now installs a more recent version of tfjs (1.0.2 -> 1.1.2). However, the problem (Hi there 馃憢....) remains even with version 1.1.2.

Hmm, how about explicitly installing the correct tfjs-node version? npm i @tensorflow/[email protected]

Ok, after

npm i @tensorflow/[email protected]

the example runs correctly, without warning. The version output is:

{ 'tfjs-core': '1.0.4',
  'tfjs-data': '1.0.4',
  'tfjs-layers': '1.0.4',
  'tfjs-converter': '1.0.4',
  tfjs: '1.0.4',
  'tfjs-node': '1.0.2' }

However, now I am completely lost, what is happening here 馃 Does this mean I need to make sure face-api.js and the example use the exact same version of tfjs-node?

face-api.js does not use tfjs-node at all. face-api.js is depending on tfjs-core. So in order to run face-api.js with the tfjs-node backend, you have to install a tfjs-node version, which is compatible with the tfjs-core version that face-api.js uses.

Sorry for the confusion.

I Time Machine restored my repo to the state I had when I opened this issue. Now, I get

{ 'tfjs-core': '1.1.0',
  'tfjs-data': '1.1.0',
  'tfjs-layers': '1.1.0',
  'tfjs-converter': '1.1.0',
  tfjs: '1.1.0',
  'tfjs-node': '1.1.0' }

============================
Hi there 馃憢. Looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, which binds to TensorFlow C++, by running npm i @tensorflow/tfjs-node, or npm i @tensorflow/tfjs-node-gpu if you have CUDA. Then call require('@tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of your program. Visit https://github.com/tensorflow/tfjs-node for more details.
============================

done, saved results to out/faceDetection.jpg

Aha, so I actually used @tensorflow/[email protected], not @tensorflow/[email protected] as said in OP. Apologies! So, it looks like the problem arises when using @tensorflow/tfjs-node>=1.1.0.

Indeed after

@@ -2,7 +2,7 @@
   "author": "justadudewhohacks",
   "license": "MIT",
   "dependencies": {
-    "@tensorflow/tfjs-node": "^1.0.2",
+    "@tensorflow/tfjs-node": "~1.0.2",
     "canvas": "^2.0.1",
     "face-api.js": "../../"
   }

and

npm install

It now runs correctly 馃帀

{ 'tfjs-core': '1.0.4',
  'tfjs-data': '1.0.4',
  'tfjs-layers': '1.0.4',
  'tfjs-converter': '1.0.4',
  tfjs: '1.0.4',
  'tfjs-node': '1.0.3' }
cpu backend was already registered. Reusing existing backend
done, saved results to out/faceDetection.jpg

face-api.js does not use tfjs-node at all. face-api.js is depending on tfjs-core. So in order to run face-api.js with the tfjs-node backend, you have to install a tfjs-node version, which is compatible with the tfjs-core version that face-api.js uses.

Ok, I think I got it now. However, this is valuable information and might be important for newcomers that try to get started with face-api.js in a Node.js environment.

Suggestion

Should we ...

  1. pin the tfjs-node version in the Node.js example, such that the example will run correctly on a freshly cloned repo and
  2. add a small comment somewhere in the Node.js example explaining that a compatible tfjs-node version is needed?

If no objections, I am happy to send PR for the above mentioned.

Was this page helpful?
0 / 5 - 0 ratings