beginning to add results

This commit is contained in:
aj 2019-12-01 02:30:20 +00:00
parent 752243d192
commit a658913d21
24 changed files with 2415 additions and 111 deletions

View File

@ -22,8 +22,8 @@ OUT_FOLDER = 'descriptors';
%% and within that folder, create another folder to hold these descriptors
%% the idea is all your descriptors are in individual folders - within
%% the folder specified as 'OUT_FOLDER'.
OUT_SUBFOLDER='avgRGB';
% OUT_SUBFOLDER='globalRGBhisto';
% OUT_SUBFOLDER='avgRGB';
OUT_SUBFOLDER='globalRGBhisto';
% OUT_SUBFOLDER='spatialColour';
% OUT_SUBFOLDER='spatialColourTexture';
@ -37,9 +37,9 @@ for filenum=1:length(allfiles)
fout=[OUT_FOLDER,'/',OUT_SUBFOLDER,'/',fname(1:end-4),'.mat'];%replace .bmp with .mat
%% EXTRACT FUNCTION
F=extractAvgRGB(img);
% F=extractGlobalColHist(img);
% F=extractSpatialColour(img);
% F=extractAvgRGB(img);
F=extractGlobalColHist(img);
% F=extractSpatialColour(img, 2, 1);
% F=extractSpatialColourTexture(img);
save(fout,'F');
toc

View File

@ -161,17 +161,7 @@ for run=1:run_total
%% 5) 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);
average_precision = sum(precision_values .* correct_at_n) / CAT_HIST(1,run);
AP_values(run) = average_precision;

View File

@ -28,9 +28,9 @@ DESCRIPTOR_FOLDER = 'descriptors';
%% and within that folder, another folder to hold the descriptors
%% we are interested in working with
% DESCRIPTOR_SUBFOLDER='avgRGB';
% DESCRIPTOR_SUBFOLDER='globalRGBhisto';
DESCRIPTOR_SUBFOLDER='globalRGBhisto';
% DESCRIPTOR_SUBFOLDER='spatialColour';
DESCRIPTOR_SUBFOLDER='spatialColourTexture';
% DESCRIPTOR_SUBFOLDER='spatialColourTexture';
CATEGORIES = ["Farm Animal"
"Tree"
@ -93,6 +93,9 @@ NIMG=size(ALLFEAT,1); % number of images in collection
confusion_matrix = zeros(CAT_TOTAL);
all_precision = [];
all_recall = [];
AP_values = zeros([1, CAT_TOTAL]);
for iteration=1:CAT_TOTAL
@ -101,7 +104,7 @@ for iteration=1:CAT_TOTAL
%% 3) Compute EigenModel
E = getEigenModel(ALLFEAT);
E = deflateEigen(E, 12);
E = deflateEigen(E, 3);
%% 4) Project data to lower dimensionality
ALLFEAT=ALLFEAT-repmat(E.org,size(ALLFEAT,1),1);
@ -122,10 +125,10 @@ for iteration=1:CAT_TOTAL
dst=sortrows(dst,1); % sort the results
%% 4) Calculate PR
precision_values=zeros([1, NIMG]);
recall_values=zeros([1, NIMG]);
precision_values=zeros([1, NIMG-1]);
recall_values=zeros([1, NIMG-1]);
correct_at_n=zeros([1, NIMG]);
correct_at_n=zeros([1, NIMG-1]);
query_row = dst(1,:);
query_category = query_row(1,3);
@ -134,9 +137,12 @@ for iteration=1:CAT_TOTAL
end
fprintf('category was %s\n', CATEGORIES(query_category))
dst = dst(2:NIMG, :);
%calculate PR for each n
for i=1:NIMG
for i=1:size(dst, 1)
% NIMG-1 and j iterator variable is in order to skip calculating for query image
rows = dst(1:i, :);
correct_results = 0;
@ -168,7 +174,7 @@ for iteration=1:CAT_TOTAL
end
precision = correct_results / i;
recall = correct_results / CAT_HIST(1,iteration);
recall = correct_results / (CAT_HIST(1,iteration) - 1);
precision_values(i) = precision;
recall_values(i) = recall;
@ -176,28 +182,23 @@ for iteration=1:CAT_TOTAL
%% 5) 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,iteration)
average_precision = sum(precision_values .* correct_at_n) / CAT_HIST(1,iteration);
AP_values(iteration) = average_precision;
all_precision = [all_precision ; precision_values];
all_recall = [all_recall ; recall_values];
%% 6) plot PR curve
figure(1)
plot(recall_values, precision_values);
plot(recall_values, precision_values,'LineWidth',1.5);
hold on;
title('PR Curve');
xlabel('Recall');
ylabel('Precision');
xlim([0 1]);
ylim([0 1]);
%% 7) Visualise the results
@ -222,6 +223,17 @@ for iteration=1:CAT_TOTAL
end
%% Plot average PR curve
figure(4)
mean_precision = mean(all_precision);
mean_recall = mean(all_recall);
plot(mean_recall, mean_precision,'LineWidth',5);
title('PR Curve');
xlabel('Average Recall');
ylabel('Average Precision');
xlim([0 1]);
ylim([0 1]);
% normalise confusion matrix
norm_confusion_matrix = confusion_matrix ./ sum(confusion_matrix, 'all');

View File

@ -163,17 +163,7 @@ for iteration=1:CAT_TOTAL
%% 5) 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,iteration)
average_precision = sum(precision_values .* correct_at_n) / CAT_HIST(1,iteration);
AP_values(iteration) = average_precision;

View File

@ -28,9 +28,9 @@ DESCRIPTOR_FOLDER = 'descriptors';
%% and within that folder, another folder to hold the descriptors
%% we are interested in working with
% DESCRIPTOR_SUBFOLDER='avgRGB';
% DESCRIPTOR_SUBFOLDER='globalRGBhisto';
DESCRIPTOR_SUBFOLDER='globalRGBhisto';
% DESCRIPTOR_SUBFOLDER='spatialColour';
DESCRIPTOR_SUBFOLDER='spatialColourTexture';
% DESCRIPTOR_SUBFOLDER='spatialColourTexture';
CATEGORIES = ["Farm Animal"
"Tree"
@ -93,6 +93,9 @@ NIMG=size(ALLFEAT,1); % number of images in collection
confusion_matrix = zeros(CAT_TOTAL);
all_precision = [];
all_recall = [];
AP_values = zeros([1, CAT_TOTAL]);
for iteration=1:CAT_TOTAL
@ -114,10 +117,10 @@ for iteration=1:CAT_TOTAL
dst=sortrows(dst,1); % sort the results
%% 4) Calculate PR
precision_values=zeros([1, NIMG]);
recall_values=zeros([1, NIMG]);
precision_values=zeros([1, NIMG-1]);
recall_values=zeros([1, NIMG-1]);
correct_at_n=zeros([1, NIMG]);
correct_at_n=zeros([1, NIMG-1]);
query_row = dst(1,:);
query_category = query_row(1,3);
@ -126,16 +129,18 @@ for iteration=1:CAT_TOTAL
end
fprintf('category was %s\n', CATEGORIES(query_category))
dst = dst(2:NIMG, :);
%calculate PR for each n
for i=1:NIMG
for i=1:size(dst, 1)
% NIMG-1 and j iterator variable is in order to skip calculating for query image
rows = dst(1:i, :);
correct_results = 0;
incorrect_results = 0;
if i > 1
if i > 1
for n=1:i - 1
row = rows(n, :);
category = row(3);
@ -161,7 +166,7 @@ for iteration=1:CAT_TOTAL
end
precision = correct_results / i;
recall = correct_results / CAT_HIST(1,iteration);
recall = correct_results / (CAT_HIST(1,iteration) - 1);
precision_values(i) = precision;
recall_values(i) = recall;
@ -169,28 +174,23 @@ for iteration=1:CAT_TOTAL
%% 5) 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,iteration);
average_precision = sum(precision_values .* correct_at_n) / CAT_HIST(1,iteration);
AP_values(iteration) = average_precision;
all_precision = [all_precision ; precision_values];
all_recall = [all_recall ; recall_values];
%% 6) plot PR curve
figure(1)
plot(recall_values, precision_values);
hold on;
title('PR Curve');
xlabel('Recall');
ylabel('Precision');
% figure(1)
% plot(recall_values, precision_values,'LineWidth',1.5);
% hold on;
% title('Global Colour Histogram PR (n=20)');
% xlabel('Recall');
% ylabel('Precision');
% xlim([0 1]);
% ylim([0 1]);
%% 7) Visualise the results
@ -215,6 +215,17 @@ for iteration=1:CAT_TOTAL
end
%% Plot average PR curve
figure(4)
mean_precision = mean(all_precision);
mean_recall = mean(all_recall);
plot(mean_recall, mean_precision,'LineWidth',5);
title('Global Colour Histogram Average PR (n=5)');
xlabel('Average Recall');
ylabel('Average Precision');
xlim([0 1]);
ylim([0 1]);
% normalise confusion matrix
norm_confusion_matrix = confusion_matrix ./ sum(confusion_matrix, 'all');

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 109 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

BIN
data/data.ods Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 189 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 KiB

View File

@ -1,6 +1,6 @@
function F=extractGlobalColHist(img)
divs = 8;
divs = 5;
qimg = floor(img .* divs);
bin = qimg(:,:,1) * divs^2 + qimg(:,:,2) * divs^1 + qimg(:,:,3);

View File

@ -1,7 +1,4 @@
function F=extractSpatialColour(img)
grid_rows = 10;
grid_columns = 10;
function F=extractSpatialColour(img, grid_rows, grid_columns)
img_size = size(img);
img_rows = img_size(1);

File diff suppressed because it is too large Load Diff

BIN
report/l2norm.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

BIN
report/pca-example.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -1 +1,3 @@
@misc{microsoft_msrc, title={Image Understanding }, url={https://www.microsoft.com/en-us/research/project/image-understanding/}, journal={Microsoft}, publisher={Microsoft}, year={2000}, month={Jan}}
@misc{setosa_pca, title={Principal Component Analysis}, url={http://setosa.io/ev/principal-component-analysis/}, journal={Explained Visually}, publisher={Setosa}, author={Powell, Victor and Lehe, Lewis}}

280
single_selected_image.m Normal file
View File

@ -0,0 +1,280 @@
%% EEE3032 - Computer Vision and Pattern Recognition (ee3.cvpr)
%%
%% cvpr_visualsearch.m
%% Skeleton code provided as part of the coursework assessment
%%
%% This code will load in all descriptors pre-computed (by the
%% function cvpr_computedescriptors) from the images in the MSRCv2 dataset.
%%
%% It will pick a descriptor at random and compare all other descriptors to
%% it - by calling cvpr_compare. In doing so it will rank the images by
%% similarity to the randomly picked descriptor. Note that initially the
%% function cvpr_compare returns a random number - you need to code it
%% so that it returns the Euclidean distance or some other distance metric
%% between the two descriptors it is passed.
%%
%% (c) John Collomosse 2010 (J.Collomosse@surrey.ac.uk)
%% Centre for Vision Speech and Signal Processing (CVSSP)
%% University of Surrey, United Kingdom
close all;
clear all;
%% Edit the following line to the folder you unzipped the MSRCv2 dataset to
DATASET_FOLDER = 'dataset';
%% Folder that holds the results...
DESCRIPTOR_FOLDER = 'descriptors';
%% and within that folder, another folder to hold the descriptors
%% we are interested in working with
% DESCRIPTOR_SUBFOLDER='avgRGB';
% DESCRIPTOR_SUBFOLDER='globalRGBhisto';
DESCRIPTOR_SUBFOLDER='spatialColour';
% DESCRIPTOR_SUBFOLDER='spatialColourTexture';
CATEGORIES = ["Farm Animal"
"Tree"
"Building"
"Plane"
"Cow"
"Face"
"Car"
"Bike"
"Sheep"
"Flower"
"Sign"
"Bird"
"Book Shelf"
"Bench"
"Cat"
"Dog"
"Road"
"Water Features"
"Human Figures"
"Coast"
];
QUERY_INDEXES=[301 358 384 436 447 476 509 537 572 5 61 80 97 127 179 181 217 266 276 333];
% 1_10 2_16 3_12 4_4 5_15 6_14 7_17 8_15 9_1 10_14 11_8 12_26 13_10 14_10
% 15_8 16_10 17_16 18_5 19_15 20_12
map = [];
for b=1:3
for c=1:3
%% 1) Load all the descriptors into "ALLFEAT"
%% each row of ALLFEAT is a descriptor (is an image)
ALLFEAT=[];
ALLFILES=cell(1,0);
ALLCATs=[];
ctr=1;
% allfiles=dir (fullfile([DATASET_FOLDER,'/Images/*.bmp']));
% for filenum=1:length(allfiles)
% fname=allfiles(filenum).name;
%
% %identify photo category for PR calculation
% split_string = split(fname, '_');
% ALLCATs(filenum) = str2double(split_string(1));
%
% imgfname_full=([DATASET_FOLDER,'/Images/',fname]);
% img=double(imread(imgfname_full))./255;
% thesefeat=[];
% featfile=[DESCRIPTOR_FOLDER,'/',DESCRIPTOR_SUBFOLDER,'/',fname(1:end-4),'.mat'];%replace .bmp with .mat
% load(featfile,'F');
% ALLFILES{ctr}=imgfname_full;
% ALLFEAT=[ALLFEAT ; F];
% ctr=ctr+1;
% end
allfiles=dir (fullfile([DATASET_FOLDER,'/Images/*.bmp']));
for filenum=1:length(allfiles)
fname=allfiles(filenum).name;
% fprintf('Processing file %d/%d - %s\n',filenum,length(allfiles),fname);
% tic;
imgfname_full=([DATASET_FOLDER,'/Images/',fname]);
img=double(imread(imgfname_full))./255;
%identify photo category for PR calculation
split_string = split(fname, '_');
ALLCATs(filenum) = str2double(split_string(1));
%% EXTRACT FUNCTION
% F=extractAvgRGB(img);
% F=extractGlobalColHist(img);
F = extractSpatialColour(img, b, c);
% F=extractSpatialColourTexture(img);
% toc
ALLFEAT=[ALLFEAT ; F];
end
% get counts for each category for PR calculation
CAT_HIST = histogram(ALLCATs).Values;
CAT_TOTAL = length(CAT_HIST);
NIMG=size(ALLFEAT,1); % number of images in collection
confusion_matrix = zeros(CAT_TOTAL);
all_precision = [];
all_recall = [];
AP_values = zeros([1, CAT_TOTAL]);
for iteration=1:CAT_TOTAL
%% 2) Pick an image at random to be the query
queryimg=QUERY_INDEXES(iteration); % index of a random image
%% 3) Compute the distance of image to the query
dst=[];
for i=1:NIMG
candidate=ALLFEAT(i,:);
query=ALLFEAT(queryimg,:);
category=ALLCATs(i);
%% COMPARE FUNCTION
thedst=compareEuclidean(query, candidate);
dst=[dst ; [thedst i category]];
end
dst=sortrows(dst,1); % sort the results
%% 4) Calculate PR
precision_values=zeros([1, NIMG-1]);
recall_values=zeros([1, NIMG-1]);
correct_at_n=zeros([1, NIMG-1]);
query_row = dst(1,:);
query_category = query_row(1,3);
if query_category ~= iteration
dst
end
fprintf('category was %s\n', CATEGORIES(query_category))
dst = dst(2:NIMG, :);
%calculate PR for each n
for i=1:size(dst, 1)
% NIMG-1 and j iterator variable is in order to skip calculating for query image
rows = dst(1:i, :);
correct_results = 0;
incorrect_results = 0;
if i > 1
for n=1:i - 1
row = rows(n, :);
category = row(3);
if category == iteration
correct_results = correct_results + 1;
else
incorrect_results = incorrect_results + 1;
end
end
end
% LAST ROW
row = rows(i, :);
category = row(3);
if category == iteration
correct_results = correct_results + 1;
correct_at_n(i) = 1;
else
incorrect_results = incorrect_results + 1;
end
precision = correct_results / i;
recall = correct_results / (CAT_HIST(1,iteration) - 1);
precision_values(i) = precision;
recall_values(i) = recall;
end
%% 5) calculate AP
average_precision = sum(precision_values .* correct_at_n) / CAT_HIST(1,iteration);
AP_values(iteration) = average_precision;
all_precision = [all_precision ; precision_values];
all_recall = [all_recall ; recall_values];
%% 6) plot PR curve
% figure(1)
% plot(recall_values, precision_values,'LineWidth',1.5);
% hold on;
% title('Global Colour Histogram PR (n=20)');
% xlabel('Recall');
% ylabel('Precision');
% xlim([0 1]);
% ylim([0 1]);
%% 7) Visualise the results
%% These may be a little hard to see using imgshow
%% If you have access, try using imshow(outdisplay) or imagesc(outdisplay)
SHOW=25; % Show top 25 results
dst=dst(1:SHOW,:);
outdisplay=[];
for i=1:size(dst,1)
% img=imread(ALLFILES{dst(i,2)});
% img=img(1:2:end,1:2:end,:); % make image a quarter size
% img=img(1:81,:,:); % crop image to uniform size vertically (some MSVC images are different heights)
% outdisplay=[outdisplay img];
%populate confusion matrix
confusion_matrix(query_category, dst(i,3)) = confusion_matrix(query_category, dst(i,3)) + 1;
end
% figure(3)
% imgshow(outdisplay);
% axis off;
end
%% Plot average PR curve
% figure(4)
% mean_precision = mean(all_precision);
% mean_recall = mean(all_recall);
% plot(mean_recall, mean_precision,'LineWidth',5);
% title('Global Colour Histogram Average PR (n=20)');
% xlabel('Average Recall');
% ylabel('Average Precision');
% xlim([0 1]);
% ylim([0 1]);
% normalise confusion matrix
norm_confusion_matrix = confusion_matrix ./ sum(confusion_matrix, 'all');
%% 8 Calculate MAP
% figure(4)
% histogram(AP_values);
% title('Average Precision Distribution');
% ylabel('Count');
% xlabel('Average Precision');
% xlim([0, 1]);
MAP = mean(AP_values)
AP_sd = std(AP_values)
% figure(2)
% plot(1:CAT_TOTAL, AP_values);
% title('Average Precision Per Run');
% xlabel('Run');
% ylabel('Average Precision');
fprintf('%i,%i %i', b, c, MAP);
map(b, c) = MAP;
end
end