-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_run.m
More file actions
76 lines (64 loc) · 2.06 KB
/
Copy pathexample_run.m
File metadata and controls
76 lines (64 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
% EXAMPLE_RUN Simple end-to-end test on the example dataset.
%
% This script is for local testing only and will NOT be used by the autograder.
%
% It attempts to:
% - Load 'project_data_example.mat'
% - Build SVD-based features with svd_features
% - Train an LDA classifier with lda_train
% - Predict on the test set with lda_predict
% - Print the test accuracy
clear; clc;
if ~isfile('project_data_example.mat')
fprintf('Example data file project_data_example.mat not found.\n');
return;
end
data = load('project_data_example.mat');
X_train = data.X_train; % N_train x m x n
y_train = data.y_train; % N_train x 1
X_test = data.X_test; % N_test x m x n
y_test = data.y_test; % N_test x 1
[N_train, m, n] = size(X_train);
[N_test, ~, ~] = size(X_test);
fprintf('X_train size: %d x %d x %d\n', N_train, m, n);
fprintf('X_test size: %d x %d x %d\n', N_test, m, n);
p = min(5, min(m, n));
fprintf('Using p = %d leading singular values for features.\n', p);
% Build feature matrices
try
Xf_train = zeros(N_train, p + 2);
for i = 1:N_train
img = squeeze(X_train(i, :, :));
feat = svd_features(img, p);
Xf_train(i, :) = feat(:).';
end
Xf_test = zeros(N_test, p + 2);
for i = 1:N_test
img = squeeze(X_test(i, :, :));
feat = svd_features(img, p);
Xf_test(i, :) = feat(:).';
end
catch ME
fprintf('Error while computing features: %s\n', ME.message);
fprintf('Implement svd_features.m before running this example.\n');
return;
end
% Train LDA
try
[w, threshold] = lda_train(Xf_train, y_train);
catch ME
fprintf('Error while training LDA: %s\n', ME.message);
fprintf('Implement lda_train.m before running this example.\n');
return;
end
% Predict
try
y_pred = lda_predict(Xf_test, w, threshold);
catch ME
fprintf('Error while predicting with LDA: %s\n', ME.message);
fprintf('Implement lda_predict.m before running this example.\n');
return;
end
% Compute accuracy
accuracy = mean(y_pred(:) == y_test(:));
fprintf('Example test accuracy: %.3f\n', accuracy);