PQNNSwithADC.m~ 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. function [Performances, normCCost, iSet, iXMinDist] = PQNNSwithADC( kk, mm, nRecall, flagVec)
  2. %clear all
  3. close all
  4. clc
  5. addpath('../Functions');
  6. addpath('./Functions');
  7. addpath('./Functions/Yael Library');
  8. % kk=100; % Number of Centroids for each set
  9. % mm=16; % Number of Centroid Sets to take into account
  10. % kCent=kk^mm; % Total cardinality of search space
  11. % nRecall=1; % Number of Recalls
  12. imgSz=128; % Image Descriptor size
  13. subImgSz=imgSz/mm; % Size of sub images to analyze
  14. Nlearn=1e5; % Number of pictures learn set
  15. Ntrain=1e6; % Number of pictures train set
  16. Ntests=1e4; % Number of pictures test set
  17. PQCentCompon=flagVec(1); % flag to activate Centroids Computation
  18. Quantizationon=flagVec(2); % flag to activate Data Sub Quantization Indices
  19. ADCCompon=flagVec(3); % flag to activate ADC Computations
  20. dispPQCenton=flagVec(4); % flag to activate Centroids Displayt
  21. justGetResultson=flagVec(5);% flag to just get results from stored data
  22. if(~justGetResultson)
  23. %% Computing/Loading PQ SubCentroids
  24. ndots=10;
  25. str2print='';
  26. ll=10;
  27. while mm/ll>1
  28. ndots=ndots-1;
  29. ll=ll*10;
  30. end
  31. ll=10;
  32. while kk/ll>1
  33. ndots=ndots-1;
  34. ll=ll*10;
  35. end
  36. for ll=0:ndots
  37. str2print=strcat(str2print,'.');
  38. end
  39. if(PQCentCompon)
  40. %% Reading Binary Learning Data Set
  41. fprintf('Loading Learning Data Set ...................... ');
  42. TSetMat=fvecs_read('../Data/sift/sift_learn.fvecs');
  43. fprintf('Done.\n');
  44. %% Preparing Training Data for Computation
  45. TSubSetMat=zeros(subImgSz,Nlearn,mm);
  46. for jj=1:m
  47. TSubSetMat(:,:,jj)=;
  48. end
  49. clearvars TSetMat
  50. fprintf('Computing m=%1.0f, k=%1.0f PQ subcentroids ...%s',mm,kk,str2print);
  51. CSubSetMat=zeros(subImgSz,kk,mm);
  52. for jj=1:mm
  53. CSubSetMat(:,:,jj)=yael_kmeans(single(TSubSetMat(:,:,jj)),kk, 'niter', 100, 'verbose', 0);
  54. %[~, CCtemp]=kmeans(TSubSetMat(:,:,jj)',kk);
  55. %CSubSetMat(:,:,jj)=CCtemp';
  56. pp=double(jj/mm*100);
  57. if(pp<10)
  58. fprintf('\b\b%2.0f%%',pp);
  59. else
  60. fprintf('\b\b\b%2.0f%%',pp);
  61. end
  62. end
  63. fprintf('\b\b Done.\n');
  64. save(strcat('./Data/k',int2str(kk),'m',int2str(mm),'CSubSetMat.mat'),'CSubSetMat');
  65. else
  66. fprintf('Loading m=%1.0f, k=%1.0f PQ subcentroids ....%s',mm,kk,str2print);
  67. load(strcat('./Data/k',int2str(kk),'m',int2str(mm),'CSubSetMat.mat'));
  68. fprintf(' Done.\n');
  69. end
  70. clearvars TSubSetMat
  71. %% Displaying PQ SubCentroids
  72. if(dispPQCenton)
  73. fprintf('Displaying PQ subcentroids: ... \n',mm);
  74. mmax=min(3,mm);
  75. for jj=1:mmax
  76. figure('units','normalized','outerposition',[0 0 1 1])
  77. suptitle('Centroid Portions of Pictures')
  78. imshow255_texmex(0,imgSz,[255*ones(kk,(jj-1)*subImgSz) ...
  79. CSubSetMat(:,:,jj)' ...
  80. 255*ones(kk,(mm-jj)*subImgSz)]);
  81. end
  82. end
  83. %% Preparing Training Data for Computation
  84. fprintf('Loading Training Data Set ...................... ');
  85. XSetMat=fvecs_read('../Data/sift/sift_base.fvecs');
  86. fprintf('Done.\n');
  87. %% Computing/Loading PQ SubCentroids for Training Data
  88. if(Quantizationon)
  89. nSteps=100; % number of steps for computation, be sure it divides 1e6
  90. fprintf('Computing PQ subcentroids for Training Data .... ');
  91. iXQ=zeros(mm,Ntrain);
  92. for jj=1:mm
  93. for ll=1:Ntrain/nSteps
  94. [~,iXQ(jj,(ll-1)*nSteps+1:ll*nSteps)]=Quantization(...
  95. XSetMat((jj-1)*subImgSz+1:jj*subImgSz,(ll-1)*nSteps+1:ll*nSteps),CSubSetMat(:,:,jj));
  96. end
  97. pp=double(jj/mm*100);
  98. if(pp<10)
  99. fprintf('\b\b%2.0f%%',pp);
  100. else
  101. fprintf('\b\b\b%2.0f%%',pp);
  102. end
  103. end
  104. fprintf('\b\b\b\b\b\bDone.\n');
  105. save(strcat('./Data/ADC/ADCk',int2str(kk),'m',int2str(mm),'iXQ.mat'),'iXQ');
  106. else
  107. fprintf('Loading PQ subcentroids for Training Data ...... ');
  108. load(strcat('./Data/ADC/ADCk',int2str(kk),'m',int2str(mm),'iXQ.mat'));
  109. fprintf('Done.\n');
  110. end
  111. clearvars XSetMat
  112. %% Preparing Testing Data for Computation
  113. fprintf('Loading Testing Data Set ....................... ');
  114. YSetMat=fvecs_read('../Data/sift/sift_query.fvecs');
  115. fprintf('Done.\n');
  116. %% Computing/Loading NNs by applying ADC
  117. if(ADCCompon)
  118. fprintf('Solving NNS Applying ADC ....................... ');
  119. [iXMinDist]= AsymmetricDistanceComputation(CSubSetMat, iXQ, YSetMat, nRecall);
  120. save(strcat('./Data/ADC/ADCk',int2str(kk),'m',int2str(mm),'iXMinDist.mat'),'iXMinDist');
  121. fprintf('\b\b\bDone.\n');
  122. else
  123. fprintf('Loading NNS results with ADC ...................... ');
  124. load(strcat('./Data/ADC/ADCk',int2str(kk),'m',int2str(mm),'iXMinDist.mat'));
  125. fprintf('Done.\n');
  126. end
  127. clearvars iXQ YSetMat CSubSetMat
  128. else
  129. fprintf('Loading NNS results with ADC ...................... ');
  130. load(strcat('./Data/ADC/ADCk',int2str(kk),'m',int2str(mm),'iXMinDist.mat'));
  131. fprintf('Done.\n');
  132. end
  133. %% Loading results
  134. fprintf('Loading Results Data Set ............');
  135. iSet=ivecs_read('../Data/sift/sift_groundtruth.ivecs')+1;
  136. fprintf('Done.\n');
  137. iSet=iSet(1,:);
  138. %% Testing PQ Data Set
  139. fprintf('Testing PQ Test Data Set ... \n');
  140. reqRecall=zeros(Ntests,1);
  141. for ii = 1:Ntests
  142. iXeq=find(iXMinDist(ii,:)==iSet(ii));
  143. if numel(iXeq)==1
  144. reqRecall(ii)=iXeq;
  145. else
  146. reqRecall(ii)=nRecall+1;
  147. end
  148. end
  149. reqRecall=sort(reqRecall);
  150. ll=1;
  151. recAtR=[1 2 5 10 20 50 100 200 500 1000 2000 5000 10000];
  152. recAtR=recAtR(recAtR<=nRecall);
  153. % recAtR=nRecall;
  154. Performances=zeros(1,length(recAtR));
  155. for ii=recAtR
  156. if ii <= nRecall
  157. Performances(ll) = length (find (reqRecall <= ii & reqRecall <= nRecall)) / Ntests * 100;
  158. fprintf ('Recall@%3d = %.3f\n', ii, Performances(ll));
  159. ll=ll+1;
  160. end
  161. end
  162. %% Computational Cost Evaluation
  163. maxCCost=Ntrain*Ntests*imgSz;
  164. quantCCost=Ntests*kk*imgSz;
  165. PQCCostADC=Ntrain*Ntests*mm+quantCCost;
  166. normCCost=PQCCostADC/maxCCost;
  167. fprintf('Computational Cost: %2.2f %% w.r.t Exhaustive NNS\n',normCCost*100);