Data augmentation in CNN
Mostrar comentarios más antiguos
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);
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Object Detection en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!