Reputation: 105
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
Upvotes: 1
Views: 853
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
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