How to make predictions with exported model from Classification Learner in App Designer?

4 Ansichten (letzte 30 Tage)
Hello,
as the titel says, I am not able to make predictions with the exported model from Classification Learner in App Designer. It works fine when typing the prompts in command window, but not in app designer.
These are the prompts i used for command window:
>> load trainedModel.mat
>> imageFeatures = analyzer.getImageFeatures(47)
>> predictedLabels = trainedModel.predictFcn(imageFeatures)
and it works just fine, when trying this in app designer it doesn't work:
% this is in properties
trainedModel = load('trainedModel.mat')
% this is in button callback function
index = randomDataset.getId;
imageFeatures = app.analyzer.getImageFeatures(index);
predictedLabel = app.trainedModel.predictFcn(imageFeatures);
I get this error:
Unrecognized field name "predictFcn".
I think it doesn't load the model properly, but I don't know why or how else to load the trained model. I appreciate any help I can get.
Thanks in advance.

Akzeptierte Antwort

Rahul
Rahul am 28 Feb. 2023
Bearbeitet: Rahul am 1 Mär. 2023
This is because you have given an output argument to "load" function. When the output variable is assigned to the function, it loads the saved contents of the MAT file into a structure array. For e.g.
saved_model = load('trainedModel.mat')
Now the contents of the structure "trainedModel" loaded from the MAT file viz., "predictfcn", "ClassficationsSVM", "About", and "HowToPredict" are saved in a structure variable "saved_model". Please check the image below for your reference.
Hence, to access the "predictfcn" function from the "trainedModel", following command needs to be executed,
predictedLabel = saved_model.trainedModel.predictFcn(imageFeatures);
Resolving your issue in app designer, I suggest to use some other variable name than "trainedModel" while loading the MAT file to avoid confusion. Load the saved model in "saved_model" variable which is defined in properties of the MATLAB App deisgner,
% this is in properties
saved_model = load('trainedModel.mat')
Then, create a callback for the pushbutton and use the "predictFcn" function with the help of following command:
% this is in button callback function
index = randomDataset.getId;
imageFeatures = app.analyzer.getImageFeatures(index);
predictedLabel = app.saved_model.trainedModel.predictFcn(imageFeatures);
P.S. I have created an app in MATLAB app designer to classify the fisher's iris dataset for reference. The MAT and MLAPP files are attached herewith for more information. The saved ML model is trained using Linear SVM on 120 training samples of the iris dataset.
  2 Kommentare
Kevin Gjoni
Kevin Gjoni am 1 Mär. 2023
Thank you, this works! Really helped me a lot with the explanation since this is for a school project and I also have to explain what I did :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Drew
Drew am 27 Feb. 2023
If this answers your question, please remember to accept this answer.

Produkte


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by