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).
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Gagan Agarwal
2023-11-20
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!