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

4 次查看(过去 30 天)
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.

采纳的回答

Rahul
Rahul 2023-2-28
编辑:Rahul 2023-3-1
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.

更多回答(1 个)

Drew
Drew 2023-2-27
If this answers your question, please remember to accept this answer.

类别

Help CenterFile Exchange 中查找有关 Classification Learner App 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by