How do I deploy a model created by Classification learner as an standalone application?

1 view (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 7 Jan 2020
To deploy the model, you would have to save the model from Classification Learners App. Please follow the steps below:
1. Once you have trained the model in the app, save that model using "Export Model" function from the app. This should generate a struct variable for the model in MATLAB workspace. Let it be "trainedModel"
2. Save the "trainedModel" struct into a "MAT" file. Lets say, "model.mat"
3. Write a "M" file to be deployed as a standalone application which should perform the following steps:
  • Load "model.mat"
  • Convert the input data from excel into "table" as model requires table datatype to execute.
  • Extract "ClassificationTree" from the model.
  • Perform a call to "predict" function passing "ClassificationTree" and the data from excel as Input parameters.
  • The call to predict function would return a prediction based on input data and the trained model. Return this value.
NOTE:
  • You cannot use "predictFcn" which is inside the "trainedModel" in a deployed target.
  • Since there are more than one predict function in all the MATLAB toolboxes, you would have to use "function pragma" to let the dependency analyzer know which "predict" function to use. In this case we have to use "predict" function from "Classification Tree" method.
  • The input data requirements may differ based on the model being used. In this case, the model requires data to be in form of table.
Even after including the ClassificationTree, the following warning message was received:
“Warning: Could not find appropriate function on path loading function handle <C:\Program Files\MATLAB\R2017a\toolbox\stats\mlearnapp\+mlearnapp\+internal\+model\DatasetSpecification.m>@(x)exportableModel.predictFcn(predictorExtractionFcn(x))>”
The error message is not clear enough to include the DataSetSpecification function. An enhancement request is raised for the same.
% This function takes "ValidationData" as an input from Excel and
% returns "yfit" which a prediction based on the "trainedModel" from "model.mat"
function yfit = runModelFromMATLAB(ValidationData)
%# function CompactClassificationTree
%# function ClassificationTree
%# function mlearnapp.internal.model.DatasetSpecification % declares which "predict" function to use
load model.mat; % trained model from Classification Learners App
ModTableData = ValidationData;
% convert data to table
ModTableData = cell2table(ModTableData, 'VariableNames',...
{'status','trial_devcount','errorcnts','trial_avgviewedmins',...
'trialviewedmins','trial_avgbitrate'});
% get classification tree from the model and call predict using this tree
ModelClassTree = trainedModel.ClassificationTree;
yfit = predict(ModelClassTree, ModTableData);
You can refer the following documentation.
・Deploy Predictions Using MATLAB Compiler

More Answers (0)

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!