123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- #include "FeatureLearningPrototypes.h"
- #include <iostream>
- #include <core/image/FilterT.h>
- #include <core/image/CircleT.h>
- #include <core/image/Convert.h>
- #include <core/vector/VectorT.h>
- #include <vislearning/features/localfeatures/LFonHSG.h>
- #include <vislearning/features/localfeatures/LFColorSande.h>
- #include <vislearning/features/localfeatures/LFColorWeijer.h>
- #include <vislearning/features/localfeatures/LFReadCache.h>
- #include <vislearning/features/localfeatures/LFWriteCache.h>
- #include <vislearning/math/cluster/KMeans.h>
- #include <vislearning/math/cluster/GMM.h>
- using namespace std;
- using namespace NICE;
- using namespace OBJREC;
-
-
-
-
-
- void FeatureLearningPrototypes::setClusterAlgo( const std::string & _clusterAlgoString)
- {
-
- if (this->clusterAlgo != NULL)
- delete clusterAlgo;
-
- if (_clusterAlgoString.compare("kmeans") == 0)
- {
- this->clusterAlgo = new OBJREC::KMeans(this->initialNumberOfClusters);
- }
- else if (_clusterAlgoString.compare("GMM") == 0)
- {
- this->clusterAlgo = new OBJREC::GMM(this->conf, this->initialNumberOfClusters);
- }
- else
- {
- std::cerr << "Unknown cluster algorithm selected, use k-means instead" << std::endl;
- this->clusterAlgo = new OBJREC::KMeans(this->initialNumberOfClusters);
- }
- }
- void FeatureLearningPrototypes::extractFeaturesFromTrainingImages( const OBJREC::MultiDataset *_md, NICE::VVector & examplesTraining )
- {
- examplesTraining.clear();
-
- int numberOfTrainImage ( 0 );
-
- const LabeledSet *trainFiles = (*_md)["train"];
-
-
- LOOP_ALL_S( *trainFiles )
- {
- EACH_INFO( classno, info );
- std::string filename = info.img();
-
- NICE::ColorImage img( filename );
- if ( b_showTrainingImages )
- {
- showImage( img, "Input" );
- }
-
-
- NICE::VVector features;
- NICE::VVector cfeatures;
- NICE::VVector positions;
-
- Globals::setCurrentImgFN ( filename );
- if (featureExtractor == NULL)
- std::cerr << "feature Extractor is NULL" << std::endl;
- else
- featureExtractor->extractFeatures ( img, features, positions );
-
-
- for ( NICE::VVector::iterator i = features.begin();
- i != features.end();
- i++)
- {
-
- i->normalizeL1();
- examplesTraining.push_back(*i);
- }
-
-
- features.clear();
- positions.clear();
- numberOfTrainImage++;
- }
- }
- void FeatureLearningPrototypes::train ( const OBJREC::MultiDataset *_md )
- {
- bool loadSuccess = this->loadInitialCodebook();
-
- if ( !loadSuccess )
- {
-
-
-
-
-
-
- std::cerr << " EXTRACT FEATURES FROM TRAINING IMAGES" << std::endl;
-
- NICE::VVector examplesTraining;
- this->extractFeaturesFromTrainingImages( _md, examplesTraining );
-
-
-
-
-
-
-
-
- std::cerr << " CLUSTER FEATURES FROM TRAINING IMAGES" << std::endl;
-
- prototypes.clear();
- std::vector< double > weights;
- std::vector< int > assignment;
- clusterAlgo->cluster ( examplesTraining, prototypes, weights, assignment);
- weights.clear();
- assignment.clear();
- }
-
- this->writeInitialCodebook();
- }
- bool FeatureLearningPrototypes::loadInitialCodebook ( )
- {
- if ( b_loadInitialCodebook )
- {
- std::cerr << " INITIAL CODEBOOK ALREADY COMPUTED - RE-USE IT" << std::endl;
- std::cerr << " // WARNING - WE DO NOT VERIFY WHETHER THIS IS THE CORRECT CODEBOOK FOR THIS TRAINING SET!!!!" << std::endl;
-
- prototypes.clear();
-
- try
- {
- prototypes.read(cacheInitialCodebook);
- }
- catch (...)
- {
- std::cerr << "Error while loading initial codebook" << std::endl;
- return false;
- }
- return true;
- }
- else
- return false;
- }
- bool FeatureLearningPrototypes::writeInitialCodebook ( )
- {
- if ( b_saveInitialCodebook )
- {
- std::cerr << " SAVE INITIAL CODEBOOK " << std::endl;
-
- try
- {
- prototypes.write( cacheInitialCodebook );
- }
- catch (...)
- {
- std::cerr << "Error while saving initial codebook" << std::endl;
- return false;
- }
- return true;
- }
- else
- return false;
- }
-
-
-
-
-
- FeatureLearningPrototypes::FeatureLearningPrototypes ( const Config *_conf,
- const MultiDataset *_md, const std::string & _section )
- : FeatureLearningGeneric ( _conf, _section )
- {
-
-
- std::string opSiftImpl;
- opSiftImpl = conf->gS ( "Descriptor", "implementation", "VANDESANDE" );
-
- bool readfeat;
- readfeat = conf->gB ( "Descriptor", "read", true );
-
- bool writefeat;
- writefeat = conf->gB ( "Descriptor", "write", true );
-
-
-
- initialNumberOfClusters = conf->gI(section, "initialNumberOfClusters", 10);
-
-
- std::string clusterAlgoString = conf->gS(section, "clusterAlgo", "kmeans");
-
-
- std::string distFunctionString = conf->gS(section, "distFunction", "euclidian");
-
-
-
-
-
-
-
-
-
-
-
-
- std::cerr << " SET UP VARIABLES AND METHODS " << std::endl;
-
-
- LocalFeatureRepresentation *cSIFT = NULL;
- LocalFeatureRepresentation *writeFeats = NULL;
- LocalFeatureRepresentation *readFeats = NULL;
- this->featureExtractor = NULL;
- if ( opSiftImpl == "NICE" )
- {
- cSIFT = new OBJREC::LFonHSG ( conf, "HSGtrain" );
- }
- else if ( opSiftImpl == "VANDESANDE" )
- {
- cSIFT = new OBJREC::LFColorSande ( conf, "LFColorSandeTrain" );
- }
- else
- {
- fthrow ( Exception, "feattype: %s not yet supported" << opSiftImpl );
- }
- this->featureExtractor = cSIFT;
-
- if ( writefeat )
- {
-
- writeFeats = new LFWriteCache ( conf, cSIFT );
- this->featureExtractor = writeFeats;
- }
- if ( readfeat )
- {
-
- if ( writefeat )
- {
- readFeats = new LFReadCache ( conf, writeFeats, -1 );
- }
- else
- {
- readFeats = new LFReadCache ( conf, cSIFT, -1 );
- }
- this->featureExtractor = readFeats;
- }
-
- this->clusterAlgo = NULL;
- this->setClusterAlgo( clusterAlgoString );
-
- if (distFunctionString.compare("euclidian") == 0)
- {
- distFunction = new NICE::EuclidianDistance<double>();
- }
- else
- {
- std::cerr << "Unknown vector distance selected, use euclidian instead" << std::endl;
- distFunction = new NICE::EuclidianDistance<double>();
- }
-
-
- this->train( _md );
-
-
- if ( cSIFT != NULL )
- cSIFT = NULL;
- if ( writeFeats != NULL )
- writeFeats = NULL;
- if ( readFeats != NULL )
- readFeats = NULL ;
-
-
- this->newImageCounter = 0;
-
-
- this->maxValForVisualization = 0.005;
- }
- FeatureLearningPrototypes::~FeatureLearningPrototypes()
- {
-
- if ( clusterAlgo != NULL )
- delete clusterAlgo;
- if ( distFunction != NULL )
- delete distFunction;
- if ( featureExtractor != NULL )
- delete featureExtractor;
- }
- NICE::FloatImage FeatureLearningPrototypes::evaluateCurrentCodebook ( const std::string & _filename , const bool & beforeComputingNewFeatures )
- {
- NICE::ColorImage img( _filename );
- if ( b_showTrainingImages )
- {
- showImage( img, "Input" );
- }
-
- int xsize ( img.width() );
- int ysize ( img.height() );
-
-
- NICE::VVector features;
- NICE::VVector cfeatures;
- NICE::VVector positions;
-
- Globals::setCurrentImgFN ( _filename );
- featureExtractor->extractFeatures ( img, features, positions );
-
- FloatImage noveltyImage ( xsize, ysize );
- noveltyImage.set ( 0.0 );
-
- double maxDist ( 0.0 );
-
- NICE::VVector::const_iterator posIt = positions.begin();
-
- for ( NICE::VVector::iterator i = features.begin();
- i != features.end();
- i++, posIt++)
- {
-
- i->normalizeL1();
-
-
- double minDist ( std::numeric_limits<double>::max() );
- for (NICE::VVector::const_iterator it = prototypes.begin(); it != prototypes.end(); it++)
- {
-
- double tmpDist ( this->distFunction->calculate(*i,*it) );
- if (tmpDist < minDist)
- minDist = tmpDist;
- }
-
- if (minDist > maxDist)
- maxDist = minDist;
-
-
- noveltyImage ( (*posIt)[0], (*posIt)[1] ) = minDist;
- }
-
-
- FloatImage noveltyImageGaussFiltered ( xsize, ysize );
- float sigma ( 3.0 );
- FilterT<float, float, float> filter;
- filter.filterGaussSigmaApproximate ( noveltyImage, sigma, &noveltyImageGaussFiltered );
- double maxFiltered ( noveltyImageGaussFiltered.max() );
- std::cerr << "maximum distance of Training images: " << maxDist << std::endl;
- std::cerr << "maximum distance of Training images after filtering: " << maxFiltered << std::endl;
- if ( beforeComputingNewFeatures )
- this->oldMaxDist = maxFiltered;
-
-
-
- NICE::ColorImage noveltyImageRGB ( xsize, ysize );
- if ( beforeComputingNewFeatures )
- {
- imageToPseudoColorWithRangeSpecification( noveltyImageGaussFiltered, noveltyImageRGB, 0 , maxValForVisualization );
- std::cerr << "set max value to: " << noveltyImageGaussFiltered.max() << std::endl;
- }
- else
- {
- imageToPseudoColorWithRangeSpecification( noveltyImageGaussFiltered, noveltyImageRGB, 0 , maxValForVisualization );
- std::cerr << "set max value to: " << this->oldMaxDist << std::endl;
- }
-
-
-
- if ( b_showResults )
- showImage(noveltyImageRGB, "Novelty Image");
- else
- {
- std::vector< std::string > list2;
- StringTools::split ( _filename, '/', list2 );
- std::string destination ( s_resultdir + NICE::intToString(this->newImageCounter -1 ) + "_" + list2.back() + "_3_updatedNoveltyMap.ppm");
- if ( beforeComputingNewFeatures )
- destination = s_resultdir + NICE::intToString(this->newImageCounter) + "_" + list2.back() + "_0_initialNoveltyMap.ppm";
- noveltyImageRGB.writePPM( destination );
- }
-
-
-
- int tmpProtCnt ( 0 );
- for (NICE::VVector::const_iterator protIt = prototypes.begin(); protIt != prototypes.end(); protIt++, tmpProtCnt++)
- {
- double distToNewCluster ( std::numeric_limits<double>::max() );
- int indexOfMostSimFeat( 0 );
- double tmpDist;
- int tmpCnt ( 0 );
-
- for ( NICE::VVector::iterator i = features.begin();
- i != features.end();
- i++, tmpCnt++)
- {
- tmpDist = this->distFunction->calculate( *i, *protIt );
- if ( tmpDist < distToNewCluster )
- {
- distToNewCluster = tmpDist;
- indexOfMostSimFeat = tmpCnt;
- }
- }
-
- int posX ( ( positions[indexOfMostSimFeat] ) [0] );
- int posY ( ( positions[indexOfMostSimFeat] ) [1] );
- NICE::Circle circ ( Coord( posX, posY), 2*tmpProtCnt , Color(200,0,255 ) );
- img.draw(circ);
- }
-
- if ( b_showResults )
- showImage(img, "Current image and most similar features for current cluster");
- else
- {
- std::vector< std::string > list2;
- StringTools::split ( _filename, '/', list2 );
- std::string destination ( s_resultdir + NICE::intToString(this->newImageCounter-1) + "_" + list2.back() + "_3_updatedCurrentCluster.ppm");
- if ( beforeComputingNewFeatures )
- destination = s_resultdir + NICE::intToString(this->newImageCounter) + "_" + list2.back() + "_0_initialCurrentCluster.ppm";
-
- img.writePPM( destination );
- }
-
- return noveltyImageGaussFiltered;
- }
|