Skip to content

Commit 81800f8

Browse files
committed
upload codes
1 parent 971c8db commit 81800f8

24 files changed

+111082
-0
lines changed

data/.place_holder

Whitespace-only changes.

eval/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.*~

eval/precision_singlelabel.m

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function [map, precision_at_k, agg_prec] = precision_singlelabel (trn_label, trn_binary, tst_label, tst_binary, mode)
2+
K = size(trn_binary,1);
3+
QueryTimes = size(tst_binary,1);
4+
5+
AP = zeros(QueryTimes,1);
6+
7+
Ns = 1:1:K;
8+
sum_tp = zeros(1, length(Ns));
9+
10+
agg_prec = zeros(1, 11);
11+
12+
for i = 1:QueryTimes
13+
14+
%img_path = tst_list(i,1);
15+
query_label = tst_label(i);
16+
fprintf('query %d\n',i);
17+
query_binary = tst_binary(i,:);
18+
if mode==1
19+
tic
20+
similarity = pdist2(double(trn_binary),double(query_binary),'hamming');
21+
toc
22+
fprintf('Complete Query [Hamming] %.2f seconds\n',toc);
23+
elseif mode ==2
24+
tic
25+
similarity = pdist2(trn_binary,query_binary,'euclidean');
26+
toc
27+
fprintf('Complete Query [Euclidean] %.2f seconds\n',toc);
28+
end
29+
30+
[x2,y2] = sort(similarity);
31+
32+
buffer_yes = trn_label(y2(1:K)) == query_label;
33+
% compute precision
34+
P = cumsum(buffer_yes) ./ Ns';
35+
if (sum(buffer_yes) == 0)
36+
AP(i) = 0;
37+
else
38+
AP(i) = sum(P .* buffer_yes) / sum(buffer_yes);
39+
end
40+
sum_tp = sum_tp + cumsum(buffer_yes)';
41+
42+
if (sum(buffer_yes) > 0)
43+
% recall
44+
R = cumsum(buffer_yes) ./ sum(buffer_yes);
45+
46+
% aggregated precision
47+
for mm = 1: 11
48+
rr = (mm - 1)/10;
49+
idx = find(R >= rr);
50+
agg_prec(mm) = agg_prec(mm) + max(P(idx));
51+
end
52+
end
53+
end
54+
precision_at_k = sum_tp ./ (Ns * QueryTimes);
55+
map = mean(AP);
56+
57+
agg_prec = agg_prec / QueryTimes;
58+
end
59+

eval/readNPY.m

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
3+
function data = readNPY(filename)
4+
% Function to read NPY files into matlab.
5+
% *** Only reads a subset of all possible NPY files, specifically N-D arrays of certain data types.
6+
% See https://github.com/kwikteam/npy-matlab/blob/master/npy.ipynb for
7+
% more.
8+
%
9+
10+
[shape, dataType, fortranOrder, littleEndian, totalHeaderLength, ~] = readNPYheader(filename);
11+
12+
if littleEndian
13+
fid = fopen(filename, 'r', 'l');
14+
else
15+
fid = fopen(filename, 'r', 'b');
16+
end
17+
18+
try
19+
20+
[~] = fread(fid, totalHeaderLength, 'uint8');
21+
22+
% read the data
23+
data = fread(fid, prod(shape), [dataType '=>' dataType]);
24+
25+
if length(shape)>1 && ~fortranOrder
26+
data = reshape(data, shape(end:-1:1));
27+
data = permute(data, [length(shape):-1:1]);
28+
elseif length(shape)>1
29+
data = reshape(data, shape);
30+
end
31+
32+
fclose(fid);
33+
34+
catch me
35+
fclose(fid);
36+
rethrow(me);
37+
end

eval/readNPYheader.m

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
3+
function [arrayShape, dataType, fortranOrder, littleEndian, totalHeaderLength, npyVersion] = readNPYheader(filename)
4+
% function [arrayShape, dataType, fortranOrder, littleEndian, ...
5+
% totalHeaderLength, npyVersion] = readNPYheader(filename)
6+
%
7+
% parse the header of a .npy file and return all the info contained
8+
% therein.
9+
%
10+
% Based on spec at http://docs.scipy.org/doc/numpy-dev/neps/npy-format.html
11+
12+
fid = fopen(filename);
13+
14+
% verify that the file exists
15+
if (fid == -1)
16+
if ~isempty(dir(filename))
17+
error('Permission denied: %s', filename);
18+
else
19+
error('File not found: %s', filename);
20+
end
21+
end
22+
23+
try
24+
25+
dtypesMatlab = {'uint8','uint16','uint32','uint64','int8','int16','int32','int64','single','double', 'logical'};
26+
dtypesNPY = {'u1', 'u2', 'u4', 'u8', 'i1', 'i2', 'i4', 'i8', 'f4', 'f8', 'b1'};
27+
28+
29+
magicString = fread(fid, [1 6], 'char=>char');
30+
31+
if ~strcmp(magicString, '“NUMPY')
32+
error('readNPY:NotNUMPYFile', 'Error: This file does not appear to be NUMPY format based on the header.');
33+
end
34+
35+
majorVersion = fread(fid, [1 1], 'uint8=>uint8');
36+
minorVersion = fread(fid, [1 1], 'uint8=>uint8');
37+
38+
npyVersion = [majorVersion minorVersion];
39+
40+
headerLength = fread(fid, [1 1], 'uint16=>unit16');
41+
42+
totalHeaderLength = 10+headerLength;
43+
44+
arrayFormat = fread(fid, [1 headerLength], 'char=>char');
45+
46+
% to interpret the array format info, we make some fairly strict
47+
% assumptions about its format...
48+
49+
r = regexp(arrayFormat, '''descr''\s*:\s*''(.*?)''', 'tokens');
50+
dtNPY = r{1}{1};
51+
52+
littleEndian = ~strcmp(dtNPY(1), '>');
53+
54+
dataType = dtypesMatlab{strcmp(dtNPY(2:3), dtypesNPY)};
55+
56+
r = regexp(arrayFormat, '''fortran_order''\s*:\s*(\w+)', 'tokens');
57+
fortranOrder = strcmp(r{1}{1}, 'True');
58+
59+
r = regexp(arrayFormat, '''shape''\s*:\s*\((.*?)\)', 'tokens');
60+
shapeStr = r{1}{1};
61+
arrayShape = str2num(shapeStr(shapeStr~='L'));
62+
63+
64+
fclose(fid);
65+
66+
catch me
67+
fclose(fid);
68+
rethrow(me);
69+
end

eval/run_cifar10_pca.m

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
clear;
2+
3+
bit = 512;
4+
examples_to_show = 10;
5+
% load CIFAR10 training set
6+
data_folder = '/media/iis/Data/Data/cifar10/cifar-10-batches-mat/';
7+
X_train = [];
8+
Y_train = [];
9+
for i = 1: 5
10+
datadict = load([data_folder 'data_batch_' num2str(i) '.mat']);
11+
X_train = [X_train; datadict.data];
12+
Y_train = [Y_train; datadict.labels];
13+
end
14+
15+
% load CIFAR10 test set
16+
datadict = load([data_folder 'test_batch.mat']);
17+
X_test = double(datadict.data);
18+
Y_test = datadict.labels;
19+
20+
% center the data
21+
X_train = double(X_train);
22+
sample_mean = mean(X_train, 1);
23+
X_train = (X_train - repmat(sample_mean, size(X_train,1),1));
24+
25+
fprintf('performing PCA ...\n');
26+
[pc, l] = eigs(double(cov(X_train)),bit);
27+
%[coeff,score,latent] = pca(double(X_train));
28+
29+
% reconstruct images
30+
% center data
31+
XX = X_test - repmat(sample_mean, size(X_test, 1),1);
32+
33+
% projection
34+
XX = XX * pc;
35+
36+
% reconstruction
37+
XX = XX *pc' + repmat(sample_mean, size(XX,1),1);
38+
39+
% show reconstructed images
40+
for i = 1: examples_to_show
41+
im = reshape(XX(i,:), [32 32 3]);
42+
im = permute(im, [2 1 3]);
43+
figure; imshow(uint8(im))
44+
end

eval/run_eval_singlelabel.m

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
function run_eval_singlelabel(res_dir)
3+
4+
database.label = readNPY(fullfile(res_dir, 'database_y.npy'));
5+
[max_vals, database.label] = max(database.label');
6+
database.label = database.label';
7+
8+
query.label = readNPY(fullfile(res_dir, 'query_y.npy'));
9+
[max_vals, query.label] = max(query.label');
10+
query.label = query.label';
11+
12+
database.feat = readNPY(fullfile(res_dir, 'database_x.npy'));
13+
query.feat = readNPY(fullfile(res_dir, 'query_x.npy'));
14+
15+
database.binary = database.feat > 0.;
16+
query.binary = query.feat > 0.;
17+
18+
map_file = fullfile(res_dir, 'map.txt');
19+
precision_file = fullfile(res_dir, 'precision-at-k.txt');
20+
pr_file = fullfile(res_dir, 'pr.txt');
21+
22+
% retreival performance
23+
[map, precision_at_k, agg_prec] = precision_singlelabel(database.label, database.binary, ...
24+
query.label, query.binary, 1);
25+
26+
% save results
27+
outfile = fopen(map_file, 'w');
28+
fprintf(outfile, '%.4f\t', map);
29+
fclose(outfile);
30+
31+
P = [[1:1:size(database.label,1)]' precision_at_k'];
32+
save(precision_file, 'P', '-ascii');
33+
34+
pr = [[0:.1:1]' agg_prec'];
35+
save(pr_file, 'pr', '-ascii');
36+
end

0 commit comments

Comments
 (0)