partial face recognition using CNN-alexnet
2 次查看(过去 30 天)
显示 更早的评论
an arror occured while implementing face recognition using CNN -alexnet.please help me to move the project.
clc
clear
n=1;
im = imageDatastore('images','IncludeSubfolders',true,'LabelSource','foldernames');
% Resize the images to the input size of the net
im.ReadFcn = @(loc)imresize(imread(loc),[227,227]);
[Train ,Test] = splitEachLabel(im,0.8,'randomized');
fc = fullyConnectedLayer(n);
net = alexnet;
ly = net.Layers;
ly(23) = fc;
cl = classificationLayer;
ly(25) = cl;
% options for training the net if your newnet performance is low decrease
% the learning_rate
learning_rate = 0.00001;
opts = trainingOptions("rmsprop","InitialLearnRate",learning_rate,'MaxEpochs',5,'MiniBatchSize',64,'Plots','training-progress');
[newnet,info] = trainNetwork(Train, ly, opts);
[predict,scores] = classify(newnet,Test);
names = Test.Labels;
pred = (predict==names);
s = size(pred);
acc = sum(pred)/s(1);
fprintf('The accuracy of the test set is %f %% \n',acc*100);
%%%%ERROR%%%%
Training on single CPU.
Initializing input data normalization.
Error using trainNetwork (line 170)
Unexpected image size: All images must have the same size.
Error in work2 (line 21)
[newnet,info] = trainNetwork(Train, ly, opts);
>>
0 个评论
回答(1 个)
Srivardhan Gadila
2020-3-16
Make sure that all the images have the same number of channels.
Make use of the following:
im = imageDatastore('images','IncludeSubfolders',true,'LabelSource','foldernames');
im.ReadFcn = @customReadDatstoreImage;
function data = customReadDatastoreImage(filename)
% code from default function:
onState = warning('off', 'backtrace');
c = onCleanup(@() warning(onState));
data = imread(filename); % added lines:
data = data(:,:,min(1:3, end));
data = imresize(data,[227 227]);
end
Refer to the following: How can i resize image stored in imageDatastore
Alternatively you can load the images using imageDatastore and then use the augmentedImageDatastore for resizing the images.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!