Bug in loadLearnerForCoder from MATLAB coder

6 次查看(过去 30 天)
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 个评论
Diogo Tecelão
Diogo Tecelão 2020-12-1
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.

请先登录,再进行评论。

回答(2 个)

Sriram Padavettan
Sriram Padavettan 2020-12-4
编辑:Sriram Padavettan 2020-12-4
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 2024-2-11
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by