Euclidian distance showing different result for different formula

조회 수: 2 (최근 30일)
new_user
new_user 2021년 12월 29일
댓글: Meg Noah 2021년 12월 29일
d = (query_feature' - train_feature').^2; % Eucledian distance
d_1 = sqrt(sum((query_feature' - train_feature') .^ 2))
The method with d is giving some error in retrieval, but when using d_1 it always give 100% accurate retrival (all retrieved images are similar as query image)
What can be wrong? Any suggestions appreciated.
  댓글 수: 2
Image Analyst
Image Analyst 2021년 12월 29일
편집: Image Analyst 2021년 12월 29일
Not sure what is wrong. Can you attach your data?
d is a list of the squared differences, while d_1 is the root mean square - a single number and a different thing. Not sure what you're expecting. The only way d or d_1 would be zero (meaning no differences and 100% accuracy) would be if query_feature exaclty equaled train_feature. Is that the case?
new_user
new_user 2021년 12월 29일
편집: new_user 2021년 12월 29일
db = 'CBIR';
[fn, pn] = uigetfile(db);
im = fullfile(pn, fn);
outputFlder = fullfile('Test'); %returns full path of last arugument
rootFolder = fullfile(outputFlder); %variable storing path
images_query = imageDatastore(rootFolder, 'IncludeSubfolders',true, 'LabelSource','foldernames'); %%'ReadFcn', @readCBIR
%R = imread("99 (5).jpeg"); % Read image
R = imread(im); % Read image
Input_Layer_Size_q = net.Layers(1).InputSize(1:2); % (1:2 = 1st 2 elemnts of input size), input layer size stored in this variable (Input_layer_size)
Resized_Test_image_q = augmentedImageDatastore(Input_Layer_Size_q, R, 'ColorPreprocessing','gray2rgb'); %% For defining test image replace "Testing _image with test folder
%Extract feature
train_feature = activations(net, Resized_Training_image, 'Animal Feature Learner', 'OutputAs', 'Rows');
query_feature = activations(net, Resized_Test_image_q, 'Animal Feature Learner', 'OutputAs', 'Rows');
%Equation 2
a = query_feature; % transposing
b = transpose(1-a);
%Equation 3
c = zeros(Number_of_Classes,Number_of_Training_images);
d = (query_feature' - train_feature').^2; % Eucledian distance
% d = sqrt(sum((query_feature' - train_feature') .^ 2)); % other method eucledian: giving all images from same category maybe something is wrong
for e = 1 : Number_of_Training_images
f = b.*d(:,e);
c(:, e) = f;
end
c = sqrt(sum(c))';
% Fetch top 25 similar images
g = sort(c);
[~, n] = sort(c);
n = n(1:50);
files = cell(1, 50);
for h =1:50
files{h} = Training_image.Files{n(h)};
end
%Display query image
figure;
imshow(R);
title('query')
% Display retrived images
figure;
montage(files);
title("retrived")

댓글을 달려면 로그인하십시오.

답변 (2개)

John D'Errico
John D'Errico 2021년 12월 29일
What you write in d is simply not Euclidean distance What can be wrong? Your belief that it is so? What you write in d1 IS a Euclidean distance computation, so that it works should be no surprise.
  댓글 수: 1
new_user
new_user 2021년 12월 29일
but giving all the time 100% retrival accuracy using pretrained CNN is ok?
I mean I am using pretrained CNN to extract features and then measuing the similarity between them using d & d_1. So, the result for d_1 are 100% always.

댓글을 달려면 로그인하십시오.


Meg Noah
Meg Noah 2021년 12월 29일
Some code - look at the sizes of the arrays to see why:
nsamples = 25;
ncomponents = 4;
s = RandStream('dsfmt19937','Seed',1123581321);
query_feature = rand(s,nsamples,ncomponents);
train_feature = rand(s,1,ncomponents);
% relative vector between query vectors and training vector
d = (query_feature' - train_feature').^2;
% Three ways to compute Eucledian distance between query vectors and a
% training vector
d_1 = sqrt(sum((query_feature' - train_feature') .^ 2))';
d_2 = sqrt(sum(bsxfun(@minus, query_feature, train_feature).^2,2));
d_3 = vecnorm((query_feature'-train_feature'),2)';
  댓글 수: 2
new_user
new_user 2021년 12월 29일
running d_2 is showing error. Matrix size mismatch
Meg Noah
Meg Noah 2021년 12월 29일
The code snippet above creates the distances as vectors - arrays that are nsamples in rows and with one column. What matrix deimension is your code expecting?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Analytics Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by