An error using trainNetwork. Invalid training data. The output size (1311) of the last layer does not match the number of classes of the responses (20).
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
rootFolder = fullfile('');
categories = {'A', 'B', 'C', 'D', 'E',...
'F', 'G', 'H', 'I', 'J',...
'K', 'L', 'M', 'N', 'O',...
'P', 'Q', 'R', 'S', 'T'};
handles.categories = categories;
%% Store categories into imds
imds = imageDatastore(fullfile(rootFolder, categories), 'LabelSource', 'foldernames');
handles.imds = imds;
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
%% Load net
net = googlenet;
deepNetworkDesigner(net)
inputSize = net.Layers(1).InputSize;
%% Replace final layer
lgraph = layerGraph(net);
numClasses = numel(categories(imdsTrain.Labels));
newLearnableLayer = fullyConnectedLayer(numClasses, ...
'Name','new_fc', ...
'WeightLearnRateFactor',10, ...
'BiasLearnRateFactor',10);
lgraph = replaceLayer(lgraph,'loss3-classifier',newLearnableLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'output',newClassLayer);
%% Train network
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);
%% Training Option
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',6, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
%% Training Progress Plot
netTransfer = trainNetwork(augimdsTrain,lgraph,options);
Error using trainNetwork
Invalid training data. The output size (1311) of the last layer does not match the number of classes of the responses (20).
Error in test (line 55)
netTransfer = trainNetwork(augimdsTrain,lgraph,options);
1 commentaire
Matt J
le 14 Nov 2023
You need to explain why you think the error message is invalid. We can't see why.
Réponses (1)
arushi
le 29 Août 2024
Hi Afiq,
I understand that you are encountering an error due to a mismatch between the output layer and the number of classes in the response. This issue occurs when the number of output nodes in the neural network exceeds the number of output classes. To resolve this error, you can adjust the number of output nodes in the network to match the number of output classes. You can achieve this by utilizing the 'outputSize' attribute of the 'fullyConnectedLayer' function.
For additional information please refer to the following documentation link:
I hope it helps!
0 commentaires
Voir également
Catégories
En savoir plus sur Image Data Workflows dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!