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?

2 vues (au cours des 30 derniers jours)
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 commentaire
Chandani Madnani
Chandani Madnani le 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);

Connectez-vous pour commenter.

Réponses (1)

progga ilma
progga ilma le 29 Déc 2019
Modifié(e) : progga ilma le 29 Déc 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' );

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by