Tfjs: loadLayersModel in browser extension

Created on 18 Apr 2020  路  2Comments  路  Source: tensorflow/tfjs

Hello

TensorFlow.js version

1.7.2

Browser version

Version 80.0.3987.163 (Official Build) Built on Ubuntu , running on Ubuntu 18.04 (64-bit)

Describe the problem or feature request

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

others

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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings