필터 지우기
필터 지우기

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

조회 수: 7 (최근 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 :)

댓글을 달려면 로그인하십시오.

추가 답변 (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