Invalid training data. The output size (1000) of the last layer does not match the number of classes (68).
55 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I try to use my code for the Xception and ResNet50. It worked for AlexNet and VGG16, now I get the above error.
Can someone tell me what is wrong with my code or how i can fix it?
[imdsTrain,imdsValidation] = splitEachLabel(trainset,0.7,'randomized');
net = xception;
inputSize = net.Layers(1).InputSize;
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain,'OutputSizeMode','resize','ColorPreprocessing', 'gray2rgb');
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation,'OutputSizeMode','resize','ColorPreprocessing', 'gray2rgb');
%lgraph = vgg16 ('Weigths','none');
layersTransfer = net.Layers(1:end-3);
%layer_Classifiy = fullyConnectedLayer(numClasses);
%layer_Classifiy.Name = 'fc1000'; %resnet fc1000
%lgraph = replaceLayer(lgraph,'fc1000',layer_Classifiy);
numClasses = numel(categories(imdsTrain.Labels))
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
%net_vgg16 = trainNetwork(augimdsTrain_Matrix,options);
options = trainingOptions('sgdm', ...
'MiniBatchSize',3, ...
'MaxEpochs',6, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',50, ...
'Verbose',false, ...
'Plots','training-progress',...
'ExecutionEnvironment','GPU');
netTransfer = trainNetwork(augimdsTrain,layerGraph(net),options);
augimdsTest = augmentedImageDatastore(inputSize(1:2),testset,'OutputSizeMode','resize','ColorPreprocessing', 'gray2rgb');
YPred = classify(netTransfer,augimdsTest, 'MiniBatchSize', 5);
YTest = testset.Labels;
accuracy = mean(YPred == YTest)
0 commentaires
Réponses (1)
Prateek Rai
le 13 Sep 2021
To my understanding, you want to use the code for the Xception and ResNet50 and getting error for invalid training data. In the given code, you are not updating the "net.Layers" with the new layers and hence trainNetwork is using the Xception meant for 1000 classes.
You can update "net.Layers" as mentioned below:
net.Layers = layers;
So overall code may look like:
(Note: Comments are added at the location of modification)
[imdsTrain,imdsValidation] = splitEachLabel(trainset,0.7,'randomized');
net = xception;
inputSize = net.Layers(1).InputSize;
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain,'OutputSizeMode','resize','ColorPreprocessing', 'gray2rgb');
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation,'OutputSizeMode','resize','ColorPreprocessing', 'gray2rgb');
%lgraph = vgg16 ('Weigths','none');
layersTransfer = net.Layers(1:end-3);
%layer_Classifiy = fullyConnectedLayer(numClasses);
%layer_Classifiy.Name = 'fc1000'; %resnet fc1000
%lgraph = replaceLayer(lgraph,'fc1000',layer_Classifiy);
numClasses = numel(categories(imdsTrain.Labels))
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
%net_vgg16 = trainNetwork(augimdsTrain_Matrix,options);
options = trainingOptions('sgdm', ...
'MiniBatchSize',3, ...
'MaxEpochs',6, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',50, ...
'Verbose',false, ...
'Plots','training-progress',...
'ExecutionEnvironment','GPU');
%% -- Updating net.Layers with new "layers" is required -- %%
net.Layers = layers;
%% -- %%
netTransfer = trainNetwork(augimdsTrain,layerGraph(net),options);
augimdsTest = augmentedImageDatastore(inputSize(1:2),testset,'OutputSizeMode','resize','ColorPreprocessing', 'gray2rgb');
YPred = classify(netTransfer,augimdsTest, 'MiniBatchSize', 5);
YTest = testset.Labels;
accuracy = mean(YPred == YTest)
You can refer to xception MathWorks documentation page to find more on Xception convolutional neural network.
1 commentaire
Joss Knight
le 16 Sep 2021
net.Layers = layers won't work since that property isn't editable. Your issue here is that you haven't actually trained the modified network, you've just passed layerGraph(net) to trainNetwork. Perhaps you did this because passing layers didn't work, which will be because resnet50 is not a series network and cannot be trained like one. You need to edit the layer graph so you don't break its connections.
lgraph = layerGraph(net);
lgraph = removeLayers(lgraph, {lgraph.Layers(end-3:end).Name});
lastLayerName = lgraph.Layers(end).Name;
lgraph = addLayers(lgraph, ...
[fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20,'Name','fcTransfer')
softmaxLayer
classificationLayer];
lgraph = connectLayers(lgraph, lastLayerName, 'fcTransfer');
...
netTransfer = trainNetwork(augimdsTrain,layerGraph(net),options);
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!