Error using num2str Input to num2str must be numeric. Error in face_recognition_model (line 59) categoryNames = cellstr(num2str(uniqueLabels));
显示 更早的评论
%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.7, '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 = vertcat(trainFeatures{idx});
neighborLabels = double(trainImgs.Labels(idx));
mdl = fitrlinear(neighbors,neighborLabels,'Learner','leastsquares','Lambda',lambda);
yhat = zeros(size(features));
for j = 1:size(features,1)
patch = features(j,:);
pred = predict(mdl,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);
%% Train the linear regression model on the modified LBP features
mdl = fitrlinear(trainFeatures,double(trainImgs.Labels),'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);
predictions = predict(mdl,testFeatures);
%% Evaluate the performance of the model using the confusionmat and classificationReport functions
confMat = confusionmat(testImgs.Labels,predictions);
classificationReport = classificationReport(testImgs.Labels,predictions);
%% Load the saved model from a file
load('face_recognition_model.mat');
%% Use the loaded model for prediction
testImg = imread('test_image.jpg');
testFeatures = extractLBPFeatures(testImg, numNeighbors, radius, numBins);
prediction = predict(mdl,testFeatures);
11 个评论
the cyclist
2023-8-4
Neither the num2str function nor the uniqueLabels variable appears in the code you posted, so I don't see how we can help you.
All we can conclude is that uniqueLabels is not numeric, because the error message tells you that.
Post the code snippet that gives the error and the data required to run that snippet.
Abdelrahman
2023-8-4
the cyclist
2023-8-4
编辑:the cyclist
2023-8-4
Again, you have not provided the data we need to run your code.
Again, the error message is quite clear: the two inputs to confusionmat are not the same data type. (For example, one might be a character array, and the other one is numeric.) So, you need to convert the data type of one of them. But, since we don't know what those variables are, we cannot suggest a specific solution.
Abdelrahman
2023-8-4
编辑:Walter Roberson
2023-8-5
the cyclist
2023-8-5
Your code did not run for me. It may be because I don't have the proper toolbox.
But, we don't need to process all of your images to solve the problem. If you just upload testImgs and predictions in a MAT file, then we can see why
confMat = confusionmat(testImgs.Labels,predictions);
doesn't run, and suggest a solution.
Image Analyst
2023-8-5
编辑:Image Analyst
2023-8-5
I don't see this line
categoryNames = cellstr(num2str(uniqueLabels));
in the code you posted.
If you have any more questions, then attach face_recognition_model.m, and your data with the paperclip icon after you read this:
Abdelrahman
2023-8-5
Walter Roberson
2023-8-5
Please show the output for
which -all LBPFeatures
which -all extractLBPFeatures
LBPFeatures does not appear to exist in MATLAB at all. You must not be using the Mathworks extractLBPFeatures as that takes a different set of parameters than you are using.
Abdelrahman
2023-8-6
Walter Roberson
2023-8-6
I already showed you in https://www.mathworks.com/matlabcentral/answers/2004402-undefined-function-extractlbp-for-input-arguments-of-type-uint8#comment_2839377 the syntax you need to use.
Abdelrahman
2023-8-6
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Image Category Classification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!