Ml5-library: KNN: export the model

Created on 7 Feb 2018  路  11Comments  路  Source: ml5js/ml5-library

The KNNImageClassifier has a function to import a model (loadModel(model)). Is there any way to export the trained model so I can import it later?

Most helpful comment

99 closes this. Now this is possible:

const knn = new ml5.KNNImageClassifier();

// Will prompt the user to save a json file
// Accepts one arg (the name of the file or defaults to Date.now())
knn.save(); 

// Loads a json file that was saved with .save() 
knn.load('sometrainedmodel.json'); 

All 11 comments

Unfortunately, that's something that deeplearn.js needs to handle.
This issue in the deeplearn repo is following that.

@matamalaortiz made a quick fix where he pre saved a couple of images and when the page loaded, used those images to train the classifier.

As a reference, @matamalaortiz made it into a small package: https://github.com/matamalaortiz/ml5-recorded-videos-training

Closing this for now

Perhaps we should open a new issue to track or revisit later? I do think having a mechanism in the library via a saved JSON file and/or localStorage is worth researching. I'm pretty sure the KNNImageClassifier does not alter the original SqueezeNet or MobileNet model so we should just be able to save just the feature vectors that come out of each training image themselves. @nsthorat do you know if this is correct off the top of your head?

@jameswex who did something similar.

We released a new version of KNN image classifier which has two methods, getClassLogitsMatrices() and setClassLogitsMatrics() which you can use to serialize and deserialize. This /just/ landed in the latest version of KNN image classifier, so update your package dep :)

Let me know if you have any more questions.

awesome! We will update then!

99 closes this. Now this is possible:

const knn = new ml5.KNNImageClassifier();

// Will prompt the user to save a json file
// Accepts one arg (the name of the file or defaults to Date.now())
knn.save(); 

// Loads a json file that was saved with .save() 
knn.load('sometrainedmodel.json'); 

why when i save the ml5.js classifier show error,
no function called save

why when i save the ml5.js classifier show error,
no function called save

I have the same problem.
[email protected]
"_TypeError: knnClassifier.save is not a function_"

Please try version 0.2.1, save() is only available as of this version. Let us know if that works!

Please try version 0.2.1, save() is only available as of this version. Let us know if that works!

I'm not lucky enough to get it works.
""
error on console is gone. But network error comes, when i save more than 45 examples.... I'm not able to get the knn json file.

Thanks for the work. It is an amazing Library.

Yes, save() should work with 0.2.1, but the network error still exists for large datasets in this example. Follow #270 and #271, hopefully we'll get this in a new release soon! You can also see this code I published with a temporary fix: https://github.com/CodingTrain/website/blob/master/Courses/beginner_ml5/07_knn_classifier/sketch.js

Thank you!!

Was this page helpful?
0 / 5 - 0 ratings