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

"response.arrayBuffer is not a function" while loadLayersModel (Node.js) #2029

Closed
mrqo opened this issue Sep 12, 2019 · 12 comments
Closed

"response.arrayBuffer is not a function" while loadLayersModel (Node.js) #2029

mrqo opened this issue Sep 12, 2019 · 12 comments
Assignees
Labels
comp:converter type:bug Something isn't working

Comments

@mrqo
Copy link

mrqo commented Sep 12, 2019

TensorFlow.js version

1.2.9

Browser version

any

Describe the problem or feature request

I have exported a Keras model written in Python, then converted it with tensorflowjs_converter and uploaded it to server. Then, I get error while loading it. I have tried it with @tensorflow/tfjs-node' as well as @tensorflow/tfjs'.

TypeError: response.arrayBuffer is not a function at f:\Developer\x\node_modules\@tensorflow\tfjs-core\dist\io\weights_loader.js:97:90 at Array.map (<anonymous>) at Object.<anonymous> (f:\Developer\x\node_modules\@tensorflow\tfjs-core\dist\io\weights_loader.js:97:48) at step (f:\Developer\x\node_modules\@tensorflow\tfjs-core\dist\io\weights_loader.js:48:23) at Object.next (f:\Developer\x\node_modules\@tensorflow\tfjs-core\dist\io\weights_loader.js:29:53) at fulfilled (f:\Developer\x\node_modules\@tensorflow\tfjs-core\dist\io\weights_loader.js:20:58) at process._tickCallback (internal/process/next_tick.js:68:7)

Code to reproduce the bug / link to feature request

File is publicly available, bin file also resides there.
const tf = require('@tensorflow/tfjs-node');
model = await tf.loadLayersModel('https://storage.googleapis.com/valid-ship-252510.appspot.com/1568271407928-tfjs_model_1/model.json');

@rthadur rthadur self-assigned this Sep 12, 2019
@rthadur
Copy link
Contributor

rthadur commented Sep 12, 2019

@kqtus getting a CORS exception while loading , can you please check.

@mrqo
Copy link
Author

mrqo commented Sep 12, 2019

@rthadur hmm, strange. I tried loadLayersModel and through Postman on both model.json and bin on another PC, and files seem to download without CORS exceptions.

-- Edit 1.
IDK if that'll help, but I logged the response I get - no arrayBuffer() indeed.

image

-- Edit 2.
I bypassed this problem by downloading all files manually and providing file:// url.

@mrqo
Copy link
Author

mrqo commented Sep 17, 2019

Any estimations on when it will be fixed?

From what I understand, Node.js doesn't support arrayBuffer(). You'd need to change this:

weights_loader.ts, line 60:
const bufferPromises = responses.map(response => response.arrayBuffer());

to this:
const bufferPromises = responses.map(response => response.buffer());

@rthadur
Copy link
Contributor

rthadur commented Sep 17, 2019

@kangyizhang any thoughts on above comments from @kqtus ?

@mrqo
Copy link
Author

mrqo commented Sep 17, 2019

Ok I think I solved it the proper way, having replaced global fetch.

const nodeFetch = require('node-fetch');
global.fetch = nodeFetch;

It is strange though, because previously I tried passing it as a fetchFunc, but it didn't trigger.

@rthadur
Copy link
Contributor

rthadur commented Sep 17, 2019

thanks for the update , closing this issue.

@rthadur rthadur closed this as completed Sep 17, 2019
@kangyizhang
Copy link
Contributor

tf.loadLayersModel() should automatically use node-fetch when it's in Node environment. @dsmilkov any thoughts why .arrayBuffer() is not available?

@dsmilkov
Copy link
Contributor

dsmilkov commented Sep 18, 2019

@kqtus We'd like to understand why you had to manually do node-fetch. In Node.js, we do override the global fetch internally so the user doesn't have to. I'm unable to reproduce the bug.

Can you provide a bit more info:

  • What version of node you are using?
  • We don't seem to have access to the model.json on GCS (Access denied). If you can share the model files publicly, that would be great.
  • Can you share the error you are getting by simply calling tf.loadLayersModel(url), without specifying any options like fetchFunc?

Thank you!

@dsmilkov
Copy link
Contributor

dsmilkov commented Sep 18, 2019

Another idea. Can you double-check the node-fetch version you had installed before? It is possible that you had manually installed node-fetch 1.x which lacks body.arraybuffer(), while tfjs depends on node-fetch 2.x.

@mrqo
Copy link
Author

mrqo commented Sep 21, 2019

@dsmilkov

@kangyizhang
Copy link
Contributor

@kqtus can you provide your node version and which operating system you are using?

@mrqo
Copy link
Author

mrqo commented Sep 22, 2019

@kangyizhang Node v10.16.3, Windows 10 1903. Here's also package.json: https://pastebin.com/swpTke2q.

One additional thing came to my mind - we are having frontend app and backend in the same project (Express + React). Frontend is being transpiled and backend is run as-is - maybe that causes ambiguity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:converter type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants
@dsmilkov @kangyizhang @caisq @mrqo @rthadur and others