123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <core/vector/VectorT.h>
- #include <core/vector/MatrixT.h>
- #include <core/image/ImageT.h>
- #include <core/basics/Config.h>
- #include <core/vector/VVector.h>
- #include <vislearning/cbaselib/MultiDataset.h>
- #include <vislearning/features/localfeatures/LFonHSG.h>
- #include <vislearning/features/localfeatures/GenericLocalFeatureSelection.h>
- #include <vislearning/features/simplefeatures/FCCodebookHistBin.h>
- #include <vislearning/features/simplefeatures/CodebookPrototypes.h>
- #include <vislearning/math/cluster/KMeans.h>
- #include <set>
- #include <vislearning/baselib/Globals.h>
- using namespace std;
- using namespace NICE;
- using namespace OBJREC;
- int main(int argc, char **argv)
- {
- Config conf ( argc, argv );
- string destForFeat = conf.gS("main", "destForFeat", "/tmp/");
- double percentageOfPatchesForKMeans = conf.gD("main", "percentageOfPatchesForKMeans", 0.3);
- int nrOfClusters = conf.gI("main", "nrOfClusters", 200);
- bool verbose = conf.gB("main", "verbose", false);
- MultiDataset md ( &conf );
- const LabeledSet & train = *(md["train"]);
- const LabeledSet & test = *(md["test"]);
- const ClassNames & classNamesTrain = md.getClassNames("train");
- const ClassNames & classNamesTest = md.getClassNames("test");
- LFonHSG GridForSIFT(&conf);
- std::vector<VVector> featuresTrainset;
-
- LOOP_ALL_S ( train )
- {
- EACH_S(classno, imgfilename);
- if (verbose)
- std::cerr << "imgfilename: " << imgfilename << std::endl;
- Image img (imgfilename);
- NICE::VVector positions;
- NICE::VVector features;
- // Extract the descriptor-Values from a given grayscale-Image.
- GridForSIFT.extractFeatures ( img, features, positions );
- if (verbose)
- std::cerr << "features.size: " << features.size() << std::endl;
- featuresTrainset.push_back(features);
- }
- int trainsize(featuresTrainset.size());
- int nrOfFivePercent(round(0.05*trainsize));
- std::cerr << "Now sample randomly some patches (SIFT-features)" << std::endl;
- VVector x;
- x.clear();
- int count(0);
- //randomly sampling of patches -- take percentageOfPatchesForKMeans % of the patches from each image
- for (int i = 0; i < trainsize; i++, count++)
- {
- //small stat bar
- if ( (count % nrOfFivePercent) == 0)
- std::cerr << "sampling for image nr: " << count << " / " << trainsize << std::endl;
-
- std::set<int> indices;
- int currentSize(featuresTrainset[i].size());
- for (int k = 0; k < (int) round(percentageOfPatchesForKMeans*currentSize); k++)
- {
- int idx( round((float)rand() * (currentSize-1)/ (float)(RAND_MAX)) );
- while ( (indices.find(idx) != indices.end()) && (i > 0))
- {
- idx = ( round((float)rand() * (currentSize-1)/ (float)(RAND_MAX)) );
- }
- x.push_back(featuresTrainset[i][idx]);
- indices.insert(idx);
- }
- }
- std::cerr << "sampling done - final number of patches: " << x.size() << std::endl;
- std::cerr << "Apply k-Means on randomly sampled patches" << std::endl;
- VVector prototypes;
- std::vector<double> weights;
- std::vector<int> assignments;
- std::cerr << "nrOfClusters: " << nrOfClusters << std::endl;
- ClusterAlgorithm *clusteralg = new KMeans (nrOfClusters);
- clusteralg->cluster(x, prototypes, weights, assignments);
- delete clusteralg;
- std::cerr << "prototypes.size(): " << prototypes.size() << std::endl;
- CodebookPrototypes codebook ( prototypes );
- std::cerr << "Now apply our k-means codebook to every image for computing the resulting features" << std::endl;
- FCCodebookHistBin featureGenerator(&conf, &GridForSIFT, "sum", &codebook);
- std::cerr << "codebook.size(): " << codebook.size() << std::endl;
- int cntTrain(0);
- LOOP_ALL_S ( train )
- {
- //small stat bar
- if ( (cntTrain % nrOfFivePercent) == 0)
- std::cerr << "image nr: " << cntTrain << " / " << trainsize << std::endl;
-
- EACH_S(classno, imgfilename);
-
- std::string finalDest(classNamesTrain.text(classno) +imgfilename);
- size_t posOfEnding = finalDest.find_last_of(".");
- size_t lengthOfEnding = finalDest.size() - posOfEnding;
- finalDest = finalDest.erase(posOfEnding, lengthOfEnding)+".txt";
- size_t posOfclassSlash = finalDest.find_last_of("/");
- size_t posOfFolderSlash = finalDest.find_last_of("/", posOfclassSlash-1);
- finalDest = finalDest.erase(0, posOfFolderSlash+1);
- finalDest = destForFeat + finalDest;
- // std::cerr << "finalDest: " << finalDest<< std::endl;
-
- Image img (imgfilename);
- NICE::Vector vec;
- //compute the histogram
- featureGenerator.convert (img, vec );
-
- std::filebuf fb;
- fb.open (finalDest.c_str(),ios::out);
- ostream outstream(&fb);
- outstream << vec;
- fb.close();
-
- cntTrain++;
- }
- int testsize(0);
- LOOP_ALL_S ( test )
- {
- testsize++;
- }
- int nrOfFivePercentTest(round(0.05*testsize));
- int cntTest(0);
- LOOP_ALL_S ( test )
- {
- //small stat bar
- if ( (cntTest % nrOfFivePercentTest) == 0)
- std::cerr << "image nr: " << cntTest << " / " << testsize << std::endl;
-
- EACH_S(classno, imgfilename);
-
- std::string finalDest(classNamesTest.text(classno) +imgfilename);
- size_t posOfEnding = finalDest.find_last_of(".");
- size_t lengthOfEnding = finalDest.size() - posOfEnding;
- finalDest = finalDest.erase(posOfEnding, lengthOfEnding)+".txt";
- size_t posOfclassSlash = finalDest.find_last_of("/");
- size_t posOfFolderSlash = finalDest.find_last_of("/", posOfclassSlash-1);
- finalDest = finalDest.erase(0, posOfFolderSlash+1);
- finalDest = destForFeat + finalDest;
- // std::cerr << "finalDest: " << finalDest<< std::endl;
-
- Image img (imgfilename);
- NICE::Vector vec;
- //compute the histogram
- featureGenerator.convert (img, vec );
-
- std::filebuf fb;
- fb.open (finalDest.c_str(),ios::out);
- ostream outstream(&fb);
- outstream << vec;
- fb.close();
-
- cntTest++;
- }
- return 0;
- }
|