-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Santos Safrao
authored and
Santos Safrao
committed
Dec 8, 2023
1 parent
fddb9ae
commit 6ee69af
Showing
5 changed files
with
107 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
%% Load data | ||
testDataUsage = TestDataUsage.Subsets; | ||
n = 2; | ||
[trainData, testData, testLabels] = prepareData(testDataUsage, n); | ||
|
||
%% Train model | ||
numDimReferenceSubspace = 10; | ||
numDimInputSubspace = 4; | ||
sigma = 1; | ||
|
||
model = KMSM(trainData,... | ||
numDimReferenceSubspace,... | ||
numDimInputSubspace,... | ||
sigma,... | ||
testLabels); | ||
|
||
%% Evaluate model | ||
modelEvaluation = model.evaluate(testData); | ||
modelEvaluation.printResults(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,19 @@ | ||
%% Load Data | ||
clear; | ||
clc; | ||
load('TsukubaHandDigitsDataset24x24.mat') | ||
% Check if the variable 'trainData' and 'testData' do not exist in the workspace | ||
% if ~(exist('trainData', 'var') == 1 && exist('testData', 'var') == 1) | ||
% % If they don't exist, load the data from the .mat file | ||
% load('TsukubaHandDigitsDataset24x24.mat'); | ||
% end | ||
% you can use the following code to convert the test data format from 3d to 4d | ||
testData = subsetTestData(testData, 2); | ||
% specific_class = 5; | ||
%% to accomodate for MATLAB indexing | ||
% specific_class = specific_class + 1; | ||
training_data = cvlNormalize(trainData); | ||
testing_data = cvlNormalize(testData); | ||
% testing_data = testData(:, :, specific_class); | ||
size_of_test_data = size(testing_data); | ||
%% Load data | ||
testDataUsage = TestDataUsage.Subsets; | ||
n = 2; | ||
[trainData, testData, testLabels] = prepareData(testDataUsage, n); | ||
|
||
% get number of elements of size_of_test_data | ||
array_size = numel(size_of_test_data); | ||
|
||
if array_size == 4 | ||
% do nothing | ||
num_sets = size_of_test_data(3); | ||
num_classes = size_of_test_data(4); | ||
elseif array_size == 3 | ||
num_sets = 1; | ||
num_classes = size_of_test_data(3); | ||
else | ||
num_classes = 1; | ||
num_sets = 1; | ||
end | ||
|
||
%% Train Model | ||
num_dim_reference_subspaces = 10; | ||
num_dim_input_subpaces = 5; | ||
%% Train model | ||
numDimReferenceSubspace = 10; | ||
numDimInputSubspace = 4; | ||
sigma = 1; | ||
|
||
reference_subspaces = cvlKernelBasisVector(training_data, num_dim_reference_subspaces, sigma); | ||
input_subspaces = cvlKernelBasisVector(testing_data, num_dim_input_subpaces, sigma); | ||
% save('reference_subspaces.mat', 'reference_subspaces'); | ||
% reference_subspaces = reference_subspaces(:, :, 1); | ||
tic; | ||
%% Recognition Phase | ||
similarities = cvlKernelCanonicalAngles(training_data,reference_subspaces,... | ||
testing_data, input_subspaces, sigma); | ||
similarities = similarities(:, :, end, end); | ||
% End timing and display the elapsed time | ||
elapsedTime = toc; | ||
fprintf('The code block executed in %.5f seconds.\n', elapsedTime); | ||
model_evaluation = ModelEvaluation(similarities, generateLabels(num_classes, num_sets)); | ||
|
||
displayModelResults('Kernel Mutual Subspace Methods', model_evaluation); | ||
model = KMSM(trainData,... | ||
numDimReferenceSubspace,... | ||
numDimInputSubspace,... | ||
sigma,... | ||
testLabels); | ||
|
||
%% Print preditions | ||
% disp(model_evaluation.predicted_labels); | ||
% disp(model_evaluation.true_labels); | ||
% disp(similarities) | ||
% plotSimilarities(similarities) | ||
%% Evaluate model | ||
modelEvaluation = model.evaluate(testData); | ||
modelEvaluation.printResults(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
classdef KMSM | ||
properties | ||
name = 'Kernel Mutual Subspace Method'; | ||
trainData; | ||
referenceSubspaces; | ||
numDimInputSubspace; | ||
numDimReferenceSubspace; | ||
sigma; | ||
trueTestLabels; | ||
end | ||
|
||
methods | ||
function obj = KMSM(trainData, numDimReferenceSubspace, numDimInputSubspace, sigma, labels) | ||
obj.trainData = cvlNormalize(trainData); | ||
obj.sigma = sigma; | ||
obj.numDimReferenceSubspace = numDimReferenceSubspace; | ||
obj.numDimInputSubspace = numDimInputSubspace; | ||
obj.trueTestLabels = labels; | ||
obj.referenceSubspaces = cvlKernelBasisVector(obj.trainData,... | ||
obj.numDimReferenceSubspace,... | ||
obj.sigma); | ||
end | ||
|
||
|
||
% Returns the predicted labels for the test data | ||
function prediction = predict(obj, testData) | ||
similarityScores = obj.getSimilarityScores(testData); | ||
eval = ModelEvaluation(similarityScores, obj.trueTestLabels, obj.name); | ||
prediction = eval.predicted_labels; | ||
end | ||
|
||
% Returns the similarity scores for the test data (same as probabilities) | ||
function probabilities = predictProb(obj, testData) | ||
probabilities = obj.getSimilarityScores(testData); | ||
end | ||
|
||
% Returns the evaluation object for the test data | ||
function eval = evaluate(obj, testData) | ||
similarityScores = obj.getSimilarityScores(testData); | ||
eval = ModelEvaluation(similarityScores, obj.trueTestLabels, obj.name); | ||
end | ||
end | ||
|
||
methods (Access = private) | ||
function scores = getSimilarityScores(obj, testData) | ||
testData = cvlNormalize(testData); | ||
inputSubspace = cvlKernelBasisVector(testData,... | ||
obj.numDimInputSubspace,... | ||
obj.sigma); | ||
similarities = cvlKernelCanonicalAngles(obj.trainData,... | ||
obj.referenceSubspaces,... | ||
testData,... | ||
inputSubspace,... | ||
obj.sigma); | ||
scores = similarities(:, :, end, end); | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters