Error with layer when trying to train my own CNN

1 次查看(过去 30 天)
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 个评论
Maria Duarte Rosa
Maria Duarte Rosa 2017-12-15
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 2019-6-28
What will be the fullyconnected layer output If we want to remove noise kind of thing not class?

请先登录,再进行评论。

回答(0 个)

类别

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