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

1

2 Answers 2

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');
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.