Index in position 2 exceeds array bounds. Index must not exceed 59. Error in FRM_PLS (line 27) features = trainFeaturesMat(:, i);

2 visualizaciones (últimos 30 días)
This is the error for this code:
% PLS Method
% 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;
trainFeatures = cell(numel(trainImgs.Files), 1);
for i = 1:numel(trainImgs.Files)
img = readimage(trainImgs, i);
trainFeatures{i} = extractLBPFeatures(img, 'NumNeighbors', numNeighbors, 'Radius', radius);
end
% Perform PLS regression
numComponents = 50; % Number of PLS components to retain
trainingLabels = double(trainImgs.Labels);
trainFeaturesMat = cell2mat(trainFeatures);
[~,~,~,~,betaPLS,~] = plsregress(trainFeaturesMat, trainingLabels, numComponents);
% Project train features onto PLS subspace
trainFeaturesPLS = cell(numel(trainImgs.Files), 1);
for i = 1:numel(trainImgs.Files)
features = trainFeaturesMat(:, i);
trainFeaturesPLS{i} = features' .* betaPLS;
end
% Train the linear regression model on the modified LBP features
trainFeaturesPLS = cell2mat(trainFeaturesPLS);
mdl = fitrlinear(trainFeaturesPLS, trainingLabels, 'Learner', 'leastsquares', 'Lambda', lambda);
% Save the model to a file
save('my_Model.mat', 'mdl', 'betaPLS');
% 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} = extractLBPFeatures(img, 'NumNeighbors', numNeighbors, 'Radius', radius);
end
% Project test features onto PLS subspace
testFeaturesPLS = cell(numel(testImgs.Files), 1);
for i = 1:numel(testImgs.Files)
features = cell2mat(testFeatures(i));
testFeaturesPLS{i} = features' * betaPLS;
end
% Convert test features to matrix
testFeaturesPLS = cell2mat(testFeaturesPLS);
% Make predictions using the loaded model
load('my_Model.mat', 'mdl');
predictions = predict(mdl, testFeaturesPLS);
% Evaluate the performance of the model using the confusionmat and classificationReport functions
confMat = confusionmat(testImgs.Labels, predictions);
classificationReport = classificationReport(testImgs.Labels, predictions);
% Use the loaded model for prediction
testImg = imread('test_image.jpg');
testFeatures = extractLBPFeatures(testImg, 'NumNeighbors', numNeighbors, 'Radius', radius);
testFeaturesPLS = testFeatures' * betaPLS;
prediction = predict(mdl, testFeaturesPLS);
Suggest a solution

Respuestas (1)

Cris LaPierre
Cris LaPierre el 10 de Ag. de 2023
Editada: Cris LaPierre el 12 de Ag. de 2023
numel(testImgs.Files) is apparently greater than the number of columns in trainFeaturesMat. The number of columns is 59, so when your loop index exceeds the number of columns, your indexing operation throws the error you are seeing.
Here is a simplified example of the error.
A = 1;
% This works
A(1,1)
ans = 1
% This produces the same error
A(1,2)
Index in position 2 exceeds array bounds. Index must not exceed 1.
You need to write your code in such a way so that your index does not exceed your variable dimensions. Perhaps something like this?
for i = 1:size(trainFeaturesMat,2)
features = trainFeaturesMat(:, i);
trainFeaturesPLS{i} = features' .* betaPLS;
end
  1 comentario
Abdelrahman
Abdelrahman el 11 de Ag. de 2023
I face Issue with this code:
Error using classreg.learning.Linear.prepareDataCR
X and Y do not have the same number of observations.
Error in RegressionLinear.prepareData (line 629)
[X,Y,W,dataSummary] = classreg.learning.Linear.prepareDataCR(...
Error in classreg.learning.FitTemplate/fit (line 246)
this.PrepareData(X,Y,this.BaseFitObjectArgs{:});
Error in RegressionLinear.fit (line 488)
[varargout{1:nargout}] = fit(temp,X,Y);
Error in fitrlinear (line 147)
[varargout{1:nargout}] = RegressionLinear.fit(X,y,RemainingArgs{:});
Error in FRM_PLS (line 39)
mdl = fitrlinear(trainFeaturesPLS, trainingLabels, 'Learner', 'leastsquares', 'Lambda', lambda);
Original code:
% PLS Method
% 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;
trainFeatures = cell(numel(trainImgs.Files), 1);
for i = 1:numel(trainImgs.Files)
img = readimage(trainImgs, i);
trainFeatures{i} = extractLBPFeatures(img, 'NumNeighbors', numNeighbors, 'Radius', radius);
end
%% Perform PLS regression
numComponents = 50; % Number of PLS components to retain
trainingLabels = double(trainImgs.Labels);
trainFeaturesMat = cell2mat(trainFeatures);
[~,~,~,~,betaPLS,~] = plsregress(trainFeaturesMat, trainingLabels, numComponents);
% Project train features onto PLS subspace
trainFeaturesPLS = cell(numel(trainImgs.Files), 1);
for i = 1:size(trainFeaturesMat, 2)
features = trainFeaturesMat(:, i);
trainFeaturesPLS{i} = features .* betaPLS';
end
% Convert train features to a consistent data type
trainFeaturesPLS = cellfun(@double, trainFeaturesPLS, 'UniformOutput', false);
% Convert train features to matrix
trainFeaturesPLS = cell2mat(trainFeaturesPLS);
%% Train the linear regression model on the modified LBP features
lambda = 0.1; % Set the value of lambda
mdl = fitrlinear(trainFeaturesPLS, trainingLabels, 'Learner', 'leastsquares', 'Lambda', lambda);
% Save the model to a file
save('FRM_PLS.mat', 'mdl', 'betaPLS');
% 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} = extractLBPFeatures(img, 'NumNeighbors', numNeighbors, 'Radius', radius);
end
% Project test features onto PLS subspace
testFeaturesPLS = cell(numel(testImgs.Files), 1);
for i = 1:numel(testImgs.Files)
features = cell2mat(testFeatures(i));
testFeaturesPLS{i} = features' * betaPLS;
end
% Convert test features to matrix
testFeaturesPLS = cell2mat(testFeaturesPLS);
% Make predictions using the loaded model
load('my_Model.mat', 'mdl');
predictions = predict(mdl, testFeaturesPLS);
% Evaluate the performance of the model using the confusionmat and classificationReport functions
confMat = confusionmat(testImgs.Labels, predictions);
classificationReport = classificationReport(testImgs.Labels, predictions);
% Use the loaded model for prediction
testImg = imread('test_image.jpg');
testFeatures = extractLBPFeatures(testImg, 'NumNeighbors', numNeighbors, 'Radius', radius);
testFeaturesPLS = testFeatures' * betaPLS;
prediction = predict(mdl, testFeaturesPLS);
Link of Dataset
I want to solve this issue and can't understand where error

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by