Invalid training data. The output size (8) of the last layer does not match the number of classes (6).

1 次查看(过去 30 天)
clc;
close all;
clear all;
%% load network
net=alexnet;
ppl=net.Layers;
net=net.Layers(1:19);
% layers(23)=fullyConnectedLayer(7);
% layers(25)=classificationLayer;
layers=[net
fullyConnectedLayer(8)
softmaxLayer()
classificationLayer()];
matlabpath='C:\Users\ayush\MATLAB_CAPSTONE\Dataset1';
data=fullfile(matlabpath,'trainingset');
train=imageDatastore(data,'IncludeSubfolders',true,'FileExtensions','.jpg','LabelSource','foldernames');
[imgtrain,imgtest]=splitEachLabel(train,0.8,'randomized');
count=train.countEachLabel;
%% training
opt=trainingOptions('sgdm','MaxEpochs',2,'InitialLearnRate',0.001,'Plots','training-progress','MiniBatchSize',64);
TrainNet=trainNetwork(train,layers,opt);
%% accuracy
pred=classify(TrainNet,imgtest);
accuracy=mean(pred==imgtest.Labels);
  1 个评论
AYUSH ANAND SAGAR
AYUSH ANAND SAGAR 2020-9-18
Error using trainNetwork (line 170)
Invalid training data. The output size (8) of the last layer does not match the number of classes (6).
Error in transfer (line 27)
TrainNet=trainNetwork(train,layers,opt);
this is the error thats popping up all the time

请先登录,再进行评论。

回答(1 个)

Mohammad Sami
Mohammad Sami 2020-9-19
Your final layer has 8 outputs, however your image data store only has 6 labels / classes. If you expect there to be 8 classes, check you image data store. If there are 6 classes, then change your final layer to have 6 outputs.

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by