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

조회 수: 38(최근 30일)
Youngmin Na
Youngmin Na 2021년 1월 26일
답변: Shashank Gupta 2021년 2월 4일
I want to build seven inputs, one output network. (11 classes; 1, 2, ... , 11)
I used the filedatastore and tranformed datastore type.
My CNN model's input layer is 3D image arrays for each input.
One single mat file inculded Cell array (11x8); (7 double arrays (inputs) and categorical type (label))
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[1 ]}
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[2 ]}
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[3 ]}
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[4 ]}
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[5 ]}
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[6 ]}
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[7 ]}
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[8 ]}
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[9 ]}
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[10]}
{10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {10×9×640 double} {[11]}
When I load the datastore, I used below code.
TrainDS=Simple_combined([File_path 'Train']);
function [trainingDatastore]=Simple_combined(folder)
tempdatastore = datastore(folder,'ReadFcn',@load,'IncludeSubfolders',1,'Type','file','FileExtensions','.mat');
trainingDatastore = transform(tempdatastore,@rearrangeData);
function out = rearrangeData(ds)
out = ds.CombinedCell;
end
end
I tried to train my model using this code.
[trainedNet, traininfo]= trainNetwork(TrainDS,lgraph,options);
The error message was this: Invalid training data. The output size (11) of the last layer does not match the number of classes (1).
I also debuged the code.
I found the "ResponseSize" and "OutputSizes" are different after "iCreateMISOTrainingDataDispatcher" in the "trainNetwork" function.
trainingDispatcher =
GeneralDatastoreDispatcher with properties:
ImageSize: {[10 9 640 1] [10 9 640 1] [10 9 640 1] [10 9 640 1] [10 9 640 1] [10 9 640 1] [10 9 640 1]}
ResponseSize: {[1 1 1 1]}
NumObservations: Inf
ResponseMetaData: [1×1 nnet.internal.cnn.response.ClassificationMetaData]
IsDone: 0
Precision: [1×1 nnet.internal.cnn.util.Precision]
EndOfEpoch: 'discardLast'
ShuffleOption: 'once'
MiniBatchSize: 10
DataLoader: [1×1 nnet.internal.cnn.DataLoader]
DataTranslator: [1×1 nnet.internal.cnn.util.DataTranslator]
CollateFunctions: [1×1 struct]
InputSizes: {[10 9 640 1] [10 9 640 1] [10 9 640 1] [10 9 640 1] [10 9 640 1] [10 9 640 1] [10 9 640 1]}
OutputSizes: {[1 1 1 11]}
UnpackCellInputOutput: 0
CanPreserveOrder: 0
CatAndSliceStrategy: [1×1 nnet.internal.cnn.util.NDArrayCatAndSliceStrategy]
RunInBackground: 0
What should I do? Please help me

답변(1개)

Shashank Gupta
Shashank Gupta 2021년 2월 4일
Hi Youngmin,
did you check the datastore which you created? does it have label information attached with it. My first guess looking at the error is, the datastore is not picking the label in the right format. Instead there is another way of solving this, you can use imageDatastore to construct a datastore. I am also attaching a small peice of code for your reference.
% Creating a datastore.
ds = imageDatastore('training_dataset','IncludeSubfolders',true, ...
'FileExtensions','.mat','ReadFcn',@loadMAT,'LabelSource','foldernames');
Good thing about this partiular datastore is, you can give LabelSource as name-value pair, which can take the label information automatically in accordance with the folder construction.
I hope this helps.
Cheers.

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by