Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadFrozenModel does not work in node #759

Closed
DavidGOrtega opened this issue Oct 4, 2018 · 12 comments
Closed

loadFrozenModel does not work in node #759

DavidGOrtega opened this issue Oct 4, 2018 · 12 comments
Labels
comp:node.js question usage question or debugging support

Comments

@DavidGOrtega
Copy link

TensorFlow.js version

tfjs 0.13.1
tfjs-node 0.1.17
tfjs-converter 0.6.2

Browser version

node 10.5.0 on Windows 10

Describe the problem or feature request

Promise { <pending> }
(node:21296) UnhandledPromiseRejectionWarning: Error: browserHTTPRequest is not supported outside the web browser without a fetch polyfill.
    at new BrowserHTTPRequest (C:\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:46:19)
    at Object.browserHTTPRequest (C:\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:247:12)
    at FrozenModel.findIOHandler (C:\nodetfjs\node_modules\@tensorflow\tfjs-converter\dist\src\executor\frozen_model.js:99:38)
    at FrozenModel.<anonymous> (C:\nodetfjs\node_modules\@tensorflow\tfjs-converter\dist\src\executor\frozen_model.js:114:30)
    at step (C:\nodetfjs\node_modules\@tensorflow\tfjs-converter\dist\src\executor\frozen_model.js:32:23)
    at Object.next (C:\nodetfjs\node_modules\@tensorflow\tfjs-converter\dist\src\executor\frozen_model.js:13:53)
    at C:\nodetfjs\node_modules\@tensorflow\tfjs-converter\dist\src\executor\frozen_model.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (C:\nodetfjs\node_modules\@tensorflow\tfjs-converter\dist\src\executor\frozen_model.js:3:12)
    at FrozenModel.load (C:\nodetfjs\node_modules\@tensorflow\tfjs-converter\dist\src\executor\frozen_model.js:109:16)

Code to reproduce the bug / link to feature request

require('@tensorflow/tfjs-node');
const tf = require('@tensorflow/tfjs');
const tfcv = require('@tensorflow/tfjs-converter');

const URI           = 'file://C:/pix2pix/models/facades_A2B/tfjs/';
const MODEL_URL     = URI + 'tensorflowjs_model.pb';
const WEIGHTS_URL   = URI + 'weights_manifest.json';
const model         = tfcv.loadFrozenModel(MODEL_URL, WEIGHTS_URL);

you can try also with same results

const model         = tf.loadFrozenModel(MODEL_URL, WEIGHTS_URL);
@caisq
Copy link
Contributor

caisq commented Oct 4, 2018

Did you try global.fetch = require('node-fetch') (with dependency on node-fetch)?

@DavidGOrtega
Copy link
Author

Hi @caisq . Thanks for the reply!
Yeah that one was one of my initial try outs.
With the polyfill I can load from HTTP but not local models.

Only HTTP(S) protocols are supported

I 'm looking at the code. LoadfrozenModel is just

const model = new tf.FrozenModel(MODEL_URL, WEIGHTS_URL, {});
model.load()

However it depends on a handler that is going ALWAYS to be empty on node

const handlers = tfc.io.getLoadHandlers(path);
if (handlers.length === 0) {
        handlers.push(tfc.io.browserHTTPRequest(path, this.requestOption));
         }...
         
this.handler = this.handlers[0];

My options right now are
1 - Write my frozenModel that loads the model with it's own IOHandler
2 - Write just the fileLoader and polyfill tfc.io.getLoadHandlers to return my IOHandler
3 - Try to find a fetch polyfill that loads file://

What would you do?
;)

@caisq
Copy link
Contributor

caisq commented Oct 4, 2018

Sorry I overlooked an important problem in your code before. Please import tfjs-node after importing tfjs, i.e.:

const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');

@DavidGOrtega
Copy link
Author

DavidGOrtega commented Oct 4, 2018

@caisq I do it intentionally.

If I do it the other way around it does not fix the file:// issue and arises a nasty error:

Registration of backend tensorflow failed
TypeError: backend.setDataMover is not a function
cpu backend was already registered. Reusing existing backend
2018-10-04 18:22:19.784559: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Registration of backend tensorflow failed
TypeError: backend.setDataMover is not a function
    at Environment.registerBackend (C:\\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\environment.js:186:21)
    at Object.<anonymous> (C:\\nodetfjs\node_modules\@tensorflow\tfjs-node\dist\index.js:9:9)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\\nodetfjs\test2.js:5:1)
(node:28416) UnhandledPromiseRejectionWarning: Error: file://C:/models/pix2pix/models/facades_A2B/tfjs/tensorflowjs_model.pb not found. TypeError: Only HTTP(S) protocols are supported
    at BrowserHTTPRequest.<anonymous> (C:\\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:124:31)
    at step (C:\\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:32:23)
    at Object.throw (C:\\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:13:53)
    at rejected (C:\\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:5:65)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:746:11)
    at startup (internal/bootstrap/node.js:240:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:564:3)
(node:28416) 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: 3)
(node:28416) [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.
(node:28416) UnhandledPromiseRejectionWarning: TypeError: Only HTTP(S) protocols are supported
    at getNodeRequestOptions (C:\\nodetfjs\node_modules\node-fetch\lib\index.js:1261:9)
    at C:\\nodetfjs\node_modules\node-fetch\lib\index.js:1322:19
    at new Promise (<anonymous>)
    at fetch (C:\\nodetfjs\node_modules\node-fetch\lib\index.js:1319:9)
    at BrowserHTTPRequest.<anonymous> (C:\\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:137:36)
    at step (C:\\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:32:23)
    at Object.next (C:\\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:13:53)
    at C:\\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (C:\\nodetfjs\node_modules\@tensorflow\tfjs-core\dist\io\browser_http.js:3:12)
(node:28416) 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)

@caisq
Copy link
Contributor

caisq commented Oct 4, 2018

I see. I think you can remove @tensorflow/tfjs as a dependency from your package.json and keep @tensorflow/tfjs-node. You are running into these issues due to multiple versions of @tensorflow/tfjs being pulled in.

@DavidGOrtega
Copy link
Author

I can't!

"dependencies": {
    "@tensorflow/tfjs-node": "^0.1.17",
    "node-fetch": "^2.2.0",
  },
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
Error: Cannot find module '@tensorflow/tfjs'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
    at Function.Module._load (internal/modules/cjs/loader.js:520:25)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)

@nkreeger
Copy link
Contributor

nkreeger commented Oct 4, 2018

Try deleting the node_modules directory and retry npm install or yarn

@davidsoergel davidsoergel added question usage question or debugging support comp:node.js labels Oct 4, 2018
@DavidGOrtega
Copy link
Author

@nkreeger thanks for the reply. I tried it and documented it #739
I did swept the node-modules completely and just installed the @tensorflow/tfjs-node however did not worked for me. I needed to install the tfjs module.

@nkreeger
Copy link
Contributor

nkreeger commented Oct 4, 2018

@DavidGOrtega I think you have an old version of tfjs-node. Please try installing 0.1.18.

@DavidGOrtega
Copy link
Author

@nkreeger thanks, that solved the problem of issue #739 ! I'm impressed how fast new versions come out! mine was a couple of days old?

@jtoy
Copy link

jtoy commented Oct 6, 2018

nice, I ran into same thing and found this thread. think we can close this thread.

@rthadur
Copy link
Contributor

rthadur commented Nov 7, 2018

Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:node.js question usage question or debugging support
Projects
None yet
Development

No branches or pull requests

6 participants