[fileInput, pathInput] = uigetfile('*.png','MultiSelect','on');
[fileValid, pathValid] = uigetfile('*.png','MultiSelect','on');
numTrainfiles =numel(fileInput);
numValidfiles = numel(fileValid);
for k = 1:length(numTrainfiles)
baseFileNameInput{k} = fileInput{k};
fullFileName{k} = fullfile(baseFileNameInput);
imageArrayInput(k,:,:,:) = imread(fullFileName{k});
[~, height{k}, width{k}, colors{k}] = size(imageArrayInput);
end
for l = 1:length(numValidfiles)
baseFileNameValid{l} = fileValid{l};
fullFileName2{l} = fullfile(baseFileNameValid);
imageArrayValid(l,:,:,:) = imread(fullFileNameValid{l});
[~, height{l}, width{l}, colors{l}] = size(imageArrayValid);
end
imageAugmenter = imageDataAugmenter( ...
'RandRotation', [-20,20], ...
'RandXTranslation',[-3,3], ...
'RandYTranslation',[-3,3]);
imageSize = [height{k} width{k} colors{k}];
augimds = augmentedImageDatastore(imageSize,numTrainfiles,'DataAugmentation',imageAugmenter);
[imdsTrain] = splitEachLabel(imds,numTrainfiles,'randomized');
[imdsValidation] = splitEachLabel(imds2,numValidfiles,'randomized');
augimds = augmentedImageDatastore(imageSize,imdsTrain,'DataAugmentation',imageAugmenter);
numClasses = numel(categories(imdsTrain.Labels));
numClasses2 = numel(categories(imdsValidation.Labels));
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];
options = trainingOptions('sgdm', ...
'MaxEpochs',6, ...
'Shuffle','every-epoch', ...
'MiniBatchSize',64,...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
yvalidation = imdsValidation.Labels;
accuracy = mean(Ypred == yvalidation);