0

I am building an npm package that loads a pretrained tensorflow model and makes some prediction.

When i do tf.loadLayersModel() with tf = require('@tensorflow/tfjs-node') i have no problem. Unfortunately, that library seem problematic upon installation. I tried using instead tf = require('@tensorflow/tfjs). Although, i get the following error:

UnhandledPromiseRejectionWarning: Error: browserHTTPRequest is not supported outside the web browser without a fetch polyfill.

This is my code:

 // Load ML model
  tf.loadLayersModel("file://" + __dirname + "mymodel/model.json").then(
    model => {
      }
    }
  );

Is there a way i can work this around?.

2 Answers2

1

UnhandledPromiseRejectionWarning: Error: browserHTTPRequest is not supported outside the web browser without a fetch polyfill.

You need a polyfill for fetch if you're using @tensorflow/tfjs instead of @tensorflow/tfjs-node

global.fetch = require('node-fetch');
edkeveked
  • 14,876
  • 8
  • 45
  • 80
  • Added that and now i get this error: Error: Request for file://D:\Users\panos\Desktop\eslint-generator/mymodel/model.json failed due to error: TypeError: Only HTTP(S) protocols are supported – Panagiotis Sakkis Apr 03 '19 at 15:37
  • You have to serve the model files using a local server since you are not using node binding. Consider this answer : https://stackoverflow.com/questions/53639919/load-tensorflow-js-model-from-local-file-system-in-javascript – edkeveked Apr 03 '19 at 15:54
1

It is better to use @tensorflow/tfjs-node when you working on Node.js. Just don't require @tensorflow/tfjs and everything will work as expected.

But if you really want to only use @tensorflow/tfjs, then just add tfjs-node-save

npm install --save tfjs-node-save

and use it in your script

require('tfjs-node-save');
CoderFF
  • 139
  • 1
  • 9