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

5 ビュー (過去 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.
  2 件のコメント
Kevin Gjoni
Kevin Gjoni 2023 年 3 月 1 日
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 :)
Rahul
Rahul 2023 年 3 月 1 日
Glad to help. Keep learning and exploring.

サインインしてコメントする。

その他の回答 (1 件)

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

カテゴリ

Help Center および File ExchangeClassification Learner App についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by