MATLAB error: The output size (4) of the last layer doesn't match the number of classes (2). How to match the size for neural network?

3 views (last 30 days)
categories={'Dog','Cat'};
rootfolder='C:\Users\njindal\Documents\CNN\CNn\DeepLearningDemos\DeepLearningDemos\train dataset';
imds = imageDatastore(fullfile(rootfolder,categories),...
'LabelSource','foldernames');
imds.countEachLabel
varSize=32;
conv1=convolution2dLayer(5,varSize,'Padding',2);
conv1.Weights=gpuArray(single(randn([5 5 3 varSize]) *0.0001));
fc1=fullyConnectedLayer(64);
fc1.Weights=gpuArray(single(randn([64 576]) *0.1));
fc2=fullyConnectedLayer(4);
fc2.Weights=gpuArray(single(randn([4 64]) *0.1));
layers = [imageInputLayer([varSize varSize 3]);
conv1;
maxPooling2dLayer(3,'Stride',2);
reluLayer();
convolution2dLayer(5,32,'Padding',2);
reluLayer();
averagePooling2dLayer(3,'Stride',2);
convolution2dLayer(5,64,'Padding',2);
reluLayer();
averagePooling2dLayer(3,'Stride',2);
fc1;
reluLayer();
fc2;
softmaxLayer();
classificationLayer()];
opts = trainingOptions('sgdm',...
'InitialLearnRate',0.001,...
'LearnRateSchedule', 'piecewise',...
'LearnRateDropFactor',0.1,...
'LearnRateDropPeriod',8,...
'L2Regularization',0.004,...
'MaxEpochs',10,...
'MiniBatchSize',100,...
'Verbose',true);
[net, info] = trainNetwork(imds,layers,opts);
net.Layers
  1 Comment
Chandani Madnani
Chandani Madnani on 5 Oct 2017
This error usually occurs when a wrong number of classes is specified in the fully connected layer.
Try changing the following line: fc2=fullyConnectedLayer(4); with fc2=fullyConnectedLayer(2);

Sign in to comment.

Answers (1)

progga ilma
progga ilma on 29 Dec 2019
Edited: progga ilma on 29 Dec 2019
replace fullyConnectedLayer (4) with
fullyConnectedLayer (numClasses)
numClasses = numel (categories (imdsTrain.Labels));
imds = imageDatastore ( 'emotics2another' , ...
'IncludeSubfolders' , true, ...
'LabelSource' , 'foldernames' );
% imdsValidation = imageDatastore ('flower_photostest2', ...
% 'IncludeSubfolders', true, ...
% 'LabelSource', 'foldernames');
[imdsTrain, imdsValidation] = splitEachLabel (imds, 0.3, 'randomize' );
imdsTrain.ReadFcn = @customreader;
imdsValidation.ReadFcn = @customreader;
% augmentedTrainingSet = augmentedImageDatastore (imdsTrain, 'ColorPreprocessing', 'rgb2gray');% {resized according to image size%}
%
% augmentedTestSet = augmentedImageDatastore (imdsValidation, 'ColorPreprocessing', 'rgb2gray');
numClasses = numel (categories (imdsTrain.Labels));
layers = [
imageInputLayer ([200 200 3])
convolution2dLayer (3.8)
batchNormalizationLayer
reluLayer
maxPooling2dLayer (2)
convolution2dLayer (3.16)
batchNormalizationLayer
reluLayer
maxPooling2dLayer (2)
convolution2dLayer (3.32)
batchNormalizationLayer
reluLayer
fullyConnectedLayer (numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions ( 'sgdm' , ...
'InitialLearnRate' , 0.01, ...
'MaxEpochs' , 4, ...
'Shuffle' , 'every-epoch' , ...
'ValidationData' , imdsValidation, ...
'ValidationFrequency' , 30, ...
'Verbose' , false, ...
'Plots' , 'training-progress' );

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!