Poor classification accuracy when using PCA
2 次查看(过去 30 天)
显示 更早的评论
I'm trying to classify a set of images using PCA. The training set contains ~1800 images of 380 persons, each image with the person's unique label (ID). The testing set contains ~750 images of the same 380 persons (different images), each image with the person's corresponding label (ID). All images are 160 x 128 pixels.
My code is as follows.
img_train{i} contains the i-th original image in the training set, img_test{i} contains the i-th original image in the testing set.
euclide_dist = zeros(total_TrainImageFiles,1);
num_correct_labels = 0;
% Reshape 2D training images into 1D image vectors
train_img = zeros(irow*icol,total_TrainImageFiles);
for i = 1 : total_TrainImageFiles
temp = reshape(img_train{i}',irow*icol,1);
train_img(:,i) = temp;
end
% Calculate mean image vector
mean_face = mean(train_img,2);
% Subtract mean face from all training images
centred_data = train_img - repmat(mean_face, 1, total_TrainImageFiles);
% Determine eigenvectors and eigenvalues using SVD
[U, D, V] = svd(centred_data,0);
d = 10;
% Generate feature vectors from training set for subsequent classification
% Keep the top 'd' eigenvectors
eigenvectors = U(:,1:d);
% Project the training images into the facespace to generate the training feature vectors
train_features = eigenvectors' * centred_data;
% Classify testing images
for i = 1 : total_TestImageFiles
% Reshape 2D test image into 1D image vectors
test_img = reshape(img_test{i}',irow*icol,1);
% Subtract mean face from test image
centred_test_img = double(test_img) - mean_face;
% Project test image onto the facespace
test_features = eigenvectors' * centred_test_img;
% Calculate the euclidian distance of all projected trained images from the projected test image
for j = 1 : total_TrainImageFiles
edist = (norm(train_features(:,j) - test_features))^2;
euclide_dist(j) = edist;
end
% Find the minimum distance and compare class labels
[euclide_dist_min,train_index] = min(euclide_dist);
predicted_labels(i) = training_label(train_index);
if training_label(train_index) == testing_label(i)
num_correct_labels = num_correct_labels + 1;
end
end
% Calculate accuracy
accuracy = double(num_correct_labels) / double(total_TrainImageFiles);
I've tried with d = 10 and d = 50, but my accuracy is very low, ~1.5% to 2.5%. What is wrong with my code?
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!