Load Tensorflow js model from local file system in javascript

前端 未结 7 1201
一个人的身影
一个人的身影 2020-11-30 10:31

I have converted a keras model to tensorflow json format and saved it locally in my computer. I am trying to load that json model in a javascript code using the below comman

相关标签:
7条回答
  • 2020-11-30 11:14

    If you are trying to load it in server side, use @tensorflow/tfjs-node instead of @tensorflow/tfjs and update to 0.2.1 or higher version to resolve this issue.

    0 讨论(0)
  • 2020-11-30 11:19

    You could use insecure chrome instance:

    C:\Program Files (x86)\Google\Chrome\Application>chrome.exe --disable-web-security --disable-gpu --user-data-dir=C:/Temp
    

    Than you could add this script to redefine fetch function

    async function fetch(url) {
      return new Promise(function(resolve, reject) {
        var xhr = new XMLHttpRequest
        xhr.onload = function() {
          resolve(new Response(xhr.responseText, {status: 200}))
        }
        xhr.onerror = function() {
          reject(new TypeError('Local request failed'))
        }
        xhr.open('GET', url)
        xhr.send(null)
      })
    }
    

    After that be shure that you use the right model loader my comment about loader issue

    BUT your weights will be incorrect - as I understand there are some encoding problems.

    0 讨论(0)
  • 2020-11-30 11:21

    Check out our documentation for loading models: https://js.tensorflow.org/api/latest/#Models-Loading

    You can use tf.loadModel takes a string which is a URL to your model definition which needs to get served over HTTP. This means you need to start an http-server to serve those files (it will not allow you to make a request to your filesystem because of CORS).

    This package can do that for you: npmjs.com/package/http-server

    0 讨论(0)
  • 2020-11-30 11:30

    I know you're trying to load your model in a browser but if anybody lands here that's trying to do it in Node, here's how:

    const tf = require("@tensorflow/tfjs");
    const tfn = require("@tensorflow/tfjs-node");
    const handler = tfn.io.fileSystem("./path/to/your/model.json");
    const model = await tf.loadModel(handler);
    
    0 讨论(0)
  • 2020-11-30 11:30

    LoadModel uses fetch under the hood. And fetch cannot access the local files directly. It is meant to be used to get files served by a server. More on this here. To load a local file with the browser, there is two approaches, asking the user to upload the file with

    <input type="file"/>
    

    Or serving the file by a server.

    In these two scenarios, tf.js provides way to load the model.

    1. Load the model by asking the user to upload the file

    html

    <input type="file" id="upload-json"/>
    <input type="file" id="upload-weights"/>
    

    js

    const uploadJSONInput = document.getElementById('upload-json');
    const uploadWeightsInput = document.getElementById('upload-weights');
    const model = await tfl.loadModel(tf.io.browserFiles(
     [uploadJSONInput.files[0], uploadWeightsInput.files[0]]));
    
    1. Serving the local files using a server

    To do so, one can use the following npm module http-server to serve the directory containing both the weight and the model. It can be installed with the following command:

     npm install http-server -g
    

    Inside the directory, one can run the following command to launch the server:

    http-server -c1 --cors .
    

    Now the model can be loaded:

     // load model in js script
     (async () => {
       ...
       const model = await tf.loadFrozenModel('http://localhost:8080/model.pb', 'http://localhost:8080/weights.json')
     })()
    
    0 讨论(0)
  • 2020-11-30 11:30

    You could try:

    const model = await tf.models.modelFromJSON(myModelJSON)
    

    Here it is in the tensorflow.org docs

    0 讨论(0)
提交回复
热议问题