The CNN model only predicts a single class out of three?
Show older comments
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));
Accepted Answer
More Answers (1)
Vinay Chawla
on 24 Jul 2020
0 votes
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!