How can I test the models?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello guys. I'm new to MATLAB. I have performed fine-tuned of AlexNet and GoogleNet pre-trained networks to fit with my dataset.
How can i split the whole data into training, validation, and testing phase?
How can I test both models with same images for accuracy comparison?
------------------------------------------------------------------------------------------
%This is the code for AlexNet%
OutputFolder = fullfile('Dataset');
imds = imageDatastore('Dataset','IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomize');
net = alexnet
analyzeNetwork(net)
inputSize = net.Layers(1).InputSize
layersTransfer = net.Layers(1:end-3);
numClasses = numel(categories(imdsTrain.Labels))
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',10,'BiasLearnRateFactor',10)
softmaxLayer
classificationLayer];
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
options = trainingOptions('sgdm', ...
'MiniBatchSize',16, ...
'MaxEpochs',5, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
netTransfer = trainNetwork(augimdsTrain,layers,options);
[YPred,scores] = classify(netTransfer,augimdsValidation);
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation)
figure
for i = 1:4
subplot(2,2,i)
I = readimage(imdsValidation,idx(i));
imshow(I)
label = YPred(idx(i));
title(string(label) + ", " + num2str(100*max(probs(idx(i),:)),3) + "%");
end
Thank you.
0 comentarios
Respuestas (1)
Vinayak
el 27 de Feb. de 2024
Hi Olalekan
Fine tuning a pre-trained model can improve the model performance, and your code achieves the requested problem effectively.
You have correctly split dataset using `splitEachLabel`.
I would suggest you to load both the models AlexNet and GoogleNet within the same script, or use a .mat file to use the same dataset across both files.
Train the fine tuned models using the same train dataset. And as you have the test set, you can use it for testing both models.
Sample code:
imds = imageDatastore(datasetFolder, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
trainRatio = 0.7;
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomize');
alexNet = alexnet;
googleNet = googlenet;
% Training Code similar to your current code.
% Validate both trained models with imdsValidation
[YPredAlexNet, scoresAlexNet] = classify(alexNet, imdsValidation);
accuracyAlexNet = mean(YPredAlexNet == imdsValidation.Labels);
disp(['AlexNet Validation Accuracy: ' num2str(accuracyAlexNet * 100) '%']);
[YPredGoogleNet, scoresGoogleNet] = classify(googleNet, imdsValidation);
accuracyGoogleNet = mean(YPredGoogleNet == imdsValidation.Labels);
disp(['GoogleNet Validation Accuracy: ' num2str(accuracyGoogleNet * 100) '%']);
You may refer to the following docs on `spliteachlabel`
Also refer to this article to discover more on Transfer Learning with MATLAB:
0 comentarios
Ver también
Categorías
Más información sobre Image Data Workflows en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!