partial face recognition using CNN-alexnet

4 views (last 30 days)
Munshida P
Munshida P on 9 Mar 2020
Commented: Munshida P on 19 Mar 2020
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);
>>

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 16 Mar 2020
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
Alternatively you can load the images using imageDatastore and then use the augmentedImageDatastore for resizing the images.
  1 Comment
Munshida P
Munshida P on 19 Mar 2020
okey sir....Thank you. I will implement and will be back with the results...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!