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

1 view (last 30 days)

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

Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!