123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- function [Performances, normCCost, iSet, iXMinDist] = PQNNSwithADC( kk, mm, nRecall, flagVec)
- %clear all
- close all
- clc
- addpath('../Functions');
- addpath('./Functions');
- addpath('./Functions/Yael Library');
- % kk=100; % Number of Centroids for each set
- % mm=16; % Number of Centroid Sets to take into account
- % kCent=kk^mm; % Total cardinality of search space
- % nRecall=1; % Number of Recalls
-
- imgSz=128; % Image Descriptor size
- subImgSz=imgSz/mm; % Size of sub images to analyze
- Nlearn=1e5; % Number of pictures learn set
- Ntrain=1e6; % Number of pictures train set
- Ntests=1e4; % Number of pictures test set
- PQCentCompon=flagVec(1); % flag to activate Centroids Computation
- Quantizationon=flagVec(2); % flag to activate Data Sub Quantization Indices
- ADCCompon=flagVec(3); % flag to activate ADC Computations
- dispPQCenton=flagVec(4); % flag to activate Centroids Displayt
- justGetResultson=flagVec(5);% flag to just get results from stored data
- if(~justGetResultson)
-
- %% Computing/Loading PQ SubCentroids
- ndots=10;
- str2print='';
- ll=10;
- while mm/ll>1
- ndots=ndots-1;
- ll=ll*10;
- end
- ll=10;
- while kk/ll>1
- ndots=ndots-1;
- ll=ll*10;
- end
- for ll=0:ndots
- str2print=strcat(str2print,'.');
- end
- if(PQCentCompon)
- %% Reading Binary Learning Data Set
- fprintf('Loading Learning Data Set ...................... ');
- TSetMat=fvecs_read('../Data/sift/sift_learn.fvecs');
- fprintf('Done.\n');
- %% Preparing Training Data for Computation
-
- TSubSetMat=zeros(subImgSz,Nlearn,mm);
-
- for jj=1:m
- TSubSetMat(:,:,jj)=;
- end
-
- clearvars TSetMat
- fprintf('Computing m=%1.0f, k=%1.0f PQ subcentroids ...%s',mm,kk,str2print);
- CSubSetMat=zeros(subImgSz,kk,mm);
- for jj=1:mm
- CSubSetMat(:,:,jj)=yael_kmeans(single(TSubSetMat(:,:,jj)),kk, 'niter', 100, 'verbose', 0);
- %[~, CCtemp]=kmeans(TSubSetMat(:,:,jj)',kk);
- %CSubSetMat(:,:,jj)=CCtemp';
- pp=double(jj/mm*100);
- if(pp<10)
- fprintf('\b\b%2.0f%%',pp);
- else
- fprintf('\b\b\b%2.0f%%',pp);
- end
- end
- fprintf('\b\b Done.\n');
- save(strcat('./Data/k',int2str(kk),'m',int2str(mm),'CSubSetMat.mat'),'CSubSetMat');
- else
- fprintf('Loading m=%1.0f, k=%1.0f PQ subcentroids ....%s',mm,kk,str2print);
- load(strcat('./Data/k',int2str(kk),'m',int2str(mm),'CSubSetMat.mat'));
- fprintf(' Done.\n');
- end
-
- clearvars TSubSetMat
-
- %% Displaying PQ SubCentroids
- if(dispPQCenton)
- fprintf('Displaying PQ subcentroids: ... \n',mm);
- mmax=min(3,mm);
-
- for jj=1:mmax
- figure('units','normalized','outerposition',[0 0 1 1])
- suptitle('Centroid Portions of Pictures')
- imshow255_texmex(0,imgSz,[255*ones(kk,(jj-1)*subImgSz) ...
- CSubSetMat(:,:,jj)' ...
- 255*ones(kk,(mm-jj)*subImgSz)]);
- end
- end
-
- %% Preparing Training Data for Computation
- fprintf('Loading Training Data Set ...................... ');
- XSetMat=fvecs_read('../Data/sift/sift_base.fvecs');
- fprintf('Done.\n');
-
-
- %% Computing/Loading PQ SubCentroids for Training Data
- if(Quantizationon)
- nSteps=100; % number of steps for computation, be sure it divides 1e6
- fprintf('Computing PQ subcentroids for Training Data .... ');
- iXQ=zeros(mm,Ntrain);
- for jj=1:mm
- for ll=1:Ntrain/nSteps
- [~,iXQ(jj,(ll-1)*nSteps+1:ll*nSteps)]=Quantization(...
- XSetMat((jj-1)*subImgSz+1:jj*subImgSz,(ll-1)*nSteps+1:ll*nSteps),CSubSetMat(:,:,jj));
- end
- pp=double(jj/mm*100);
- if(pp<10)
- fprintf('\b\b%2.0f%%',pp);
- else
- fprintf('\b\b\b%2.0f%%',pp);
- end
- end
- fprintf('\b\b\b\b\b\bDone.\n');
- save(strcat('./Data/ADC/ADCk',int2str(kk),'m',int2str(mm),'iXQ.mat'),'iXQ');
- else
- fprintf('Loading PQ subcentroids for Training Data ...... ');
- load(strcat('./Data/ADC/ADCk',int2str(kk),'m',int2str(mm),'iXQ.mat'));
- fprintf('Done.\n');
- end
- clearvars XSetMat
-
- %% Preparing Testing Data for Computation
- fprintf('Loading Testing Data Set ....................... ');
- YSetMat=fvecs_read('../Data/sift/sift_query.fvecs');
- fprintf('Done.\n');
-
- %% Computing/Loading NNs by applying ADC
- if(ADCCompon)
- fprintf('Solving NNS Applying ADC ....................... ');
-
- [iXMinDist]= AsymmetricDistanceComputation(CSubSetMat, iXQ, YSetMat, nRecall);
- save(strcat('./Data/ADC/ADCk',int2str(kk),'m',int2str(mm),'iXMinDist.mat'),'iXMinDist');
- fprintf('\b\b\bDone.\n');
- else
- fprintf('Loading NNS results with ADC ...................... ');
- load(strcat('./Data/ADC/ADCk',int2str(kk),'m',int2str(mm),'iXMinDist.mat'));
- fprintf('Done.\n');
- end
-
- clearvars iXQ YSetMat CSubSetMat
- else
- fprintf('Loading NNS results with ADC ...................... ');
- load(strcat('./Data/ADC/ADCk',int2str(kk),'m',int2str(mm),'iXMinDist.mat'));
- fprintf('Done.\n');
- end
-
- %% Loading results
- fprintf('Loading Results Data Set ............');
- iSet=ivecs_read('../Data/sift/sift_groundtruth.ivecs')+1;
- fprintf('Done.\n');
- iSet=iSet(1,:);
-
- %% Testing PQ Data Set
- fprintf('Testing PQ Test Data Set ... \n');
- reqRecall=zeros(Ntests,1);
-
- for ii = 1:Ntests
- iXeq=find(iXMinDist(ii,:)==iSet(ii));
- if numel(iXeq)==1
- reqRecall(ii)=iXeq;
- else
- reqRecall(ii)=nRecall+1;
- end
- end
-
- reqRecall=sort(reqRecall);
-
- ll=1;
- recAtR=[1 2 5 10 20 50 100 200 500 1000 2000 5000 10000];
- recAtR=recAtR(recAtR<=nRecall);
- % recAtR=nRecall;
- Performances=zeros(1,length(recAtR));
-
- for ii=recAtR
- if ii <= nRecall
- Performances(ll) = length (find (reqRecall <= ii & reqRecall <= nRecall)) / Ntests * 100;
- fprintf ('Recall@%3d = %.3f\n', ii, Performances(ll));
- ll=ll+1;
- end
- end
-
-
- %% Computational Cost Evaluation
- maxCCost=Ntrain*Ntests*imgSz;
- quantCCost=Ntests*kk*imgSz;
- PQCCostADC=Ntrain*Ntests*mm+quantCCost;
- normCCost=PQCCostADC/maxCCost;
- fprintf('Computational Cost: %2.2f %% w.r.t Exhaustive NNS\n',normCCost*100);
|