MATLAB Answers

0

Invalid validation data. The output size (118) of the last layer doesn't match the number of classes (79). I am using Alexnet for classification of my dataset. It is giving the above error in trainNetwork function. ---Chandrakala

Asked by chandra kala on 18 May 2018
Latest activity Commented on by Image Analyst
on 18 May 2018

unzip('CharacterDataset.zip'); characterds = imageDatastore('CharacterDataset','IncludeSubfolders',true,'LabelSource','foldernames'); [characterdsTrain,characterdsValidation] = splitEachLabel(characterds,0.7,'randomized'); numTrainImages = numel(characterdsTrain.Labels); idx = randperm(numTrainImages,16); figure for i = 1:16 subplot(4,4,i) I = readimage(characterdsTrain,idx(i)); imshow(I) end net = alexnet; inputSize = net.Layers(1).InputSize layersTransfer = net.Layers(1:end-3); numClasses = numel(categories(characterdsTrain.Labels))

layers = [ layersTransfer fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20) softmaxLayer classificationLayer];

pixelRange = [-30 30]; imageAugmenter = imageDataAugmenter( ... 'RandXReflection',true, ... 'RandXTranslation',pixelRange, ... 'RandYTranslation',pixelRange); augimdsTrain = augmentedImageDatastore(inputSize(1:2),characterdsTrain, ... 'DataAugmentation',imageAugmenter); augimdsValidation = augmentedImageDatastore(inputSize(1:2),characterdsValidation,... 'DataAugmentation',imageAugmenter); options = trainingOptions('sgdm', ... 'MiniBatchSize',10, ... 'MaxEpochs',6, ... 'InitialLearnRate',1e-4, ... 'ValidationData',augimdsValidation, ... 'ValidationFrequency',3, ... 'ValidationPatience',Inf, ... 'Verbose',false, ... 'Plots','training-progress'); netTransfer = trainNetwork(augimdsTrain,layers,options);

[YPred,scores] = classify(netTransfer,augimdsValidation); idx = randperm(numel(characterdsValidation.Files),4); figure for i = 1:4 subplot(2,2,i) I = readimage(characterdsValidation,idx(i)); imshow(I) label = YPred(idx(i)); title(string(label)); end

Products


Release

R2018a

0 Answers