EuclideanNNS.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. clear all
  2. close all
  3. clc
  4. addpath('../Functions');
  5. imgSz=128; % Lateral Image pixel size
  6. %imgSzSq=imgSz^2; % Square Image pixel size
  7. Ntr=1e6; % Number of pictures train set
  8. Ntests=1e4; % Number of pictures test set
  9. %fileOffsetPic=16; % Bytes of pictures file offset
  10. %fileOffsetLab=8; % Bytes of label file offset
  11. ENNSon=1; %
  12. fprintf('Loading data... \n');
  13. XSet=fvecs_read('../Data/sift/sift_base.fvecs');
  14. YSet=fvecs_read('../Data/sift/sift_query.fvecs');
  15. %YLab=ivecs_read('../Data/sift/sift_groundtruth.ivecs');
  16. break;
  17. %% Reading Binary Training Images and Labels
  18. %
  19. % [XSet, XLab]=ReadingPicSetLabSet(imgSz, Ntr, fileOffsetPic, '../Data/train-images.idx3-ubyte',...
  20. % fileOffsetLab, '../Data/train-labels.idx1-ubyte', 0);
  21. %
  22. %% Reading Binary Testing Images and Labels
  23. % [YSet, YLab]=ReadingPicSetLabSet(imgSz, Ntests, fileOffsetPic, '../Data/t10k-images.idx3-ubyte',...
  24. % fileOffsetLab, '../Data/t10k-labels.idx1-ubyte', 0);
  25. %% Computing NNS with Euclidean Distance
  26. if(ENNSon)
  27. fprintf('Computing Euclidean NNS: ... ');
  28. dEuc=zeros(1,Ntr);
  29. indmin=zeros(1,Ntests);
  30. for ii=1:Ntests
  31. for jj=1:Ntr
  32. Xjj=XSet(imgSz*(jj-1)+1:imgSz*jj);
  33. Yii=YSet(imgSz*(ii-1)+1:imgSz*ii);
  34. dEuc(jj)=sum((Xjj-Yii).^2);
  35. end
  36. [~,indmin(ii)]=min(dEuc);
  37. fprintf('\b\b\b\b\b\b%3.2f %%',ii/Ntests*100);
  38. end
  39. save('indminEuNNS.mat','indmin');
  40. else
  41. fprintf('Loading Euclidean NNS results ...');
  42. load('indminEuNNS.mat')
  43. end
  44. %% Plot some of the matches
  45. %Nstart=9983;
  46. %for kk=Nstart+1:Nstart+10%Ntests
  47. %subplot(2,10,kk-Nstart)
  48. %imshow255(0,imgSz,YSet')
  49. %xlabel(int2str(YLab(kk)));
  50. %title('Test Image')
  51. %subplot(2,10,kk-Nstart+10)
  52. %figure()
  53. %imshow255(0,imgSz,XSet(indmin(1))')
  54. %xlabel(int2str(XLab(indmin(kk))));
  55. %title('Match Image')
  56. %end
  57. %NErrors=Ntests-sum(XLab(indmin)==YLab(1:length(indmin)));
  58. %Performances=100-NErrors/Ntests*100