Face recognition using Local ridge regression

3 visualizaciones (últimos 30 días)
Abdelrahman
Abdelrahman el 7 de Ag. de 2023
If any one can run this code and give me results and modify to my model to get better results
% Face Recognition using LRR
%% Load a dataset of grayscale face images
Dataset = imageDatastore('ExtendedYaleB', 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
%% Split the data into training and testing sets
[trainImgs, testImgs] = splitEachLabel(Dataset, 0.8, 'randomized');
%% Extract local patches from the training images using the extractLBPFeatures function
numNeighbors = 8;
radius = 1;
numBins = numNeighbors*(numNeighbors-1)+3;
trainFeatures = cell(numel(trainImgs.Files),1);
for i = 1:numel(trainImgs.Files)
img = readimage(trainImgs,i);
trainFeatures{i} = LBPFeatures(img, numNeighbors, radius, numBins);
end
%% Train the local ridge regression model using the fitrlinear function and local ridge regression
lambda = 1;
span = 0.5;
for i = 1:numel(trainImgs.Files)
features = trainFeatures{i};
label = double(trainImgs.Labels(i));
idx = setdiff(1:numel(trainFeatures),i);
neighbors = [];
neighborLabels = [];
for j = 1:numel(idx)
neighbor = trainFeatures{idx(j)};
if size(neighbor, 2) == size(features, 2) % Check dimensions
neighbors = [neighbors; neighbor];
neighborLabels = [neighborLabels; double(trainImgs.Labels(idx(j)))];
end
end
mdlLocal = fitrlinear(neighbors, neighborLabels, 'Learner', 'leastsquares', 'Lambda', lambda);
yhat = zeros(size(features,1),1);
for j = 1:size(features,1)
patch = features(j,:);
pred = predict(mdlLocal, patch);
dist = pdist2(patch, neighbors);
w = exp(-dist.^2/(2*span^2));
yhat(j) = sum(w.*pred)/sum(w);
end
trainFeatures{i} = yhat;
end
trainFeatures = cell2mat(trainFeatures);
%% Convert text labels to numeric values
[trainImgs.Labels, labelIdx] = grp2idx(trainImgs.Labels);
%% Train the linear regression model on the modified LBP features
mdl = fitrlinear(trainFeatures, labelIdx, 'Learner', 'leastsquares', 'Lambda', lambda);
%% Save the model to a file
save('face_recognition_model.mat', 'mdl');
%% Extract local patches from the testing images and make predictions using the predict function
testFeatures = cell(numel(testImgs.Files),1);
for i = 1:numel(testImgs.Files)
img = readimage(testImgs,i);
testFeatures{i} = LBPFeatures(img, numNeighbors, radius, numBins);
end
testFeatures = cell2mat(testFeatures);
%% Convert test labels to numeric for prediction
[testImgs.Labels, testLabelIdx] = grp2idx(testImgs.Labels);
%% Perform prediction and convert numeric predictions back to text labels
predictionsIdx = predict(mdl, testFeatures);
predictions = idx2grp(predictionsIdx, labelIdx);
%% Evaluate the performance of the model using the confusionmat and classificationReport functions
confMat = confusionmat(testImgs.Labels, predictions);
disp(confMat);
classification_report = classificationReport(testImgs.Labels,predictions);
disp(classification_report);
%% Load the saved model from a file
load('face_recognition_model.mat');
%% Use the loaded model for prediction
testImg = imread('ExtendedYaleB\yaleB11\yaleB11_P00A+000E+00_result.jpg');
testFeatures = extract+LBPFeatures(testImg, numNeighbors, radius, numBins);
prediction = predict(mdl, testFeatures);
and LBPFeatures function
function features = LBPFeatures(img, numNeighbors, radius, numBins)
% Computes Local Binary Pattern (LBP) features for an input grayscale image
%
% Inputs:
% img: input grayscale image
% numNeighbors: number of neighbors to consider for each pixel
% radius: radius of the LBP circle
% numBins: number of bins to use in the LBP histogram
%
% Outputs:
% features: LBP feature vector for the input image
% Compute the LBP texture map
lbpImg = extractLBP(img, numNeighbors, radius);
% Compute the LBP histogram
[counts, edges] = histcounts(lbpImg, numBins);
% Normalize the histogram
features = counts / sum(counts);
end
%***********************************
function lbpImg = extractLBP(img, numNeighbors, radius)
% Compute the local binary pattern (LBP) image of an input grayscale image.
%
% Inputs:
% - img: the input grayscale image.
% - numNeighbors: the number of neighbors to consider when computing LBP.
% - radius: the radius of the circular neighborhood around each pixel.
%
% Outputs:
% - lbpImg: the computed LBP image.
% Pad the input image with zeros to avoid boundary effects
img = padarray(img, [radius radius], 'replicate', 'both');
% Precompute the circular neighborhood coordinates
theta = 0:(2*pi/numNeighbors):(2*pi*(1-1/numNeighbors));
offsets = round(radius * [cos(theta)', sin(theta)']);
% Compute the LBP image by comparing each pixel to its neighbors
lbpImg = zeros(size(img));
for i = 1:numNeighbors
neighborImg = img((radius+1+offsets(i,1)):(end-radius+offsets(i,1)), ...
(radius+1+offsets(i,2)):(end-radius+offsets(i,2)));
neighborImg = imresize(neighborImg, size(img), 'nearest');
lbpImg = lbpImg + (img > neighborImg) * 2^(i-1);
end
% Crop the LBP image to remove the padding
lbpImg = lbpImg((radius+1):(end-radius), (radius+1):(end-radius));
end
and this the Link of my dataset:
thanks all

Respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by