Bug in loadLearnerForCoder from MATLAB coder

6 views (last 30 days)
Hello,
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'); % I tried with both ' and ", just in case
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');
saveLearnerForCoder(ensemble_model, 'my_model')
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); % raises error
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?
  1 Comment
Diogo Tecelão
Diogo Tecelão on 1 Dec 2020
Update
By using the following code:
fromStructFcn = str2func('classreg.learning.classif.CompactClassificationEnsemble.fromStruct');
obj = fromStructFcn(coder.const(matFile.compactStruct));
I now get a different error:
??? Code generation does not support MATLAB class 'classreg.learning.coder.classif.CompactClassificationEnsemble'
in this context because the class does not implement toRedirect() and/or fromRedirect() methods.

Sign in to comment.

Answers (2)

Sriram Padavettan
Sriram Padavettan on 4 Dec 2020
Edited: Sriram Padavettan on 4 Dec 2020
The problem is 2020A. I think there's a bug in that version. I messed around with this same issue all day yesterday on 2020A, tried 2020B instead, and it worked. Maybe even 2019b or earlier would work.
:)

Marou AD
Marou AD on 11 Feb 2024
It seems there is a bug in MATLAB 2020a that might be causing the problem. I had a similar issue, and after experimenting, I found that switching to MATLAB 2020b resolved the problem for me. It seems there might be a compatibility or bug fix in the later version. I recommend giving MATLAB 2020b a try , it will resolves your compilation issue.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!