I've built a classification model (esemble), and saved it using saveLearnerForCoder. However, when compiling my code to C using MATLAB coder, an error is raised. I'm using MATLAB 2020a. This issue appears to have been previously reported here. Please, find detailed information below. I would very appreciate a workaround.
I'm loading the classifier as recommended:
cough_detection_model = loadLearnerForCoder('my_model');
This triggers the following error:
??? The input to coder.const cannot be reduced to a constant: Unsupported value.
Error in ==> loadLearnerForCoder Line: 41 Column: 68
My classifier was created with the following code:
template_tree = templateTree('MaxNumSplits', 700, 'MinLeafSize', 20);
ensemble_model = fitcensemble(features, classes, ...
'Learners', template_tree, 'Method','RUSBoost', 'NumLearningCycles', 500, ...
'LearnRate', 0.1, 'ScoreTransform', 'None');
I am able to load the classifier and use it normally. The error only happens when using the codegen.
I made some digging, and created a bypass function which avoids the problems from loadLearnerForCoder. Obviously it would crash somewhere, but I think it gave me some further information. This is the code I managed to create:
matFile = coder.load("my_model");
fromStructFcn = str2func('classreg.learning.classif.CompactClassificationEnsemble.fromStruct');
obj = fromStructFcn(matFile.compactStruct);
Which raises the following error:
??? Failed to compute constant value for nontunable property 'NumPredictors'. In code generation, nontunable
properties can only be assigned constant values.
I have tried to overpass this by doing:
matFile.compactStruct.DataSummary.NumPredictors = coder.const(117);
But this doesn't help either.
Can please anyone help me?