ml5-library icon indicating copy to clipboard operation
ml5-library copied to clipboard

knnClassifier result after .save() and .load() - classIndex becomes undefined and label becomes number, labelToClassId disappears

Open ale24 opened this issue 2 years ago • 1 comments

Dear ml5 community,

I'm submitting a new issue. Please see the details below.

→ Step 1: Describe the issue 📝

KNNClassifier result after .save() and .load() in .classify() : classIndex becomes undefined and label becomes number

somehow before .save() and .load() on .addExample() stage - I getting result I want with .classify() in label key as expected

but after .save() and .load() to reveal same prediction I need to set k: Optional. Number. to 1 and by hands look all keys in confidencesByLabel to find predicted label, or used Object.keys(result.confidencesByLabel)[result.label] without setting k: Optional. Number. also give satisfaction

I checked knnClassifier and it looks like after .save() labelToClassId inside it disappears - I think this is root of my problem! exampleShape also disappears, maybe other data also have differences, extra check needed...

→ Step 2: Screenshots or Relevant Documentation 🖼

results before .save() - https://i.imgur.com/UrKYtE1.png results after .save() and .load() - https://i.imgur.com/sx3JJpg.png knnClassifier before/after .save() and .load() - https://i.imgur.com/fhANlyb.png

→ Describe your setup 🦄

used ml5.js version - https://unpkg.com/ml5@latest/dist/ml5.min.js saved model - https://assets.editor.p5js.org/6425ed4ef6508e001a82ec9f/779262d9-d5be-4d32-ae82-8c835a3d1fe4.json

ale24 avatar Apr 14 '23 19:04 ale24

another solution, maybe not valid in all situations, but it works in my

knnClassifier.load("model.json", function() {
    for (i in knnClassifier.getCount()) {
        knnClassifier.knnClassifier.labelToClassId[i] = parseInt(i);
    }
});

ale24 avatar Apr 14 '23 21:04 ale24