error with classes in network classification layer

19 views (last 30 days)
Hi all matlab folk,
I am seeking some assistance in solving this challenge and move forward! for some reason I am not getting this and I am in need of some assistance!
This error persists no matter if i set the classification layer to auto and I am finding it challenging to move past this phase of the network!
please see layers! then check the error at the bottom
thanx in advance!
Error using vision.internal.cnn.validation.checkNetworkClassificationLayer (line 11)
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 trainRCNNObjectDetector>checkNetworkAndFillRemainingParameters (line 290)
vision.internal.cnn.validation.checkNetworkClassificationLayer(analysis, trainingData);
Error in trainRCNNObjectDetector (line 256)
params = checkNetworkAndFillRemainingParameters(trainingData, network, params);
Error in test19 (line 54)
rcnn = trainRCNNObjectDetector(Wgtruth, Newlayers, options, 'NegativeOverlapRange', [0 0.3])
>>
this is my layers
Newlayers = [
imageInputLayer([32 32 3],"Mean",[],"Normalization","zerocenter", "Name","imageinput")
convolution2dLayer([5 5],32,"Name","conv","BiasLearnRateFactor",2,"Padding",[2 2 2 2],"WeightsInitializer","narrow-normal")
maxPooling2dLayer([3 3],"Name","maxpool","Stride",[2 2])
reluLayer("Name","relu")
convolution2dLayer([5 5],32,"Name","conv_1","BiasLearnRateFactor",2,"Padding",[2 2 2 2],"WeightsInitializer","narrow-normal")
reluLayer("Name","relu_1")
averagePooling2dLayer([3 3],"Name","avgpool","Stride",[2 2])
convolution2dLayer([5 5],64,"Name","conv_2","BiasLearnRateFactor",2,"Padding",[2 2 2 2],"WeightsInitializer","narrow-normal")
reluLayer("Name","relu_2")
averagePooling2dLayer([3 3],"Name","avgpool_1","Stride",[2 2])
fullyConnectedLayer(64,"Name","fc","BiasLearnRateFactor",2,"WeightsInitializer","narrow-normal")
reluLayer("Name","relu_3")
fullyConnectedLayer(2,"Name","fc_rcnn","BiasL2Factor",1,"BiasLearnRateFactor",5,"WeightLearnRateFactor",8,"WeightsInitializer","narrow-normal")
softmaxLayer("Name","softmax")
classificationLayer('Name','classoutput')]
Newlayers(15)
Newlayers(14)

Accepted Answer

Srivardhan Gadila
Srivardhan Gadila on 4 Mar 2020
Please refer to the following workflow: Create R-CNN Object Detection Network.
The total number of classes the RCNNdetector should detect will be the number of object classes you want to detect plus an additional background class. So try changing the outputSize argument of the fullyConnectedLayer to 3 (in this particular case)
  2 Comments
Daniel Rodinger
Daniel Rodinger on 21 Jul 2022
hi guys i have a question. i face a pretty similar situation now, i want to detect one object so it should be 1 +1 for the background calls but i always get the error message
"The number of 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" im really confused i basically just copy pasted the matlab example but it does not work. my trainingData is a 12x2 table so my
clc;
clear;
close all;
net = resnet50();
lgraph = layerGraph(net);
% figure
% plot(lgraph)
% ylim([-5 16])
% Remove the last 3 layers.
layersToRemove = {
'fc1000'
'fc1000_softmax'
'ClassificationLayer_fc1000'
};
lgraph = removeLayers(lgraph, layersToRemove);
% Display the results after removing the layers.
% figure
% plot(lgraph)
% ylim([-5 16])
% Specify the number of classes the network should classify.
numClasses = 1;
numClassesPlusBackground = numClasses + 1;
% Define new classfication layers
newLayers = [
fullyConnectedLayer(numClassesPlusBackground, 'Name', 'rcnnFC')
softmaxLayer('Name', 'rcnnSoftmax')
classificationLayer('Name', 'rcnnClassification')
];
% Add new layers
lgraph = addLayers(lgraph, newLayers);
% Connect the new layers to the network.
lgraph = connectLayers(lgraph, 'avg_pool', 'rcnnFC');
% Display the final R-CNN network. This can be trained using trainRCNNObjectDetector.
% figure
% plot(lgraph)
% ylim([-5 16])
load gTruth;
layers = resnet50('Weights', 'none')
trainingData = objectDetectorTrainingData(gTruth);
summary(trainingData)
options = trainingOptions('sgdm', ...
'MiniBatchSize', 128, ...
'InitialLearnRate', 1e-6, ...
'MaxEpochs', 5);
[detector,info] = trainRCNNObjectDetector(trainingData, layers, options, 'NegativeOverlapRange', [0 0.3]);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!