DarkIudeX
DarkIudeX

Reputation: 105

How to properly upload a pretrained model in angular with tensorflow.js?

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:

Upvotes: 1

Views: 853

Answers (2)

Alireza Abolhasani
Alireza Abolhasani

Reputation: 25

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

Upvotes: 0

andypotato
andypotato

Reputation: 722

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');
}

Upvotes: 3

Related Questions