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).

6 ビュー (過去 30 日間)
Afiq Anang
Afiq Anang 2023 年 11 月 14 日
回答済み: arushi 2024 年 8 月 29 日
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 件のコメント
Matt J
Matt J 2023 年 11 月 14 日
You need to explain why you think the error message is invalid. We can't see why.

サインインしてコメントする。

回答 (1 件)

arushi
arushi 2024 年 8 月 29 日
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!

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by