TFJS-Node: How to load model from url?

余生长醉 提交于 2019-12-23 14:55:52

问题


I want to load a model from an url in node.

This works in the broser:

mobileNet = await tf.loadModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json');

But not in node

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

I can do get request with node like this:

const https = require('https');
https.get(mobileNetUrl, (res) => {
    ...
});

The get request returns an Object with a modelTopology and a weightManifest. How could I create a model from it?


回答1:


You need to add this line to your code

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

tf.loadModel uses fetch under the hood. But fetch is not natively supported in nodeJs. That is why the pollyfill should be imported.



来源:https://stackoverflow.com/questions/52665923/tfjs-node-how-to-load-model-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!