1

I'm trying to upload a pretrained Keras model into my existing tensorflow.js model and then making a simple prediction by passing a tensor in the correct format. I'm loading the model from my local drive within the project (in the assets folder)

export class MotionAnalysisComponent implements OnInit {

  model: tf.LayersModel;

  ngOnInit() {
        this.loadModel()
    };

  async loadModel() {
      this.model = await 
  tf.loadLayersModel('C:/Users/..path to folder../assets/tfjs_model/model.json');
      console.log('Prediction from loaded model:');
      this.model.predict(tf.ones([60,60,60]));
    }

I implemented a button in my html code, that should start the code and printing out the predictions in my console. But I'm getting the following errors:

  • Fetch API cannot load file:///C:/Users/..path to folder../assets/tfjs_model/model.json. URL scheme must be "http" or "https" for CORS request

  • ERROR Error: Uncaught (in promise): TypeError: Failed to fetch TypeError: Failed to fetch

DarkIudeX
  • 95
  • 1
  • 7
  • Possible duplicate of [Load Tensorflow js model from local file system in javascript](https://stackoverflow.com/questions/53639919/load-tensorflow-js-model-from-local-file-system-in-javascript) – edkeveked Sep 05 '19 at 07:23

2 Answers2

3

Assuming you are running this application in a browser you cannot access local file resources. You need to put your model.json file into a folder (for example "assets") inside your Angular project and load the model via http(s) protocol from there.

For example like this:

async loadModel() {
  this.model = await tf.loadModel('/assets/tfjs_model/model.json');
}
andypotato
  • 703
  • 5
  • 10
0

A few things to note:

Like model.save(), the loadLayersModel function takes a URL-like string argument that starts with a scheme. This describes the type of destination we are trying to load a model from.

The scheme(Scheme: http:// or https://) is followed by a path.

The url-like string can be replaced by an object that matches the IOHandler interface.

The tf.loadLayersModel() function is asynchronous.

The return value of tf.loadLayersModel is tf.Model