added AP, MAP

This commit is contained in:
aj 2019-11-08 11:51:33 +00:00
parent cdb3b64ef5
commit e5302730b7
1187 changed files with 166 additions and 87 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
dataset dataset
descriptors

View File

@ -22,6 +22,7 @@ OUT_FOLDER = 'descriptors';
%% and within that folder, create another folder to hold these descriptors %% and within that folder, create another folder to hold these descriptors
%% the idea is all your descriptors are in individual folders - within %% the idea is all your descriptors are in individual folders - within
%% the folder specified as 'OUT_FOLDER'. %% the folder specified as 'OUT_FOLDER'.
% OUT_SUBFOLDER='avgRGB';
OUT_SUBFOLDER='globalRGBhisto'; OUT_SUBFOLDER='globalRGBhisto';
allfiles=dir (fullfile([DATASET_FOLDER,'/Images/*.bmp'])); allfiles=dir (fullfile([DATASET_FOLDER,'/Images/*.bmp']));
@ -34,6 +35,7 @@ for filenum=1:length(allfiles)
fout=[OUT_FOLDER,'/',OUT_SUBFOLDER,'/',fname(1:end-4),'.mat'];%replace .bmp with .mat fout=[OUT_FOLDER,'/',OUT_SUBFOLDER,'/',fname(1:end-4),'.mat'];%replace .bmp with .mat
%% EXTRACT FUNCTION %% EXTRACT FUNCTION
% F=extractAvgRGB(img);
F=extractGlobalColHist(img); F=extractGlobalColHist(img);
save(fout,'F'); save(fout,'F');
toc toc

View File

@ -27,6 +27,7 @@ DATASET_FOLDER = 'dataset';
DESCRIPTOR_FOLDER = 'descriptors'; DESCRIPTOR_FOLDER = 'descriptors';
%% and within that folder, another folder to hold the descriptors %% and within that folder, another folder to hold the descriptors
%% we are interested in working with %% we are interested in working with
% DESCRIPTOR_SUBFOLDER='avgRGB';
DESCRIPTOR_SUBFOLDER='globalRGBhisto'; DESCRIPTOR_SUBFOLDER='globalRGBhisto';
@ -58,14 +59,17 @@ end
% get counts for each category for PR calculation % get counts for each category for PR calculation
CAT_HIST = histogram(ALLCATs).Values; CAT_HIST = histogram(ALLCATs).Values;
%% 2) Pick an image at random to be the query run_total = 20;
NIMG=size(ALLFEAT,1); % number of images in collection AP_values = zeros([1, run_total]);
queryimg=floor(rand()*NIMG); % index of a random image for run=1:run_total
%% 2) Pick an image at random to be the query
NIMG=size(ALLFEAT,1); % number of images in collection
queryimg=floor(rand()*NIMG); % index of a random image
%% 3) Compute the distance of image to the query %% 3) Compute the distance of image to the query
dst=[]; dst=[];
for i=1:NIMG for i=1:NIMG
candidate=ALLFEAT(i,:); candidate=ALLFEAT(i,:);
query=ALLFEAT(queryimg,:); query=ALLFEAT(queryimg,:);
@ -74,22 +78,26 @@ for i=1:NIMG
%% COMPARE FUNCTION %% COMPARE FUNCTION
thedst=compareEuclidean(query,candidate); thedst=compareEuclidean(query,candidate);
dst=[dst ; [thedst i category]]; dst=[dst ; [thedst i category]];
end end
dst=sortrows(dst,1); % sort the results dst=sortrows(dst,1); % sort the results
%% 3.5) Calculate PR %% 3.5) Calculate PR
precision_values=[]; precision_values=zeros([1, NIMG]);
recall_values=[]; recall_values=zeros([1, NIMG]);
query_row = dst(1,:);
query_category = query_row(1,3); correct_at_n=zeros([1, NIMG]);
for i=1:NIMG
query_row = dst(1,:);
query_category = query_row(1,3);
for i=1:NIMG
rows = dst(1:i, :); rows = dst(1:i, :);
correct_results = 0; correct_results = 0;
incorrect_results = 0; incorrect_results = 0;
for n=1:i if i > 1
for n=1:i - 1
row = rows(n, :); row = rows(n, :);
category = row(3); category = row(3);
@ -100,23 +108,56 @@ for i=1:NIMG
end end
end end
end
% LAST ROW
row = rows(i, :);
category = row(3);
if category == query_category
correct_results = correct_results + 1;
correct_at_n(i) = 1;
else
incorrect_results = incorrect_results + 1;
end
precision = correct_results / i; precision = correct_results / i;
recall = correct_results / CAT_HIST(1,query_category); recall = correct_results / CAT_HIST(1,query_category);
precision_values(i) = precision; precision_values(i) = precision;
recall_values(i) = recall; recall_values(i) = recall;
end
%% 3.6) calculate AP
P_rel_n = zeros([1, NIMG]);
for i = 1:NIMG
precision = precision_values(i);
i_result_relevant = correct_at_n(i);
P_rel_n(i) = precision * i_result_relevant;
end
sum_P_rel_n = sum(P_rel_n);
average_precision = sum_P_rel_n / CAT_HIST(1,query_category);
AP_values(run) = average_precision;
%% 3.8) plot PR curve
figure(1)
plot(recall_values, precision_values);
hold on;
title('PR Curve');
xlabel('Recall');
ylabel('Precision');
end end
% plot PR curve %% 3.9 Calculate MAP
plot(recall_values, precision_values); AP_values
title('PR Curve'); MAP = mean(AP_values)
xlabel('Recall');
ylabel('Precision');
% for i=1:NIMG
% [i, " -> p: ", precision_values(i), "r: ", recall_values(i)]
% end
%% 4) Visualise the results %% 4) Visualise the results
%% These may be a little hard to see using imgshow %% These may be a little hard to see using imgshow
@ -131,5 +172,6 @@ for i=1:size(dst,1)
img=img(1:81,:,:); % crop image to uniform size vertically (some MSVC images are different heights) img=img(1:81,:,:); % crop image to uniform size vertically (some MSVC images are different heights)
outdisplay=[outdisplay img]; outdisplay=[outdisplay img];
end end
% imgshow(outdisplay); figure(2)
% axis off; imgshow(outdisplay);
axis off;

View File

@ -78,8 +78,11 @@ end
dst=sortrows(dst,1); % sort the results dst=sortrows(dst,1); % sort the results
%% 3.5) Calculate PR %% 3.5) Calculate PR
precision_values=[]; precision_values=zeros([1, NIMG]);
recall_values=[]; recall_values=zeros([1, NIMG]);
correct_at_n=zeros([1, NIMG]);
query_row = dst(1,:); query_row = dst(1,:);
query_category = query_row(1,3); query_category = query_row(1,3);
for i=1:NIMG for i=1:NIMG
@ -89,8 +92,9 @@ for i=1:NIMG
correct_results = 0; correct_results = 0;
incorrect_results = 0; incorrect_results = 0;
for n=1:i if i > 1
row = rows(i, :); for n=1:i - 1
row = rows(n, :);
category = row(3); category = row(3);
if category == query_category if category == query_category
@ -100,6 +104,18 @@ for i=1:NIMG
end end
end end
end
% LAST ROW
row = rows(i, :);
category = row(3);
if category == query_category
correct_results = correct_results + 1;
correct_at_n(i) = 1;
else
incorrect_results = incorrect_results + 1;
end
precision = correct_results / i; precision = correct_results / i;
recall = correct_results / CAT_HIST(1,query_category); recall = correct_results / CAT_HIST(1,query_category);
@ -108,9 +124,20 @@ for i=1:NIMG
recall_values(i) = recall; recall_values(i) = recall;
end end
% for i=1:NIMG
% [i, " -> p: ", precision_values(i), "r: ", recall_values(i)] %% 3.6) calculate AP
% end for i = 1:NIMG
precision = precision_values(i);
i
end
%% 3.8) plot PR curve
% plot(recall_values, precision_values);
% title('PR Curve');
% xlabel('Recall');
% ylabel('Precision');
%% 4) Visualise the results %% 4) Visualise the results
%% These may be a little hard to see using imgshow %% These may be a little hard to see using imgshow

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More