Skip to content

Commit 2398bc8

Browse files
Santos SafraoSantos Safrao
Santos Safrao
authored and
Santos Safrao
committed
first commit
0 parents  commit 2398bc8

25 files changed

+1802
-0
lines changed

Diff for: LICENSE

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Computer Vision Laboratory, University of Tsukuba
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+

Diff for: data/TsukubaHandDigitsDataset24x24.mat

723 KB
Binary file not shown.

Diff for: docs/README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Mutual Subspace Methods Repository
2+
3+
Welcome to the Mutual Subspace Methods Repository! Here, we host a comprehensive collection of mutual subspace methods and their respective implementations, aiming to provide resources and examples for researchers and practitioners interested in subspace methods.
4+
5+
## Overview
6+
Mutual Subspace Methods (MSMs) are a category of algorithms used for pattern recognition and classification. They operate by mapping data into subspaces and measuring the similarity between these subspaces. This repository includes various MSMs and their kernelized versions, providing implementations and examples to facilitate understanding and application.
7+
8+
## Available Methods
9+
In this repository, you will find implementations for the following methods:
10+
11+
- [x] Mutual Subspace Method (MSM)
12+
- [x] Constrained Mutual Subspace Method (CMSM)
13+
- [ ] Orthogonal Mutual Subspace Method (OMSM)
14+
- [x] Kernel Mutual Subspace Method (KMSM)
15+
- [ ] Kernel Constrained Mutual Subspace Method (KCMSM)
16+
- [ ] Kernel Orthogonal Mutual Subspace Method (KOMSM)
17+
- [ ] Random Fourier Features MSM (RFFMSM)
18+
- [ ] K-means KOMSM
19+
- [ ] RFF + K-means KOMSM
20+
21+
## Sample Implementation
22+
Below, we provide a sample MATLAB implementation for the Mutual Subspace Method (MSM). The implementation demonstrates how to compute the similarity between two subspaces and calculate the classification accuracy.
23+
24+
```matlab
25+
load("data/CVLABFace2.mat")
26+
training_data = X1;
27+
testing_data = X2;
28+
29+
[~, num_samples, ~] = size(training_data);
30+
[num_dim, num_samples_per_set, num_sets, num_classes] = size(testing_data);
31+
32+
num_dim_reference_subspaces = 20;
33+
num_dim_input_subpaces = 5;
34+
35+
reference_subspaces = computeBasisVectors(training_data, num_dim_reference_subspaces);
36+
input_subspaces = computeBasisVectors(testing_data, num_dim_input_subpaces);
37+
similarities = computeSubspacesSimilarities(reference_subspaces, input_subspaces);
38+
39+
model_evaluation = ModelEvaluation(similarities(:, :, end, end), generateLabels(size(testing_data, 3), num_classes));
40+
41+
displayModelResults('Mutual Subspace Methods', model_evaluation);
42+
```
43+
44+
Each example implementation for the methods listed above can be found in files named `example_(name_of_the_method).m`.
45+
46+
## Datasets
47+
The repository includes examples and implementations that utilize the following datasets:
48+
49+
1. **CVLabFace**: A simple sample dataset consisting of 270 samples, designed to provide a straightforward example for implementation and testing.
50+
2. **TsukubaHand24x24**: A comprehensive dataset created by the Computer Vision Lab of the University of Tsukuba. It contains more than 1.4 million images, offering a rich resource for testing and validating mutual subspace methods.
51+
52+
## Further Reading and References
53+
To gain a deeper understanding of Mutual Subspace Methods and their applications, we recommend exploring the following papers and resources:
54+
55+
### Basics and Fundamentals
56+
- [Subspace Methods](http://www.cvlab.cs.tsukuba.ac.jp/~kfukui/english/epapers/subspace_method.pdf): A comprehensive guide to subspace methods, providing the theoretical background and practical applications.
57+
58+
### Specific Methods
59+
- [Comparison between Constrained Mutual Subspace Method and Orthogonal Mutual Subspace Method](https://www.cs.tsukuba.ac.jp/internal/techreport/data/CS-TR-06-7.pdf): A detailed comparison between CMSM and OMSM.
60+
- [Face Recognition with the Multiple Constrained Mutual Subspace Method](http://www.cvlab.cs.tsukuba.ac.jp/~kfukui/english/epapers/AVBPA05.pdf): An application of CMSM in face recognition.
61+
- [Hand Shape Recognition based on Kernel Orthogonal Mutual Subspace Method](http://www.cvlab.cs.tsukuba.ac.jp/~kfukui/english/epapers/MVA2009.pdf): Discussing the application of KOMSM in hand shape recognition.
62+
63+
## Contribution and Collaboration
64+
We welcome contributions and collaborations from the community! Feel free to fork the repository, make your changes, and submit a pull request. If you have any questions or suggestions, feel free to open an issue or contact the maintainers.
65+
66+
Let's work together to enhance the understanding and application of Mutual Subspace Methods!

Diff for: examples/cmsm.m

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
%% Load Data
2+
clear;
3+
clc;
4+
% load('TsukubaHandDigitsDataset.mat')
5+
% Check if the variable 'trainData' and 'testData' do not exist in the workspace
6+
if ~(exist('trainData', 'var') == 1 && exist('testData', 'var') == 1)
7+
% If they don't exist, load the data from the .mat file
8+
load('TsukubaHandDigitsDataset24x24.mat');
9+
end
10+
% you can use the following code to convert the test data format from 3d to 4d
11+
% testData = subsetTestData(testData, 4);
12+
specific_class = 1;
13+
class_num = 6;
14+
%% to accomodate for MATLAB indexing
15+
specific_class = specific_class + 1;
16+
training_data = trainData;
17+
% testing_data = testData;
18+
testing_data = testData(:, :, specific_class);
19+
size_of_test_data = size(testing_data);
20+
dim = size_of_test_data(1);
21+
del_subspace_dim = 3;
22+
23+
% get number of elements of size_of_test_data
24+
array_size = numel(size_of_test_data);
25+
26+
if array_size == 4
27+
% do nothing
28+
num_samples_per_set = size_of_test_data(2);
29+
num_sets = size_of_test_data(3);
30+
num_classes = size_of_test_data(4);
31+
elseif array_size == 3
32+
num_sets = 1;
33+
num_classes = size_of_test_data(3);
34+
else
35+
num_classes = 1;
36+
num_sets = 1;
37+
end
38+
39+
%% Train Model
40+
tic;
41+
num_dim_reference_subspaces = 10;
42+
num_dim_input_subpaces = 5;
43+
44+
reference_subspaces = cvlBasisVector(training_data, num_dim_reference_subspaces);
45+
input_subspaces = cvlBasisVector(testing_data, num_dim_input_subpaces);
46+
47+
save('reference_subspaces.mat', 'reference_subspaces');
48+
% reference_subspaces = reference_subspaces(:, :, 1);
49+
% Generalizated difference subspace(Constraint Subspace)
50+
P = zeros(dim, dim);
51+
for I=1:class_num
52+
P = P + reference_subspaces(:,:,I)*reference_subspaces(:,:,I)';
53+
end
54+
[B, C] = eig(P);
55+
C = diag(C);
56+
[~, index] = sort(C,'descend');
57+
B = B(:,index); C = C(index);
58+
difference = B(:,del_subspace_dim+1:rank(P))';
59+
60+
difference_subspace = zeros(size(difference,1), num_dim_reference_subspaces, size(reference_subspaces,3));
61+
for I=1:size(reference_subspaces,3)
62+
difference_subspace(:,:,I) = orth(difference*reference_subspaces(:,:,I));
63+
end
64+
65+
% process input difference subspace
66+
if array_size == 4
67+
input_difference_subspace = zeros(size(difference,1), num_dim_input_subpaces, num_sets, num_classes);
68+
for I=1:num_classes
69+
for J=1:num_sets
70+
input_difference_subspace(:,:,J,I) = orth(difference*input_subspaces(:,:,J,I));
71+
end
72+
end
73+
elseif array_size == 3
74+
input_difference_subspace = zeros(size(difference,1), num_dim_input_subpaces, num_classes);
75+
for I=1:num_classes
76+
input_difference_subspace(:,:,I) = orth(difference*input_subspaces(:,:,I));
77+
end
78+
else
79+
input_difference_subspace = orth(difference*input_subspaces);
80+
end
81+
82+
reference_subspaces= difference_subspace;
83+
input_subspaces = input_difference_subspace;
84+
85+
reference_subspaces = reference_subspaces(:, :, 1:3);
86+
87+
tic;
88+
%% Recognition Phase
89+
% convert reference and input subspace to cells
90+
reference_subspaces = mat2cell(reference_subspaces, size(reference_subspaces, 1), size(reference_subspaces, 2), ones(1, size(reference_subspaces, 3)));
91+
input_subspaces = mat2cell(input_subspaces, size(input_subspaces, 1), size(input_subspaces, 2), ones(1, size(input_subspaces, 3)));
92+
similarities = cvlCanonicalAngles(reference_subspaces, input_subspaces);
93+
similarities = similarities(:, :, end, end);
94+
% End timing and display the elapsed time
95+
elapsedTime = toc;
96+
fprintf('The code block executed in %.5f seconds.\n', elapsedTime);
97+
model_evaluation = ModelEvaluation(similarities, generateLabels(num_classes, num_sets, specific_class));
98+
99+
displayModelResults('Contained Mutual Subspace Methods', model_evaluation);
100+
101+
%% Print preditions
102+
disp(model_evaluation.predicted_labels);
103+
disp(model_evaluation.true_labels);
104+
disp(similarities);
105+
plotSimilarityMatrix(similarities, 'CMSM')
106+
% disp(similarities)
107+
% plotSimilarities(similarities)

Diff for: examples/kmsm.m

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
%% Load Data
2+
clear;
3+
clc;
4+
load('TsukubaHandDigitsDataset24x24.mat')
5+
% Check if the variable 'trainData' and 'testData' do not exist in the workspace
6+
% if ~(exist('trainData', 'var') == 1 && exist('testData', 'var') == 1)
7+
% % If they don't exist, load the data from the .mat file
8+
% load('TsukubaHandDigitsDataset24x24.mat');
9+
% end
10+
% you can use the following code to convert the test data format from 3d to 4d
11+
testData = subsetTestData(testData, 2);
12+
% specific_class = 5;
13+
%% to accomodate for MATLAB indexing
14+
% specific_class = specific_class + 1;
15+
training_data = cvlNormalize(trainData);
16+
testing_data = cvlNormalize(testData);
17+
% testing_data = testData(:, :, specific_class);
18+
size_of_test_data = size(testing_data);
19+
20+
% get number of elements of size_of_test_data
21+
array_size = numel(size_of_test_data);
22+
23+
if array_size == 4
24+
% do nothing
25+
num_sets = size_of_test_data(3);
26+
num_classes = size_of_test_data(4);
27+
elseif array_size == 3
28+
num_sets = 1;
29+
num_classes = size_of_test_data(3);
30+
else
31+
num_classes = 1;
32+
num_sets = 1;
33+
end
34+
35+
%% Train Model
36+
num_dim_reference_subspaces = 10;
37+
num_dim_input_subpaces = 5;
38+
sigma = 1;
39+
40+
reference_subspaces = cvlKernelBasisVector(training_data, num_dim_reference_subspaces, sigma);
41+
input_subspaces = cvlKernelBasisVector(testing_data, num_dim_input_subpaces, sigma);
42+
% save('reference_subspaces.mat', 'reference_subspaces');
43+
% reference_subspaces = reference_subspaces(:, :, 1);
44+
tic;
45+
%% Recognition Phase
46+
similarities = cvlKernelCanonicalAngles(training_data,reference_subspaces,...
47+
testing_data, input_subspaces, sigma);
48+
similarities = similarities(:, :, end, end);
49+
% End timing and display the elapsed time
50+
elapsedTime = toc;
51+
fprintf('The code block executed in %.5f seconds.\n', elapsedTime);
52+
model_evaluation = ModelEvaluation(similarities, generateLabels(num_classes, num_sets));
53+
54+
displayModelResults('Kernel Mutual Subspace Methods', model_evaluation);
55+
56+
%% Print preditions
57+
% disp(model_evaluation.predicted_labels);
58+
% disp(model_evaluation.true_labels);
59+
% disp(similarities)
60+
% plotSimilarities(similarities)

Diff for: examples/msm.m

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
%% Load Data
2+
clear;
3+
clc;
4+
load('TsukubaHandDigitsDataset24x24.mat')
5+
% Check if the variable 'trainData' and 'testData' do not exist in the workspace
6+
% if ~(exist('trainData', 'var') == 1 && exist('testData', 'var') == 1)
7+
% % If they don't exist, load the data from the .mat file
8+
% load('TsukubaHandDigitsDataset24x24.mat');
9+
% end
10+
% you can use the following code to convert the test data format from 3d to 4d
11+
testData = subsetTestData(testData, 2);
12+
% specific_class = 5;
13+
%% to accomodate for MATLAB indexing
14+
% specific_class = specific_class + 1;
15+
training_data = trainData;
16+
testing_data = testData;
17+
% testing_data = testData(:, :, specific_class);
18+
size_of_test_data = size(testing_data);
19+
20+
% get number of elements of size_of_test_data
21+
array_size = numel(size_of_test_data);
22+
23+
if array_size == 4
24+
% do nothing
25+
num_sets = size_of_test_data(3);
26+
num_classes = size_of_test_data(4);
27+
elseif array_size == 3
28+
num_sets = 1;
29+
num_classes = size_of_test_data(3);
30+
else
31+
num_classes = 1;
32+
num_sets = 1;
33+
end
34+
35+
%% Train Model
36+
num_dim_reference_subspaces = 1;
37+
num_dim_input_subpaces = 1;
38+
39+
reference_subspaces = cvlBasisVector(training_data, num_dim_reference_subspaces);
40+
input_subspaces = cvlBasisVector(testing_data, num_dim_input_subpaces);
41+
% save('reference_subspaces.mat', 'reference_subspaces');
42+
% reference_subspaces = reference_subspaces(:, :, 1);
43+
tic;
44+
%% Recognition Phase
45+
similarities = cvlCanonicalAngles(reference_subspaces, input_subspaces);
46+
similarities = similarities(:, :, end, end);
47+
% End timing and display the elapsed time
48+
elapsedTime = toc;
49+
fprintf('The code block executed in %.5f seconds.\n', elapsedTime);
50+
model_evaluation = ModelEvaluation(similarities, generateLabels(num_classes, num_sets));
51+
52+
displayModelResults('Mutual Subspace Methods', model_evaluation);
53+
54+
%% Print preditions
55+
% disp(model_evaluation.predicted_labels);
56+
% disp(model_evaluation.true_labels);
57+
% disp(similarities)
58+
% plotSimilarities(similarities)

Diff for: install.m

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addpath(genpath('..')) % Add the subfolders of the project to the path

Diff for: reference_subspaces.mat

259 KB
Binary file not shown.

0 commit comments

Comments
 (0)