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 次查看(过去 30 天)
显示 更早的评论
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 个评论
Chandani Madnani
2017-10-5
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);
回答(1 个)
progga ilma
2019-12-29
编辑:progga ilma
2019-12-29
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' );
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!