Hello
1.7.2
Version 80.0.3987.163 (Official Build) Built on Ubuntu , running on Ubuntu 18.04 (64-bit)
I'm trying to load a model to use in a browser extension.
Ideally I'd like to package my neural network as a file within the extension, but loadLayersModel fails to fetch.
Is there a way to inline it ?
I tried loading them from a URL from a local http server, and it works in firefox but accounter a CORB issue on chrome.
bundle.js:23926 Uncaught (in promise) Error: Failed to parse model JSON of response from http://localhost:8000/model/parser/model.json. Please make sure the server is serving valid JSON for
bundle.js:41415 Cross-Origin Read Blocking (CORB) blocked cross-origin response http://localhost:8000/model/parser/model.json with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.
I also tried to run the https://github.com/tensorflow/tfjs-examples/tree/master/chrome-extension example and although the extension is correctly loaded no neural networks are running on the images.
Can you please advise ?
Thanks
Answering to my own question.
Using tf.io.browserFiles([fj, fb]) I can provide the files directly to tf.loadLayersModel
You can obtain those file by saving them with
model.save('downloads://my-model');
It is then just a question of fetching them locally :
//Do the same for the json file as for the binary data files below
//var fj = ...;
//
var fburl = browser.extension.getURL("model/mymodel.weights.bin");
fetch(fburl).then(response=> response.arrayBuffer() ).then(
buffer=>
{
var fb = new File([buffer],"mymodel.weights.bin", {type: 'application/octet-binary'});
tf.loadLayersModel(tf.io.browserFiles([fj, fb])).then( model=> /*Do work with model*/);
}
);
After further investigation, we in fact only need :
to set in manifest.json
"web_accessible_resources": [
"model/*"
]
tf.loadLayersModel( browser.extension.getURL("model/model.json")).then( model=> /* Do work with model */ );
And it works fine. It even give back an error message asking to set the web_accessible_resources if we forget it.
It was the first thing I tried, but I must have mis-typed something because I wasn't getting the web_accessible_resources message before.
Most helpful comment
After further investigation, we in fact only need :
to set in manifest.json
"web_accessible_resources": [
"model/*"
]
tf.loadLayersModel( browser.extension.getURL("model/model.json")).then( model=> /* Do work with model */ );
And it works fine. It even give back an error message asking to set the web_accessible_resources if we forget it.
It was the first thing I tried, but I must have mis-typed something because I wasn't getting the web_accessible_resources message before.