MATLAB: The CNN model only predicts a single class out of three

alex netcnn modelComputer Vision Toolboxdeep learningDeep Learning ToolboxerrorMATLAB and Simulink Student Suitemodel accuracy errorneural networksingle category prediction

Hello,
I am working on training a CNN model for pavement distress identification using image classification. I have three categories for defects (Alligator Cracks, Potholes and Rutting). When I run my model it only predicts a single class out of three rendering my model accuracy to 33.33%. I tried changing the training parameters/ options but the result is same. I was hoping if someone can help me figure out why. The code I am using is given below. Thanks in advance.
_______________________________________________________________________________________________________________________________________
rootFolder = fullfile ('CNN Input');
categories = {'Alligator Cracks','Potholes','Rutting'};
imds = imageDatastore (fullfile (rootFolder,categories), 'LabelSource', 'Foldernames');
tbl = countEachLabel (imds);
minSetCount = min(tbl{:,2});
countEachLabel(imds);
AlligatorCracks = find(imds.Labels == 'Alligator Cracks',1);
Potholes = find(imds.Labels == 'Potholes',1);
Rutting = find(imds.Labels == 'Rutting',1);
net = alexnet ();
rng ('default');
net.Layers(1);
net.Layers(end);
numel(net.Layers(end).ClassNames);
[trainingSet, testSet, validationSet] = splitEachLabel(imds,0.75,0.15,'randomize');
imageSize = net.Layers(1).InputSize;
augmentedTrainingSet = augmentedImageDatastore(imageSize,trainingSet,'ColorPreprocessing','gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize,testSet,'ColorPreprocessing','gray2rgb');
augmentedValidationSet = augmentedImageDatastore(imageSize,validationSet,'ColorPreprocessing','gray2rgb');
w1 = net.Layers(2).Weights;
w1 = mat2gray(w1);
featurelayer = 'drop7';
trainingFeatures = activations(net, augmentedTrainingSet, featurelayer,'MiniBatchSize', 32,'OutputAs', 'columns');
options = trainingOptions('sgdm', …
'LearnRateSchedule','piecewise',…
'LearnRateDropFactor',0.2,…
'learnRateDropPeriod',1,…
'MaxEpochs',8,…
'MiniBatchSize',16,…
'Plots','training-progress',…
'Shuffle','once');
numClasses = 3;
layersTransfer = net.Layers(1:end-3);
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
trainedNet = trainNetwork(augmentedTrainingSet,layers,options);
trainingLabels = trainingSet.Labels;
classifier = fitcecoc(trainingFeatures, trainingLabels, 'Learner', 'Linear', 'Coding', 'onevsall','observationsIn','columns');
testFeatures = activations(trainedNet,augmentedTestSet, featurelayer,'MiniBatchSize', 32,'OutputAs', 'columns');
validationFeatures = activations(trainedNet,augmentedValidationSet, featurelayer,'MiniBatchSize', 32,'OutputAs', 'columns');
predictLabels = predict(classifier,testFeatures,'ObservationsIn','columns');
predictLabelss = predict(classifier,validationFeatures,'ObservationsIn','columns');
testLables = testSet.Labels;
validationLables = validationSet.Labels;
confMat = confusionmat(testLables, predictLabels);
confMatt = confusionmat(validationLables, predictLabelss);
confMat = bsxfun(@rdivide, confMat, sum(confMat,2));
confMatt = bsxfun(@rdivide, confMatt, sum(confMatt,2));
mean(diag(confMat));
mean(diag(confMatt));

Best Answer

Hi, You facing this problem may be because of class imbalance.
Just check the number of datapoints you have in each class.
To handle the class imbalance, you can use data augmentation techniques.
For data augmentation in MATLAB, you can refer to this link: https://www.mathworks.com/help/deeplearning/ref/imagedataaugmenter.html