Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

WNNetsOverPQ_Fine_Coarse_function.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. function [LearningDistribution, NNeighPerf, ScoresCoarseCpxty, ADCfineCpxty, NormCpxty, NormPQCpxty]= ...
  2. WNNetsOverPQ_Fine_Coarse_function(bVSetMat,bWSetMat,bZSetMat,iMinDistExh,kkf,mmf,kkc,mmc,nn,L0factor)
  3. % addpath('./SIFTs Library');
  4. addpath('./PQ Library');
  5. addpath('./WNN Library');
  6. addpath('./PQ Library/Yael Library');
  7. set(0,'defaulttextinterpreter','latex');
  8. % ---- Quantities of interest for the input Data Set --------------
  9. nDim=128; % Dimensionality of the Data
  10. %NLearn=1e5; % Number of descriptors learn set
  11. Ntrain=1e6; % Number of descriptors train set
  12. Ntests=1e4; % Number of descriptors test set
  13. % ---- Quantities of interest for Fine Product Quantization -------
  14. %kkf=256; % Number of clusters kmeans
  15. %mmf=16; % Number of splits for PQ (divides 128)
  16. mDimf=nDim/mmf; % Dimensionality of splitted vectors
  17. nRecall=100;
  18. %recAtR=[1 2 5 10 20 50 100 200 500 1000 2000 5000 10000];
  19. %recAtR=recAtR(recAtR<=nRecall);
  20. recAtR=100;
  21. % ---- Quantities of interest for Coarse Product Quantization -----
  22. %kkc=20; % Number of clusters kmeans
  23. %mmc=4; % Number of splits for PQ (divides 128)
  24. mDimc=nDim/mmc; % Dimensionality of splitted vectors
  25. % ---- Quantities of interest for Willshaw Networks ---------------
  26. %nn=1e2; % Number of vectors per WNN
  27. LL=Ntrain/nn; % Number of different WNNs
  28. %Npeaks=round([1, qq./[10 5 4 3 2 4/3 1]]); % Number of peaks to select
  29. L0=round(LL*L0factor);
  30. resultsFileName=sprintf('./Run Result Sets/finePQ_m%d_k%d_coarseWNN_n%d_L0_%d_m%d_k%d.mat',mmf,kkf,nn,L0,mmc,kkc);
  31. if exist(resultsFileName,'file')==2
  32. %% LOADING RESULTS SECTION
  33. load(resultsFileName);
  34. else
  35. %% LOADING DATA SECTION
  36. % bVSetMat=double(fvecs_read('../Data/sift/sift_learn.fvecs'));
  37. % bWSetMat=double(fvecs_read('../Data/sift/sift_base.fvecs'));
  38. % bZSetMat=double(fvecs_read('../Data/sift/sift_query.fvecs'));
  39. % iMinDistExh=ivecs_read('../Data/sift/sift_groundtruth.ivecs')+1;
  40. %% COARSE PRODUCT QUANTIZATION SECTION
  41. coarseFileName=sprintf('./Coarse Quantization Indices and Dissimilarities/coarse_CWidx_sZSetMat_k%d_m%d.mat',kkc,mmc);
  42. if exist(coarseFileName,'file')==2
  43. load(coarseFileName);
  44. else
  45. str=sprintf('Coarse PQ: computing m=%1.0f, k=%1.0f subcentroids ',mmc,kkc);
  46. fprintf('%s%s ',str,('.')*ones(1,55-length(str)));
  47. CSubSetMatc=zeros(mDimc,kkc,mmc);
  48. CWidxc=zeros(Ntrain,mmc);
  49. sZSetMatc=zeros(kkc*mmc,Ntests);
  50. pperc=[];
  51. for jj=1:mmc
  52. [CSubSetMatc(:,:,jj), ~ ]=yael_kmeans(...
  53. single(bVSetMat((jj-1)*mDimc+1:jj*mDimc,:)),kkc, 'niter', 100, 'verbose', 0);
  54. ZDistMatc=EuclideanDistancesMat(bZSetMat((jj-1)*mDimc+1:jj*mDimc,:),CSubSetMatc(:,:,jj));
  55. [~,CWidxc(:,jj)]=Quantization(bWSetMat((jj-1)*mDimc+1:jj*mDimc,:),CSubSetMatc(:,:,jj));
  56. sZSetMatc((jj-1)*kkc+1:jj*kkc,:)=exp(-sqrt(ZDistMatc)');
  57. perc=sprintf('%2.2f%%',jj/mmc*100);
  58. fprintf(1,'%s',sprintf('\b')*ones(1,length(pperc)));
  59. fprintf(1,'%s',perc); pperc=perc;
  60. end
  61. clearvars CSubSetMatc ZDistMatc
  62. fprintf(1,'%s',sprintf('\b')*ones(1,length(pperc)));
  63. fprintf('Done.\n');
  64. save(coarseFileName,'CWidxc','sZSetMatc');
  65. end
  66. %% FINE PRODUCT QUANTIZATION SECTION
  67. fineFileName=sprintf('./Fine Quantization Indices and Distances/fine_CWidx_ZDistMat_k%d_m%d.mat',kkf,mmf);
  68. if exist(fineFileName,'file')==2
  69. load(fineFileName);
  70. else
  71. str=sprintf('Fine PQ: computing m=%1.0f, k=%1.0f subcentroids ',mmf,kkf);
  72. fprintf('%s%s ',str,('.')*ones(1,55-length(str)));
  73. CSubSetMatf=zeros(mDimf,kkf,mmf);
  74. CWidxf=zeros(Ntrain,mmf);
  75. ZDistMatf=zeros(Ntests,kkf,mmf);
  76. pperc=[];
  77. for jj=1:mmf
  78. [CSubSetMatf(:,:,jj), ~ ]=yael_kmeans(...
  79. single(bVSetMat((jj-1)*mDimf+1:jj*mDimf,:)),kkf, 'niter', 100, 'verbose', 0);
  80. ZDistMatf(:,:,jj)=EuclideanDistancesMat(bZSetMat((jj-1)*mDimf+1:jj*mDimf,:),CSubSetMatf(:,:,jj));
  81. [~,CWidxf(:,jj)]=Quantization(bWSetMat((jj-1)*mDimf+1:jj*mDimf,:),CSubSetMatf(:,:,jj));
  82. perc=sprintf('%2.2f%%',jj/mmf*100);
  83. fprintf(1,'%s',sprintf('\b')*ones(1,length(pperc)));
  84. fprintf(1,'%s',perc);pperc=perc;
  85. end
  86. clearvars CSubSetMatf bZSetMat
  87. fprintf(1,'%s',sprintf('\b')*ones(1,length(pperc)));
  88. fprintf('Done.\n');
  89. save(fineFileName,'CWidxf','ZDistMatf');
  90. end
  91. clearvars bWSetMat bVSetMat
  92. %% PREPROCESSING DATA TO STORE IN WNNets SECTION
  93. permRand=randperm(Ntrain,LL);
  94. str=sprintf('Preprocessing Data for Learning ');
  95. fprintf('%s%s ',str,('.')*ones(1,55-length(str)));
  96. learningNetsIdx=QuantHammingCanonicalIndices(CWidxc,permRand);
  97. LearningDistribution=hist(learningNetsIdx,1:LL);
  98. fprintf('Done.\n');
  99. %%
  100. eWDiffFeatsSplittedRpi(1:100)=struct('RunPermIndices',[]);
  101. eWDiffFeatsSplittedRci(1:100)=struct('RunCentroidsIndices',[]);
  102. multiCount=ones(1,LL);
  103. str=sprintf('Building Learning Data Sets ');
  104. fprintf('%s%s ',str,('.')*ones(1,55-length(str)));
  105. pperc=[];
  106. for ii=1:Ntrain
  107. eWDiffFeatsSplittedRpi(learningNetsIdx(ii)).RunPermIndices(multiCount(learningNetsIdx(ii)))=ii;
  108. eWDiffFeatsSplittedRci(learningNetsIdx(ii)).RunCentroidsIndices(:,multiCount(learningNetsIdx(ii)))=CWidxc(ii,:);
  109. multiCount(learningNetsIdx(ii))=multiCount(learningNetsIdx(ii))+1;
  110. perc=sprintf('%2.2f%%',ii/Ntrain*100);
  111. fprintf(1,'%s',sprintf('\b')*ones(1,length(pperc)));
  112. fprintf(1,'%s',perc); pperc=perc;
  113. end
  114. fprintf(1,'%s',sprintf('\b')*ones(1,length(pperc)));
  115. fprintf('Done.\n');
  116. clearvars eWSetMat permRand CWidxc %multiCount nStoredVec
  117. %% BUILDING WNNets AND SCORES COMPUTATION SECTION
  118. str=sprintf('Building %d Willshaw NNets and Computing Scores ',LL);
  119. fprintf('%s%s ',str,('.')*ones(1,55-length(str)));
  120. II=eye(kkc);
  121. P0=0;
  122. %Ntests=100;
  123. sX0=zeros(Ntests,LL);
  124. pperc=[];
  125. for jj=1:LL
  126. uniqCent=unique(eWDiffFeatsSplittedRci(jj).RunCentroidsIndices','rows');
  127. WXsmatrix=BuildNetworkfromIdxs(uniqCent',II);
  128. P0=P0+mean(mean(WXsmatrix));
  129. sX0(:,jj)=diag(sZSetMatc(:,1:Ntests)'*double(WXsmatrix)*sZSetMatc(:,1:Ntests));
  130. perc=sprintf('%2.2f%%',jj/LL*100);
  131. fprintf(1,'%s',sprintf('\b')*ones(1,length(pperc)));
  132. fprintf(1,'%s',perc);pperc=perc;
  133. end
  134. fprintf(1,'%s',sprintf('\b')*ones(1,length(pperc)));
  135. fprintf('Done.\n');
  136. avgP0=P0/LL;
  137. clearvars sZSetMatc eWDiffFeatsSplittedRsm WXsmatrix eWDiffFeatsSplittedRci uniqCent
  138. %% ADC COMPUTATION FOR PERFORMANCES COMPUTATION SECTION
  139. str=sprintf('Computing Performances: L=[%d:%d], recalls@[%d:%d] ',L0(1),L0(end),recAtR(1),recAtR(end));
  140. fprintf('%s%s ',str,('.')*ones(1,55-length(str)));
  141. NNeighPerf=zeros(length(recAtR),length(L0));
  142. %iXMinDistADC=zeros(1,nRecall);
  143. actIXMinDistADC=zeros(1,nRecall);
  144. [~,sortemp]=sort(sX0,2,'descend');
  145. NNmostLikely=sortemp(:,1:L0(end))';
  146. clearvars sortemp
  147. pperc=[];
  148. for jj=1:Ntests
  149. for tt=1:length(L0)
  150. if tt>1
  151. retIdxs=actIXMinDistADC;
  152. for ll=L0(tt-1)+1:L0(tt)
  153. retIdxs=[retIdxs, eWDiffFeatsSplittedRpi(NNmostLikely(ll,jj)).RunPermIndices];
  154. end
  155. else
  156. retIdxs=[];
  157. for ll=1:L0(tt)
  158. retIdxs=[retIdxs, eWDiffFeatsSplittedRpi(NNmostLikely(ll,jj)).RunPermIndices];
  159. end
  160. end
  161. iXMinDistADC=AsymmetricDistanceComputationWDist(CWidxf(retIdxs,:)', ZDistMatf(jj,:,:), nRecall);
  162. numRetIdxs=length(retIdxs);
  163. if numRetIdxs >= nRecall
  164. actIXMinDistADC=retIdxs(iXMinDistADC);
  165. else
  166. actIXMinDistADC(1:numRetIdxs)=retIdxs(iXMinDistADC(1:numRetIdxs));
  167. actIXMinDistADC(numRetIdxs+1:end)=ones(1,nRecall-numRetIdxs);
  168. end
  169. recRes=RecallTest(actIXMinDistADC,nRecall,iMinDistExh(:,jj))';
  170. NNeighPerf(:,tt) = NNeighPerf(:,tt)+ recRes(end);
  171. perc=sprintf('%2.2f%%',((jj-1)*length(L0)+tt)/(Ntests*length(L0))*100);
  172. fprintf(1,'%s',sprintf('\b')*ones(1,length(pperc)));
  173. fprintf(1,'%s',perc);pperc=perc;
  174. end
  175. end
  176. NNeighPerf=NNeighPerf/Ntests;
  177. fprintf(1,'%s',sprintf('\b')*ones(1,length(pperc)));
  178. fprintf('Done.\n');
  179. clearvars SortQueryNetIdx actIXMinDistADC RetrievedIdxs bZSetMat CWidxf NNMostLikely eWDiffFeatsSplittedRpi sX0
  180. %% COMPUTATIONAL COST EVALUATION SECTION
  181. Cpxty=kkf*nDim+kkc*nDim+avgP0*(mmc*kkc)^2*LL+L0*nn*mmf;
  182. ScoresCoarseCpxty=avgP0*(mmc*kkc)^2*LL;
  183. ADCfineCpxty=LL/10*nn*mmf;
  184. NormCpxty=Cpxty/(nDim*Ntrain);
  185. NormPQCpxty=(kkf*nDim+mmf*Ntrain)/(nDim*Ntrain);
  186. %NormNormCpxty=NormCpxty/NormPQCpxty;
  187. %% SAVING RESULTS SECTION
  188. save(resultsFileName,'NormCpxty','NormPQCpxty','NNeighPerf','Npeaks');
  189. end
  190. %% RESULTS VISUALIZATION SECTION
  191. displayPerf=0;
  192. if(displayPerf)
  193. figure();
  194. plot([NormCpxty(1:end-1) NormPQCpxty],NNeighPerf','x-'); hold on;
  195. for jj=1:length(L0)-1
  196. text(NormCpxty(jj),1.05,sprintf('L=%d',L0(jj)),'interpreter','latex');
  197. end
  198. plot(NormPQCpxty,NNeighPerf(:,end),'*');
  199. text(NormPQCpxty,1.05,'PQ','interpreter','latex');
  200. title(sprintf('Fine PQ $(k_f=%d, m_f=%d)$ + Coarse WNNets $(k_c=%d,m_c=%d,q=%d,n=%d)$',kkf,mmf,kkc,mmc,LL,nn));
  201. ylabel('Nearest Neighbour Search Performances','interpreter','latex');
  202. xlabel('Computational Cost (Normalized to Exhaustive Search)','interpreter','latex');
  203. h=legend('recall @1','recall @2','recall @5','recall @10','recall @20',...
  204. 'recall @50','recall @100','Location','southeast');
  205. grid on; grid minor;
  206. set(h,'interpreter','latex');
  207. ylim([0 1.1]);
  208. xlim([0 max(NormPQCpxty,NormCpxty(end))+.005])
  209. end