how to train a CNN by a folder of 2816 images?

1 Ansicht (letzte 30 Tage)
ghazaleh
ghazaleh am 30 Apr. 2024
Beantwortet: Harsh am 2 Mai 2024
i have this code:
digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ...
'nndatasets','DigitDataset');
digitData = imageDatastore(digitDatasetPath, ...
'IncludeSubfolders',true,'LabelSource','foldernames');
figure;
perm = randperm(50,20);
for i = 1:20
subplot(4,5,i);
imshow(digitData.Files{perm(i)});
end
CountLabel = digitData.countEachLabel;
img = readimage(digitData,1);
size(img)
trainingNumFiles = 9;
rng(1) % For reproducibility
[trainDigitData,testDigitData] = splitEachLabel(digitData, ...
trainingNumFiles,'randomize');
layers = [imageInputLayer([148 210 1])
convolution2dLayer(5,20)
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(5)
softmaxLayer
classificationLayer()];
layers = [imageInputLayer([148 210 1])
convolution2dLayer(5,20)
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(5)
softmaxLayer
classificationLayer()];
options = trainingOptions('sgdm','MaxEpochs',15, ...
'InitialLearnRate',0.0001);
convnet = trainNetwork(trainDigitData,layers,options);
YTest = classify(convnet,testDigitData);
TTest = testDigitData.Labels;
accuracy = sum(YTest == TTest)/numel(TTest)
How to train this network with a file containing 2816 png images? What changes should be made to it?

Antworten (1)

Harsh
Harsh am 2 Mai 2024
Hi,
Based on what you've shared, it looks like you're working on training a CNN with images stored in the folder matlabroot\toolbox\nnet\nndemos\nndatasets\DigitDataset.
This folder contains images for 10 different classes, each image being 28 x 28 in size. To match these specifications, kindly adjust the layers variable in your code so that the image input is set to [28 28 1], and the fully connected layer is set to 10 as shown below:
layers = [imageInputLayer([28 28 1])
convolution2dLayer(5,20)
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(10)
softmaxLayer
classificationLayer()];
Additionally, it seems like there is a duplicate declaration of the layers variable (line 28) of your code. Kindly remove the second instance of this declaration.
I hope this helps, thanks!

Kategorien

Mehr zu Image Data Workflows finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by