How to fix a Size Mismatch error in MulticlassECOC SVM Model code generation?

112 Views Asked by At

For my project, I have been generating ClassificationECOC models and using the Matlab Coder framework to generate C code from them. The methodology for this is laid out in the loadLearnerForCoder documentation. All of my ClassificationECOC models have been constructed in the same way, albeit with different parameters for inputs and classes. While Matlab Coder has been able to produce MEX files and C code for most of the models, some still fail. The following is the function being passed into codegen.

function label = classifyANose(X) %#codegen
% Classifies Nose feature for Asian face
% Reads in SVM model in file A-Nose-SVM.mat then returns class label
Mdl = loadLearnerForCoder('A-Nose-SVM.mat');
label = predict(Mdl,X);
end

When this function is called with codegen, an example input is also passed in as an argument. The example inputs have been produced in the same way for all models, and yet it fails for some models and not others. The error thrown is a Size mismatch, specifically Size mismatch ([1 x 1] ~= [1 x :?]) When investigating the Size Mismatch problem, the error was found to occur in the score function of the CompactClassificationSVM.m file. Specifically, the variable 'f' here is variable where it should be static.

function S = score(obj,X)
            %SCORE Calculate score for each observation.
            
            f = [email protected](obj,X);
            
            % Initialize all scores to minus the score for the positive
            % class and fill out the column for the positive class later.
            S = repmat(-f,1,size(obj.ClassNames,1));
            
            % For two-class learning, the obtained score is for the 2nd
            % class (+1) with non-zero probability.
            numNonZeros = sum(obj.ClassLogicalIndices~=0);
            n = numel(obj.ClassLogicalIndices);
            if numNonZeros==1 % one-class learning
                for ii = 1:n
                    if obj.ClassLogicalIndices(ii)
                        S(:,ii) = f;
                        break;
                    end
                end
            else             % binary SVM
                jj = 0;
                for ii = 1:n
                    if obj.ClassLogicalIndices(ii)
                        jj = ii;
                    end
                end
                S(:,jj) = f;
            end
        end

I am at a loss for why this occurs for certain models and not others, as they were all constructed in the same manner. There is a limited amount of change I can make to the function called by codegen, as this error seems to occur within the model itself. I have tried declaring 'label' as variable size with coder.varsize('label'); to no avail. When attempting to use alternate generators and configurers, similar errors occur. The ClassifcationECOC model is unsupported for the generateLearnerDataTypeFcn, and the learnerCoderConfigurer returns the same size mismatch issue as codegen or Matlab Coder. I have attempted to include all of the relevant information for this problem, and will provide anything else that I have missed if needed. Thank you.

0

There are 0 best solutions below