Question
Hello, I'm a relative newbie to MATLAB and neural networks, and I'm looking at disease spread and analysis in crop fields. I wanted to make an RCNN to help with this. I have some skeleton code, but I'm getting errors I don't understand and don't have the skill to debug. Here is the code: load 'D:\Documents\MATLAB\bridgeLabels.mat', 'gTruth'; %these are the labels I made in the image labeler app trainingData = objectDetectorTrainingData(gTruth); %this apparently makes the training data for me layers = [imageInputLayer([2160 3840 3]) convolution2dLayer([5 5],10) reluLayer() fullyConnectedLayer(10) softmaxLayer() classificationLayer()]; %I understand what all these things do, kind of. %I just copied this code from the demonstration in the reference %I'm getting some error with the classification layer I don't know how to fix options = trainingOptions('sgdm',... 'LearnRateSchedule','piecewise',... 'LearnRateDropFactor',0.2,... 'LearnRateDropPeriod',5,... 'MaxEpochs',20,... 'MiniBatchSize',64,... 'Plots','training-progress'); %again, most of this makes sense to me detector = trainRCNNObjectDetector(trainingData, layers, options); %ok so now the network is made apparently image = imread('D:\Documents\MATLAB\clubroot_shots\lcbo1.png'); %this is my testing image wid = 10; rois = zeros(1, (image.width/wid)*(image.height/wid)); for i=1:image.width/wid for j=1:image.height/wid rois(i+j*width) = [1+(i-1)*wid, 1+(j-1)*wid, wid, wid]; end end %I believe this code will split up the image into 10x10 regions of interest. %I wrote this block myself. classifyRegions(detector, image, rois) %and here the regions get classified. Semicolon off because i want to see what happens When I run this code, I get the following errors: Error using vision.internal.cnn.validation.checkNetworkClassificationLayer (line 9) The number object classes in the network classification layer must be equal to the number of classes defined in the input trainingData plus 1 for the "Background" class. Error in vision.internal.rcnn.parseInputs (line 35) vision.internal.cnn.validation.checkNetworkClassificationLayer(network, trainingData); Error in trainRCNNObjectDetector (line 185) params = vision.internal.rcnn.parseInputs(trainingData, network, options, mfilename, varargin{:}); Error in imagenn (line 20) detector = trainRCNNObjectDetector(trainingData, layers, options); Error in run (line 91) evalin('caller', strcat(script, ';')); I'm not sure, but I believe all these errors stem from an improperly declared classificationLayer. I have two classes, called 'clubroot' and 'healthy'. I'm not sure how to set up the network so it recognizes these two classes. If anyone could offer help, I would be eternally grateful. Getting this to work is very important to me.
Related Questions
Expert Answer
Kshitij Singh
PhD Expert
Answered Nov 20, 2025
As given here, you are improperly initializing the fullyConnectedLayer. Instead of using fullyConnectedLayer(10) try something like this.
classes = {'first', 'second'}
outputs = 1+numel(classes); % +1 for background class
layers = [imageInputLayer([2160 3840 3])
convolution2dLayer([5 5],10)
reluLayer()
fullyConnectedLayer(outputs)
softmaxLayer()
classificationLayer()];
Have a different question? Ask here