- Try to check the accuracy without using data agumentation, it might be possible that data augmentation is making the model more generalized and hence will have less testing error.
- Try to train for more epochs so that model settles down to a lower value of loss and you may conclude that the training is completed.
Data augmentation in CNN
6 次查看(过去 30 天)
显示 更早的评论
Helo... Iam working on a dataset of 300 images containing 2 classes. Below is the code i have attached for data augmentation in CNN.
But the error is that if am running the code for multiple times the accuracy is not at all constant, its changing and also the accuracy is decreasing for data augmentation. Could any one plz help me in solving this problem.
clc;
clear all;
close all;
myTrainingFolder = 'C:\Users\Admin\Desktop\Major Project\cnn_dataset';
%testingFolder = 'C:\Users\Be Happy\Documents\MATLAB\gtsrbtest';
imds = imageDatastore(myTrainingFolder,'IncludeSubfolders', true, 'LabelSource', 'foldernames');
%testingSet = imageDatastore(testingFolder,'IncludeSubfolders', true, 'LabelSource', 'foldernames');
labelCount = countEachLabel(imds);
numClasses = height(labelCount);
numImagesTraining = numel(imds.Files);
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
%This very small data set now contains 55 training images and 20 validation images. Display some sample images.
numTrainImages = numel(imdsTrain.Labels);
imageSize = [227 227 3];
inputSize = [227 227 3];
layer1 = [
imageInputLayer(inputSize)
convolution2dLayer(5,20)
batchNormalizationLayer
reluLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
layers = [
imageInputLayer(imageSize)
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options1 = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',15, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
netTransfer1 = trainNetwork(imdsTrain,layers,options1);
YPred = classify(netTransfer1,imdsValidation);
YValidation = imdsValidation.Labels;
netTransfer1BaselineAccuracy = sum(YPred == YValidation)/numel(YValidation);
inputSize = [227 227 3];
imageAugmenter = imageDataAugmenter( ...
'RandRotation',[-20,20], ...
'RandXReflection',1,...
'RandYReflection',1,...
'RandXTranslation',[-3 3], ...
'RandYTranslation',[-3 3]);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
netTransfer = trainNetwork(augimdsTrain,layers,options1);
%Classify Validation Images
[YPred,scores] = classify(netTransfer,augimdsValidation);
%Display four sample validation images with their predicted labels.
idx = randperm(numel(imdsValidation.Files),4);
figure
for i = 1:4
subplot(2,2,i)
I = readimage(imdsValidation,idx(i));
imshow(I)
label = YPred(idx(i));
title(string(label));
end
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation);
0 个评论
采纳的回答
Raunak Gupta
2020-5-30
编辑:Raunak Gupta
2020-5-30
Hi,
I see that the amount of data that you have is quiet less, so the Data Augmentation is very much appropriate while training the network. The accuracy values that are mentioned in the question will always be different when you train from scratch because of random initialization of weights in first epoch. So here are the few things you may try,
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!