Clear Filters
Clear Filters

Error with layer when trying to train my own CNN

1 view (last 30 days)
Hello,
I am training my own neural network (like in this example) with my own dataset, to classify images.
Problem is, I have this error message when I try to start the training :
Error using trainNetwork (line 140)
Invalid training data. The output size (250000) of the last layer doesn't match the number of classes (1).
Caused by:
Error using nnet.internal.cnn.util.TrainNetworkDataValidator/assertCorrectResponseSizeForOutputLayer (line
217)
Invalid training data. The output size (250000) of the last layer doesn't match the number of classes (1).
Here is my code :
digitDatasetPath = 'ChinaSet_AllFiles/CXR_png';
digitData = imageDatastore(digitDatasetPath,...
'IncludeSubfolders',true,'LabelSource','foldernames');
% creating datastore with files from my folder
trainingNumFiles = 500;
rng(1) % For reproducibility
[trainDigitData,testDigitData] = splitEachLabel(digitData,...
trainingNumFiles,'randomize');
% splits the image files in digitData into two new datastores, trainDigitData and testDigitData
layers = [imageInputLayer([600 600 1]);
convolution2dLayer(500,50);
reluLayer();
maxPooling2dLayer(2,'Stride',2);
fullyConnectedLayer(500*500);
softmaxLayer();
classificationLayer()];
options = trainingOptions('sgdm','MaxEpochs',50,...
'InitialLearnRate',0.0001);
convnet = trainNetwork(trainDigitData,layers,options); % starts the training
Does anyone knows where it comes from ?
Thanks in advance!
  2 Comments
Maria Duarte Rosa
Maria Duarte Rosa on 15 Dec 2017
Hi Alizee,
The outputSize of the fullyConnectedLayer should be equal to the number of classes in your dataset. Currently you have outputSize = 500*500. If your digit dataset contains 10 classes then you should use fullyConnectedLayer(10). You can also count the number of classes from the data:
numClasses = numel(categories(trainDigitData.Labels));
Then use this variable in the fully connected layer:
fullyConnectedLayer(numClasses).
Thanks
sasmita  mahakud
sasmita mahakud on 28 Jun 2019
What will be the fullyconnected layer output If we want to remove noise kind of thing not class?

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!