CNN, resnet50 - plot accuracy

4 views (last 30 days)
HK
HK on 19 Nov 2021
Edited: Prateek Rai on 24 Nov 2021
Can anyone help me with plotting the training progress (accuracy and loss)?
There seems to be a problem with the last line, but I'm not sure what the problem is. Help!
outputFolder = fullfile('Caltech')
rootFolder = fullfile(outputFolder, '101_ObjectCategories')
categories = {'normal_NB_resized', 'slow_OB_resized'}
imds = imageDatastore(fullfile(rootFolder,categories), 'LabelSource','foldernames')
tbl = countEachLabel(imds)
minSetCount = min(tbl{:,2})
imds = splitEachLabel(imds, minSetCount, 'randomize')
countEachLabel(imds)
normal = find(imds.Labels == 'normal', 1)
slow = find(imds.Labels == 'slow', 1)
net = resnet50();
net.Layers(1);
net.Layers(end);
numel(net.Layers(end).ClassNames);
[trainingSet, testSet] = splitEachLabel(imds, 0.7, 'randomize');
imageSize = net.Layers(1).InputSize;
augmentedTrainingSet = augmentedImageDatastore(imageSize,...
trainingSet, 'ColorPreprocessing', 'gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize,...
testSet, 'ColorPreprocessing', 'gray2rgb');
w1 = net.Layers(2).Weights;
w1 = mat2gray(w1);
featureLayer = 'fc1000';
trainingFeatures = activations(net,...
augmentedTrainingSet, featureLayer, 'MiniBatchSize', 64, 'OutputAs', 'columns');
trainingLables = trainingSet.Labels;
classifier = fitcecoc(trainingFeatures, trainingLables,...
'Learner', 'Linear', 'Coding', 'onevsall', 'ObservationsIn', 'columns');
testFeatures = activations(net,...
augmentedTestSet, featureLayer, 'MiniBatchSize', 64, 'OutputAs', 'columns');
predictLabels = predict(classifier, testFeatures, 'ObservationsIn', 'columns');
testLables = testSet.Labels
confMat = confusionmat(testLables, predictLabels)
confMat = bsxfun(@rdivide, confMat, sum(confMat,2))
mean(diag(confMat))
newImage = imread(fullfile('test.jpg'));
ds = augmentedImageDatastore(imageSize,...
newImage, 'ColorPreprocessing', 'gray2rgb');
imageFeatures = activations(net,...
ds, featureLayer, 'MiniBatchSize', 64, 'OutputAs', 'columns');
label = predict(classifier, imageFeatures, 'ObservationsIn', 'columns');
sprintf('loaded image belongs to %s class', label)
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',imds, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
netT = trainNetwork(imds,layers,options);

Accepted Answer

Prateek Rai
Prateek Rai on 22 Nov 2021
Edited: Prateek Rai on 24 Nov 2021
Hi,
There are 2 facts about the code which is important to note:
(1) Since, your dataset is big that is why it is taking more time to perform dataset involving tasks and hence you are not able to reach the last line of the code i.e. training the network and plotting the training progress. Thus, it seems that the network training started already without showing the training progress but actually training has not started.
(2) Secondly, if your image datastore is somehow created and your last line started executing then also error will come stating: "Unrecognized function or variable 'layers'. It is because you have not declared variable 'layers' but used it in the last line of the code.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!