Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

void Impala::Core::Feature::InterestPointFeature::ProjectLLC ( CodebookVectors codebookVectors,
String  codebookMode,
Mat matrixDescriptors,
int  imageWidth,
int  imageHeight,
std::vector< int > &  X,
std::vector< int > &  Y,
std::vector< Real64 > &  regionCounts,
Util::IOBuffer dumpIndices = 0 
) [inline, private]

Definition at line 647 of file InterestPointFeature.h.

References Impala::Core::Array::Add(), Impala::Core::Array::C, Impala::Core::Table::TableTem< Col1T, Col2T, Col3T, Col4T, Col5T, Col6T, Col7T, Col8T, Col9T >::GetColumn2(), Impala::CmdOptions::GetInstance(), Impala::CmdOptions::GetInt(), ILOG_DEBUG, ILOG_INFO, Impala::Core::Matrix::MatE(), Impala::Core::Matrix::MatInverse(), Impala::Core::Matrix::MatMul(), Impala::Core::Matrix::MatNorm2DistTransposed(), Impala::Core::Matrix::MatNrCol(), Impala::Core::Matrix::MatNrRow(), Impala::Core::Matrix::MatTrace(), Impala::Core::Matrix::MatTranspose(), Impala::Max(), mCodebook, mDescriptorReduce, Impala::Min(), mPointSelector, Impala::Core::Array::MulVal(), Impala::Core::Array::SetVal(), Impala::Core::Table::Table::Size(), Impala::Timer::SplitTimeStr(), Impala::StringSplit(), and Impala::Core::Array::WriteRaw().

Referenced by ProjectOntoCodebook().

00651     {
00652         using namespace Matrix;
00653         Timer timerProjection;
00654         if(mDescriptorReduce)
00655         {
00656             matrixDescriptors = MatMul(matrixDescriptors, mDescriptorReduce);
00657         }
00658         Mat* matrixCodebook = mCodebook->GetColumn2()->GetStorage();
00659         ILOG_DEBUG(MatNrRow(matrixDescriptors) << " " <<
00660                    MatNrCol(matrixDescriptors));
00661         ILOG_DEBUG(MatNrRow(matrixCodebook) << " " <<
00662                    MatNrCol(matrixCodebook));
00663         ILOG_DEBUG("Codebook projection (init) took: " <<
00664                    timerProjection.SplitTimeStr());
00665 
00666         int knn = CmdOptions::GetInstance().GetInt("codebookSigma");
00667         Real64 beta = 1e-4;
00668         int step = Min(Int64(10000), Int64(20000000 / mCodebook->Size()));
00669         Mat* II = MatEye<Mat>(knn);
00670         MulVal(II, II, beta);
00671         Mat* onesVector = MatCreate<Mat>(knn, 1);
00672         SetVal(onesVector, 1.0);
00673         
00674         Mat* dumpedIndices = 0;
00675         if(dumpIndices)
00676         {
00677             dumpedIndices = MatCreate<Mat>(MatNrRow(matrixDescriptors), 2*knn+2);
00678         }
00679         String config = codebookMode;
00680         String absolute = StringSplit(config, '-');
00681         String aggregationMode = StringSplit(config, '-');
00682         String normMode = StringSplit(config, '-');
00683         ILOG_INFO("absolute=" << absolute << "; aggregationMode=" <<
00684                   aggregationMode << "; normMode=" << normMode);
00685         for(int kkk = 0; kkk < MatNrRow(matrixDescriptors); kkk += step)
00686         {
00687             int s = MatNrRow(matrixDescriptors) - kkk;
00688             if(step < s) s = step;
00689     
00690             Mat* partial = MatCreate<Mat>(s, MatNrCol(matrixDescriptors),
00691                                           MatE(matrixDescriptors, kkk, 0), true);
00692 
00693             // result will be a (#partial-descriptors, codebook size) matrix
00694             Mat* distances = MatNorm2DistTransposed(partial,
00695                                                     matrixCodebook);
00696 
00697             ILOG_DEBUG("Euclidean distance matrix took: " <<
00698                        timerProjection.SplitTimeStr());
00699                        
00700 
00701             std::vector<std::pair<Real64, int> > distancesAndIndices;
00702             // find k closest codebook elements
00703             for(int vj = 0; vj < s; vj++)
00704             {
00705                 distancesAndIndices.clear();
00706                 distancesAndIndices.reserve(mCodebook->Size());
00707                 for(int i = 0; i < mCodebook->Size(); i++)
00708                 {
00709                     distancesAndIndices.push_back(
00710                                 std::make_pair(*MatE(distances, vj, i), i));
00711                 }
00712                 std::nth_element(distancesAndIndices.begin(), 
00713                                  distancesAndIndices.begin() + knn, 
00714                                  distancesAndIndices.end());
00715                 // the first k elements are now the k smallest
00716                 
00717                 // idx = indices[:, descriptorIdx]
00718                 // z = codebook[idx, :] - 
00719                 //          numpy.tile(descriptors[descriptorIdx, :], (knn, 1))
00720                 Mat* z = MatCreate<Mat>(knn, MatNrCol(matrixDescriptors));
00721                 for(int kk = 0; kk < knn; kk++)
00722                 {
00723                     int closestCodebookIndex = distancesAndIndices[kk].second;
00724                     for(int i = 0; i < MatNrCol(matrixDescriptors); i++)
00725                     {
00726                         *MatE(z, kk, i) = *MatE(matrixCodebook, 
00727                                                 closestCodebookIndex, i) - 
00728                                           *MatE(partial, vj, i);
00729                     }
00730                 }
00731                 
00732                 // C = numpy.dot(z, z.transpose());
00733                 Mat* zT = MatTranspose(z);
00734                 Mat* C = MatMul(z, zT);
00735                 delete z; delete zT;
00736                 
00737                 // C = C + II * beta * numpy.trace(C);
00738                 Mat* tmp = 0;
00739                 MulVal(tmp, II, MatTrace(C));
00740                 Add(C, C, tmp);
00741                 delete tmp;
00742                 
00743                 Mat* invertedC = MatInverse(C);
00744                 Mat* w = MatMul(invertedC, onesVector);
00745                 delete invertedC;
00746                 delete C;
00747                 
00748                 // should we take the absolute value of w ??? TODO
00749                 
00750                 Real64 total = 0.0;
00751                 if(absolute == "llca")
00752                 {
00753                     for(int kk = 0; kk < knn; kk++)
00754                         total += fabs(*MatE(w, kk, 0));
00755                     for(int kk = 0; kk < knn; kk++)
00756                     {
00757                         *MatE(w, kk, 0) = fabs(*MatE(w, kk, 0)) / total;
00758                     }
00759                 }
00760                 else
00761                 {
00762                     for(int kk = 0; kk < knn; kk++)
00763                         total += *MatE(w, kk, 0);
00764                     for(int kk = 0; kk < knn; kk++)
00765                     {
00766                         *MatE(w, kk, 0) = *MatE(w, kk, 0) / total;
00767                     }
00768                 }
00769                 
00770                 if(aggregationMode == "avg")
00771                 {
00772                     for(int ps = 0; ps < mPointSelector.size(); ps++)
00773                     {
00774                         if(mPointSelector[ps]->Accept(X[kkk+vj], Y[kkk+vj],
00775                                                       imageWidth, imageHeight))
00776                         {
00777                             for(int kk = 0; kk < knn; kk++)
00778                             {
00779                                 int closestCodebookIndex = 
00780                                                 distancesAndIndices[kk].second;
00781                                 codebookVectors[ps]->Elem(closestCodebookIndex) =
00782                                     codebookVectors[ps]->Elem(closestCodebookIndex) + *MatE(w, kk, 0);
00783                             }
00784                             regionCounts[ps] = regionCounts[ps] + 1;
00785                         }
00786                     }
00787                 }
00788                 else if(aggregationMode == "max" || aggregationMode.empty())
00789                 {
00790                     for(int ps = 0; ps < mPointSelector.size(); ps++)
00791                     {
00792                         if(mPointSelector[ps]->Accept(X[kkk+vj], Y[kkk+vj],
00793                                                       imageWidth, imageHeight))
00794                         {
00795                             for(int kk = 0; kk < knn; kk++)
00796                             {
00797                                 int closestCodebookIndex = 
00798                                                 distancesAndIndices[kk].second;
00799                                 codebookVectors[ps]->Elem(closestCodebookIndex) =
00800                                     Max(codebookVectors[ps]->Elem(closestCodebookIndex), *MatE(w, kk, 0));
00801                             }
00802                             regionCounts[ps] = 1;
00803                         }
00804                     }
00805                 }
00806                 
00807                 // dump
00808                 if(dumpIndices)
00809                 {
00810                     *MatE(dumpedIndices, kkk+vj, 0) = X[kkk+vj];
00811                     *MatE(dumpedIndices, kkk+vj, 1) = Y[kkk+vj];
00812                     for(int kk = 0; kk < knn; kk++)
00813                     {
00814                         int closestCodebookIndex = 
00815                                         distancesAndIndices[kk].second;
00816                         *MatE(dumpedIndices, kkk+vj, kk*2 + 2) = closestCodebookIndex;
00817                         *MatE(dumpedIndices, kkk+vj, kk*2 + 3) = *MatE(w, kk, 0);
00818                     }
00819                 }
00820                 delete w;
00821             }
00822             delete partial;
00823             delete distances;
00824             ILOG_DEBUG("Codebook projection (piece) took: " <<
00825                        timerProjection.SplitTimeStr() << " " << mPointSelector.size());
00826         }
00827         delete II;
00828         delete onesVector;
00829         ILOG_INFO("Codebook projection (LLC/" << MatNrRow(matrixDescriptors) <<
00830                   ") took: " << timerProjection.SplitTimeStr());
00831         if(mDescriptorReduce)
00832         {
00833             delete matrixDescriptors;
00834         }
00835         if(normMode.empty())
00836         {
00837             // for backward compatibility
00838             for(int ps = 0; ps < mPointSelector.size(); ps++)
00839             {
00840                 regionCounts[ps] = 1;
00841             }
00842         }
00843         else if(normMode == "L2")
00844         {
00845             for(int ps = 0; ps < mPointSelector.size(); ps++)
00846             {
00847                 Real64 tmp = 0.0;
00848                 for(int i = 0; i < mCodebook->Size(); i++)
00849                 {
00850                     tmp += codebookVectors[ps]->Elem(i) * codebookVectors[ps]->Elem(i);
00851                 }
00852                 regionCounts[ps] = sqrt(tmp);
00853             }
00854         }
00855         else if(normMode == "L2a")
00856         {
00857             Real64 tmp = 0.0;
00858             for(int ps = 0; ps < mPointSelector.size(); ps++)
00859             {
00860                 for(int i = 0; i < mCodebook->Size(); i++)
00861                 {
00862                     tmp += codebookVectors[ps]->Elem(i) * codebookVectors[ps]->Elem(i);
00863                 }
00864             }
00865             for(int ps = 0; ps < mPointSelector.size(); ps++)
00866                 regionCounts[ps] = sqrt(tmp);
00867         }
00868         else if(normMode == "L1")
00869         {
00870             for(int ps = 0; ps < mPointSelector.size(); ps++)
00871             {
00872                 Real64 tmp = 0.0;
00873                 for(int i = 0; i < mCodebook->Size(); i++)
00874                 {
00875                     tmp += codebookVectors[ps]->Elem(i);
00876                 }
00877                 regionCounts[ps] = tmp;
00878             }
00879         }
00880         else if(normMode == "L1a")
00881         {
00882             Real64 total = 0.0;
00883             for(int ps = 0; ps < mPointSelector.size(); ps++)
00884             {
00885                 Real64 tmp = 0.0;
00886                 for(int i = 0; i < mCodebook->Size(); i++)
00887                 {
00888                     tmp += codebookVectors[ps]->Elem(i);
00889                 }
00890                 regionCounts[ps] = tmp;
00891                 total += tmp;
00892             }
00893             for(int ps = 0; ps < mPointSelector.size(); ps++)
00894             {
00895                 regionCounts[ps] = total;
00896             }
00897         }
00898 
00899         if(dumpIndices)
00900         {
00901             WriteRaw(dumpedIndices, dumpIndices, 3);
00902             delete dumpedIndices;
00903         }
00904     }

Here is the call graph for this function:


Generated on Thu Jan 13 09:19:20 2011 for ImpalaSrc by  doxygen 1.5.1