Tfjs: @tensorflow-models/mobilenet does not work under Node.js

Created on 28 Sep 2018  路  14Comments  路  Source: tensorflow/tfjs

Trying to use the @tensorflow-models/mobilenet package under Node.js throws the following exception:

2018-09-27 15:25:56.699190: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
(node:98265) Warning: N-API is an experimental feature and could change at any time.
(node:98265) UnhandledPromiseRejectionWarning: Error: browserHTTPRequest is not supported outside the web browser without a fetch polyfill.
at new BrowserHTTPRequest (/Users/kreeger/workspace/mn-test/node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:46:19)
at Object.browserHTTPRequest (/Users/kreeger/workspace/mn-test/node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:247:12)
at Object. (/Users/kreeger/workspace/mn-test/node_modules/@tensorflow/tfjs-layers/dist/models.js:98:50)
at step (/Users/kreeger/workspace/mn-test/node_modules/@tensorflow/tfjs-layers/dist/models.js:42:23)
at Object.next (/Users/kreeger/workspace/mn-test/node_modules/@tensorflow/tfjs-layers/dist/models.js:23:53)
at /Users/kreeger/workspace/mn-test/node_modules/@tensorflow/tfjs-layers/dist/models.js:17:71
at new Promise ()
at __awaiter (/Users/kreeger/workspace/mn-test/node_modules/@tensorflow/tfjs-layers/dist/models.js:13:12)
at Object.loadModelInternal (/Users/kreeger/workspace/mn-test/node_modules/@tensorflow/tfjs-layers/dist/models.js:92:12)
at Object.loadModel (/Users/kreeger/workspace/mn-test/node_modules/@tensorflow/tfjs-layers/dist/exports.js:16:21)
(node:98265) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
(node:98265) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

models node.js

Most helpful comment

I finally worked it out.

I use global.fetch = require('node-fetch'). In script.sh I change the loading mode script from file to https:

  mn.path = `https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_1.0_224/model.json`
  await mn.load()

Then I met with an error Error: Unknown feature TENSORLIKE_CHECK_SHAPE_CONSISTENCY.

And then , I bump tfjs to the latest release:

    "@tensorflow/tfjs": "^0.13.0",
    "@tensorflow/tfjs-node": "^0.1.19",

It works for me now.

All 14 comments

If people come across this issue, I found a (relatively) simple workaround using the node-fetch library from NPM. https://www.npmjs.com/package/node-fetch

const mobilenet = require('@tensorflow-models/mobilenet')
global.fetch = require('node-fetch')
const model = await mobilenet.load()

@jthomas hi, is this because the CPU is not supported in tf? would you please give the complete code about how to make node-fetch work? it should be inside the async function? thank you very much.

@dontmarryme this is because fetch does not exist by default on node. So adding this line

global.fetch = require('node-fetch')

To the top of your file after installing https://www.npmjs.com/package/node-fetch should do the trick

Thank you for the help! but still couldn't get it right, I followed everything here (hope I didn't miss anything):
http://jamesthom.as/blog/2018/08/07/machine-learning-in-node-dot-js-with-tensorflow-dot-js/

@tafsiri I put global.fetch = require('node-fetch') on top inside the script.js already, still don't work, by the way I am on Mac, does it make any difference?

@jthomas also I found I missed the downloading of mobilenet model part, it's pretty manual process, still trying to figure out what's there... is there a easier way to make mobilenet running with node.js?

this all my file
https://gitlab.com/greenhouseinfo/mobilenet-on-node.js

APPLEde-MacBook-Pro-2:mobilenet apple$ node script.js mobilenet/model.json panda.jpg
cpu backend was already registered. Reusing existing backend
2018-10-23 18:52:03.945011: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
(node:44077) UnhandledPromiseRejectionWarning: TypeError: Only HTTP(S) protocols are supported
at getNodeRequestOptions (/Users/apple/Desktop/mobilenet/node_modules/node-fetch/lib/index.js:1261:9)
at /Users/apple/Desktop/mobilenet/node_modules/node-fetch/lib/index.js:1322:19
at new Promise ()
at fetch (/Users/apple/Desktop/mobilenet/node_modules/node-fetch/lib/index.js:1319:9)
at BrowserHTTPRequest. (/Users/apple/Desktop/mobilenet/node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:163:40)
at step (/Users/apple/Desktop/mobilenet/node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:32:23)
at Object.next (/Users/apple/Desktop/mobilenet/node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:13:53)
at /Users/apple/Desktop/mobilenet/node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:7:71
at new Promise ()
at __awaiter (/Users/apple/Desktop/mobilenet/node_modules/@tensorflow/tfjs-core/dist/io/browser_http.js:3:12)
(node:44077) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
(node:44077) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

@dontmarryme fetch only works for http(s) protocol. It looks like you changed the protocol to file:// which wouldn't work with fetch. I'd say you can either go the route of loading the hosted model (and not changing the protocol to file) or following the tutorial code (which doesn't need node-fetch). Have you tried running the code from that tutorial as is (after downloading the necessary files)?

@tafsiri thanks for the reply, no, I haven't tried the tutorial, which tutorial link you are referring to? what I want is to run tfjs on node.js, actually I have no background for js so all are very confusing for me, thank you for your guide again!

@dontmarryme i was referring to the tutorial you linked above http://jamesthom.as/blog/2018/08/07/machine-learning-in-node-dot-js-with-tensorflow-dot-js

Have you tried it when everything is exactly similar to that tutorial? The code you posted has some differences.

I now follow the tutorial code loading models from file with file:// without node-fetch: "script.js model.json panda.jpg", but still getting error messages, I also have downloaded the models manually.

I have exactly the same issue with @dontmarryme . Even following the tutorial, I still meet with error browserHTTPRequest is not supported outside the web browser without a fetch polyfill..

I finally worked it out.

I use global.fetch = require('node-fetch'). In script.sh I change the loading mode script from file to https:

  mn.path = `https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_1.0_224/model.json`
  await mn.load()

Then I met with an error Error: Unknown feature TENSORLIKE_CHECK_SHAPE_CONSISTENCY.

And then , I bump tfjs to the latest release:

    "@tensorflow/tfjs": "^0.13.0",
    "@tensorflow/tfjs-node": "^0.1.19",

It works for me now.

@daisy-ycguo thank you for the replies! it works for me now, but do you know why there's still the compiling error messages?

2018-10-27 18:54:05.844070: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2 AVX AVX2 FMA
classification results: [ { className: 'running shoe', probability: 0.7699763774871826 },
{ className: 'sandal', probability: 0.2003212571144104 },
{ className: 'shoe shop, shoe-shop, shoe store',
probability: 0.013504834845662117 } ]

@dontmarryme That isn't a compile error - the TensorFlow library we ship with isn't fully optimized for your machine (but runs on all hardware). It is just a harmless warning - see https://github.com/tensorflow/tfjs/issues/571 for more info.

Thanks @daisy-ycguo that worked well. I am trying to make it work still with local models, did anyone make it work?
I get the same error message (node:20391) UnhandledPromiseRejectionWarning: TypeError: Only HTTP(S) protocols are supported when following the aforementioned tutorial.

@aperkaz I found this guy has modified the script and pointed out the modification!
https://sean12697.github.io/blog/2019/07/15/mobilenet-in-nodejs.html

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Josef-Haupt picture Josef-Haupt  路  3Comments

take-kuma picture take-kuma  路  3Comments

nsthorat picture nsthorat  路  3Comments

rlexa picture rlexa  路  3Comments

lastnod picture lastnod  路  3Comments