123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- /**
- * @file CodebookRandomForest.cpp
- * @brief feature CodebookRandomForest
- * @author Erik Rodner
- * @date 02/15/2008
- */
- #include <queue>
- #include <iostream>
- #include "CodebookRandomForest.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- #undef DEBUGPRUNING
- CodebookRandomForest::CodebookRandomForest( int maxDepth, int restrictedCodebookSize )
- {
- this->clusterforest = NULL;
- this->maxDepth = maxDepth;
- this->restrictedCodebookSize = restrictedCodebookSize;
- }
- CodebookRandomForest::CodebookRandomForest( FPCRandomForests *clusterforest,
- int maxDepth, int restrictedCodebookSize )
- {
- this->clusterforest = clusterforest;
- this->maxDepth = maxDepth;
- this->restrictedCodebookSize = restrictedCodebookSize;
- buildLeafMap();
- }
- CodebookRandomForest::~CodebookRandomForest()
- {
- if ( clusterforest != NULL )
- delete clusterforest;
- }
- void CodebookRandomForest::setClusterForest( FPCRandomForests *clusterforest)
- {
- if(this->clusterforest != NULL)
- delete this->clusterforest;
-
- this->clusterforest = clusterforest;
- buildLeafMap();
- }
- void CodebookRandomForest::buildParentStructure ( DecisionNode *node,
- map<DecisionNode *, DecisionNode *> & parentStructure )
- {
- if ( node == NULL ) return;
- if ( node->left != NULL )
- {
- parentStructure.insert ( pair<DecisionNode *, DecisionNode *> ( node->left, node ) );
- buildParentStructure ( node->left, parentStructure );
- }
- if ( node->right != NULL )
- {
- parentStructure.insert ( pair<DecisionNode *, DecisionNode *> ( node->right, node ) );
- buildParentStructure ( node->right, parentStructure );
- }
- }
- void CodebookRandomForest::pruneForest ()
- {
- map<DecisionNode *, pair<long, int> > index;
- clusterforest->indexDescendants( index );
- map<DecisionNode *, DecisionNode *> parentStructure;
- vector<DecisionTree *> & trees = clusterforest->getForestNonConst();
- for ( vector<DecisionTree *>::const_iterator i = trees.begin();
- i != trees.end(); i++ )
- {
- DecisionTree *tree = *i;
- parentStructure.insert ( pair<DecisionNode *, DecisionNode *> ( tree->getRoot(), NULL ) );
- buildParentStructure ( tree->getRoot(), parentStructure );
- }
- priority_queue< triplet<double, long, DecisionNode *> > lastLevelInnerNodes;
- long leafs = 0;
- for ( map<DecisionNode *, pair<long, int> >::const_iterator k = index.begin();
- k != index.end(); k++ )
- {
- DecisionNode *node = k->first;
- if ( (!node->isLeaf()) && ((node->left->isLeaf())
- || (node->right->isLeaf())) )
- {
- double mi = node->distribution.entropy() - (
- node->left->distribution.sum() * node->left->distribution.entropy() +
- node->right->distribution.sum() * node->right->distribution.entropy() )
- / node->distribution.sum();
- lastLevelInnerNodes.push ( triplet<double, long, DecisionNode *> (
- - mi, k->second.first, node ) );
- }
- if ( node->isLeaf() ) leafs++;
- }
- set< DecisionNode * > deletedRoots;
- /*********************************************
- * EVIL Pruning method *
- *********************************************/
- set<DecisionNode *> deletedNodes;
- while ( (leafs > restrictedCodebookSize) && (lastLevelInnerNodes.size() > 0) )
- {
- const triplet<double, long, DecisionNode *> & nodemi = lastLevelInnerNodes.top();
- #ifdef DEBUGPRUNING
- double current_mi = -nodemi.first;
- fprintf (stderr, "CodebookRandomForest: %d contract leaf with mutual information %f\n", leafs, current_mi );
- #endif
- DecisionNode *node = nodemi.third;
- lastLevelInnerNodes.pop();
- assert ( node != NULL );
- DecisionNode *left = node->left;
- DecisionNode *right = node->right;
- //fprintf (stderr, "node: %ld, left: %ld, right: %ld\n", (long int)node, (long int)left,
- // (long int)right );
- if ( (deletedNodes.find(node) != deletedNodes.end() ) || node->isLeaf() ) {
- // this is a tricky case...consider the subsequent contraction of
- // two childs of a node
- // After the first child is contracted, the node is added to lastLevelInnerNodes
- // If the second child is contracted, the node is still in the queue but
- // is now a leaf node.
- // A second problem exists if the parent node is contracted after the second
- // child but before the node. Therefore we introduced deletedNodes.
- continue;
- }
- #ifdef DEBUGPRUNING
- fprintf (stderr, "CodebookRandomForest: nodes remaining %ld (min:%d); current mi %f\n",
- leafs, restrictedCodebookSize, current_mi );
- #endif
-
- assert ( parentStructure.find(node) != parentStructure.end() );
- DecisionNode *parent_node = parentStructure[node];
- //fprintf (stderr, "parent: %ld\n", (long int)parent_node );
- if ( parent_node == NULL )
- {
- #ifdef DEBUGPRUNING
- fprintf (stderr, "CodebookRandomForest: Deleting the root node !!!\n");
- #endif
- DecisionNode *newParent = NULL;
- if ( (left->isLeaf()) && (right->isLeaf()) )
- {
- //fprintf (stderr, "case (a)\n");
- delete ( node->f );
- node->f = NULL;
- delete left;
- delete right;
- deletedNodes.insert ( left );
- deletedNodes.insert ( right );
-
- node->left = NULL;
- node->right = NULL;
- newParent = node;
- leafs--;
- } else if ( left->isLeaf() ) {
- // case (b) left child is a leaf
- delete left;
- delete node;
- deletedNodes.insert ( node );
- deletedNodes.insert ( left );
- parentStructure[right] = parent_node;
- newParent = right;
- leafs--;
- } else if ( right->isLeaf() ) {
- // case (b) right child is a leaf
- delete right;
- delete node;
- deletedNodes.insert ( right );
- deletedNodes.insert ( left );
- parentStructure[left] = parent_node;
- newParent = left;
- leafs--;
- } else {
- fprintf (stderr, "UNKNOWN CASE !!\n");
- exit(-1);
- }
- for ( vector<DecisionTree *>::iterator i = trees.begin(); i != trees.end() ; i++ )
- if ( (*i)->getRoot() == node )
- (*i)->setRoot(newParent);
- continue;
- }
- long int parent_index = index[parent_node].first;
- double mi = 0.0;
- bool nodeIsLeft = ( parent_node->left == node );
- DecisionNode *sibling = nodeIsLeft ? parent_node->right : parent_node->left;
- if ( (left == NULL) || (right == NULL) )
- fthrow(Exception, "There is a bug in this code: CodebookRandomForest (good luck!) bugid=1");
- if ( (left->isLeaf()) && (right->isLeaf()) )
- {
- /* ------------ case (a) left and right childs are leafs
- (p) (p)
- (n) (s) -> (n) (s) and add p to the last level nodes
- (l) (r) */
- #ifdef DEBUGPRUNING
- fprintf (stderr, "case (a)\n");
- #endif
- delete ( node->f );
- node->f = NULL;
- delete left;
- deletedNodes.insert ( left );
- delete right;
- deletedNodes.insert ( right );
- node->left = NULL;
- node->right = NULL;
- leafs--;
- double ep = parent_node->distribution.entropy();
- double en = node->distribution.entropy();
- double es = sibling->distribution.entropy();
- double pn = node->distribution.sum();
- double ps = sibling->distribution.sum();
- mi = ep - ( pn * en + ps * es ) / (pn+ps);
- #ifdef DEBUGPRUNING
- fprintf (stderr, "ep %f en %f es %f pn %f ps %f\n",
- ep, en, es, pn, ps );
- parent_node->distribution.store(cerr);
- node->distribution.store(cerr);
- sibling->distribution.store(cerr);
-
- fprintf (stderr, "add new pre-leaf %ld: mi %lf top %lf\n", (long int)parent_node, mi,
- -lastLevelInnerNodes.top().first);
- #endif
- lastLevelInnerNodes.push ( triplet<double, long, DecisionNode *> (
- - mi, parent_index, parent_node ) );
- } else if ( left->isLeaf() ) {
- // --------------- case (b) left child is a leaf
-
- #ifdef DEBUGPRUNING
- fprintf (stderr, "case (b)\n");
- #endif
- if ( nodeIsLeft )
- parent_node->left = right;
- else
- parent_node->right = right;
- parentStructure[right] = parent_node;
- delete left;
- deletedNodes.insert ( left );
- delete node;
- deletedNodes.insert ( node );
- leafs--;
-
- } else if ( right->isLeaf() ) {
-
- // --------------- case (c) right child is a leaf
-
- #ifdef DEBUGPRUNING
- fprintf (stderr, "case (c)\n");
- #endif
- if ( nodeIsLeft )
- parent_node->left = left;
- else
- parent_node->right = left;
-
- delete right;
- deletedNodes.insert ( right );
- delete node;
- deletedNodes.insert ( node );
- parentStructure[left] = parent_node;
- leafs--;
- } else {
- fthrow(Exception, "There is a bug in this code: CodebookRandomForest (good luck!) bugid=2");
- }
- }
- for ( vector<DecisionTree *>::iterator i = trees.begin(); i != trees.end() ; )
- {
- if ( deletedRoots.find((*i)->getRoot()) != deletedRoots.end() )
- {
- delete (*i);
- trees.erase ( i );
- } else {
- i++;
- }
- }
- #ifdef DEBUGPRUNING
- fprintf (stderr, "Final number of leafs: %ld (%d)\n", leafs, restrictedCodebookSize );
- #endif
- }
- void CodebookRandomForest::buildLeafMap ()
- {
- if ( restrictedCodebookSize > 0 ) {
- pruneForest ();
- }
- map<DecisionNode *, pair<long, int> > index;
- vector< pair<long, DecisionNode *> > index_reverse;
- clusterforest->indexDescendants ( index );
- leafMap.clear();
- for ( map<DecisionNode *, pair<long, int> >::const_iterator i = index.begin();
- i != index.end(); i++ )
- {
- DecisionNode *node = i->first;
- int depth = i->second.second;
- long index = i->second.first;
- if ( ( (node->right == NULL) && (node->left == NULL) && (depth <= maxDepth) ) || ( depth == maxDepth ) )
- index_reverse.push_back ( pair<long, DecisionNode *> ( index, node ) );
- }
- sort ( index_reverse.begin(), index_reverse.end() );
- /*************************************
- Recover a kind of canonical node
- permutation
- **************************************/
- for ( vector< pair<long, DecisionNode *> >::const_iterator i = index_reverse.begin();
- i != index_reverse.end(); i++ )
- {
- DecisionNode *node = i->second;
- leafMap.insert ( pair<DecisionNode *, int> ( node, leafMap.size() ) );
- }
- #ifdef DEBUGPRUNING
- fprintf (stderr, "CSRandomForest::buildLeafMap: dimension = %d\n", (int)leafMap.size() );
- #endif
- reinit ( leafMap.size() );
- }
- void CodebookRandomForest::copy ( const Codebook *codebook )
- {
- fthrow(Exception, "CodebookRandomForest::not yet implemented !\n");
- }
- void CodebookRandomForest::vote ( const NICE::Vector & feature, int & codebookEntry, double & weight, double & distance ) const
- {
- fthrow(Exception, "CodebookRandomForest::not supported, please use multi voting feature\n");
- }
- void CodebookRandomForest::vote ( const NICE::Vector & feature, NICE::Vector & histogram,
- int & codebookEntry, double & weight, double & distance ) const
- {
- SparseVector votes;
- vote ( feature, votes );
- for ( SparseVector::const_iterator i = votes.begin();
- i != votes.end(); i++ )
- {
- int index = i->first;
- double val = i->second;
- histogram[index] += val;
- if ( i == votes.begin() )
- {
- codebookEntry = index;
- weight = val;
- }
- }
- distance = 0.0;
- }
- void CodebookRandomForest::vote ( const NICE::Vector & feature, NICE::SparseVector & votes ) const
- {
- vector<DecisionNode *> leafNodes;
- NICE::Vector *x = new NICE::Vector ( feature );
- Example pe ( x );
- clusterforest->getLeafNodes ( pe, leafNodes, maxDepth );
- delete x;
- for ( vector<DecisionNode *>::const_iterator j = leafNodes.begin();
- j != leafNodes.end(); j++ )
- {
- map<DecisionNode *, int>::const_iterator k = leafMap.find ( *j );
- assert ( k != leafMap.end() );
- int leafindex = k->second;
- votes.insert ( votes.begin(), pair<int, double> ( leafindex, 1.0 ) );
- }
- }
- void CodebookRandomForest::voteAndClassify ( const NICE::Vector & feature, NICE::SparseVector & votes, FullVector & distribution ) const
- {
- vector<DecisionNode *> leafNodes;
- NICE::Vector *x = new NICE::Vector ( feature );
- Example pe ( x );
- clusterforest->getLeafNodes ( pe, leafNodes, maxDepth );
- delete x;
- for ( vector<DecisionNode *>::const_iterator j = leafNodes.begin();
- j != leafNodes.end(); j++ )
- {
- map<DecisionNode *, int>::const_iterator k = leafMap.find ( *j );
- DecisionNode *node = *j;
- assert ( k != leafMap.end() );
- int leafindex = k->second;
- votes.insert ( votes.begin(), pair<int, double> ( leafindex, 1.0 ) );
- FullVector sDistribution ( node->distribution );
- sDistribution.normalize();
- if ( distribution.empty() )
- distribution = sDistribution;
- else
- distribution.add ( sDistribution );
- }
- distribution.normalize();
- }
- void CodebookRandomForest::voteAndClassify(const Vector &feature, SparseVector &votes, Vector &distribution) const
- {
- vector<DecisionNode *> leafNodes;
- NICE::Vector *x = new NICE::Vector ( feature );
- Example pe ( x );
- clusterforest->getLeafNodes ( pe, leafNodes, maxDepth );
- delete x;
- for ( vector<DecisionNode *>::const_iterator j = leafNodes.begin();
- j != leafNodes.end(); j++ )
- {
- map<DecisionNode *, int>::const_iterator k = leafMap.find ( *j );
- DecisionNode *node = *j;
- assert ( k != leafMap.end() );
- int leafindex = k->second;
- votes.insert ( votes.begin(), pair<int, double> ( leafindex, 1.0 ) );
- FullVector sDistribution ( node->distribution );
- sDistribution.normalize();
- if ( distribution.size() == 0 )
- {
- distribution.resize(sDistribution.size() );
- distribution.set(0.0f);
- }
- for(int i = 0; i< sDistribution.size(); i++)
- distribution[i] += sDistribution[i];
- }
- distribution.normalizeL2();
- }
- void CodebookRandomForest::add ( const Codebook *codebook )
- {
- fthrow ( Exception, "CodebookRandomForest::not yet implemented !");
- }
-
- Codebook *CodebookRandomForest::clone () const
- {
- return (new CodebookRandomForest(maxDepth, restrictedCodebookSize));
- }
- void CodebookRandomForest::clear ()
- {
- if ( clusterforest != NULL )
- clusterforest->clear();
- Codebook::clear();
- }
- void CodebookRandomForest::restore ( istream & is, int format )
- {
- if (is.good())
- {
- std::string tmp;
- is >> tmp; //class name
- if ( ! this->isStartTag( tmp, "CodebookRandomForest" ) )
- {
- std::cerr << " WARNING - attempt to restore CodebookRandomForest, but start flag " << tmp << " does not match! Aborting... " << std::endl;
- throw;
- }
- if(this->clusterforest == NULL)
- this->clusterforest = new FPCRandomForests ();
- bool b_endOfBlock = false;
- while ( !b_endOfBlock )
- {
- is >> tmp; // start of block
- if ( this->isEndTag( tmp, "CodebookRandomForest" ) || is.eof() )
- {
- b_endOfBlock = true;
- continue;
- }
- tmp = this->removeStartTag ( tmp );
- if ( tmp.compare("baseclass") == 0 )
- {
- Codebook::restore(is, format);
- is >> tmp; // end of block
- tmp = this->removeEndTag ( tmp );
- }
- else if ( tmp.compare("maxDepth") == 0 )
- {
- is >> maxDepth;
- is >> tmp; // end of block
- tmp = this->removeEndTag ( tmp );
- }
- else if ( tmp.compare("restrictedCodebookSize") == 0 )
- {
- is >> restrictedCodebookSize;
- is >> tmp; // end of block
- tmp = this->removeEndTag ( tmp );
- }
- else if ( tmp.compare("maxClassNo") == 0 )
- {
- int maxClassNo = 0;
- is >> maxClassNo;
- is >> tmp; // end of block
- tmp = this->removeEndTag ( tmp );
- if(clusterforest != NULL)
- clusterforest->setMaxClassNo(maxClassNo);
- }
- else if ( tmp.compare("clusterforest") == 0 )
- {
- clusterforest->restore ( is, format );
- is >> tmp; // end of block
- tmp = this->removeEndTag ( tmp );
- }
- }
- buildLeafMap();
- }
- }
- void CodebookRandomForest::store ( ostream & os, int format ) const
- {
- if (os.good())
- {
- // show starting point
- os << this->createStartTag( "CodebookRandomForest" ) << std::endl;
- os.precision (numeric_limits<double>::digits10 + 1);
- os << this->createStartTag( "baseclass" ) << std::endl;
- Codebook::store ( os, format );
- os << this->createEndTag( "baseclass" ) << std::endl;
- os << this->createStartTag( "maxDepth" ) << std::endl;
- os << maxDepth << std::endl;
- os << this->createEndTag( "maxDepth" ) << std::endl;
- os << this->createStartTag( "restrictedCodebookSize" ) << std::endl;
- os << restrictedCodebookSize << std::endl;
- os << this->createEndTag( "restrictedCodebookSize" ) << std::endl;
- os << this->createStartTag( "maxClassNo" ) << std::endl;
- os << clusterforest->getMaxClassNo() << endl;
- os << this->createEndTag( "maxClassNo" ) << std::endl;
- os << this->createStartTag( "clusterforest" ) << std::endl;
- clusterforest->store ( os, format );
- os << this->createEndTag( "clusterforest" ) << std::endl;
- /* Codebook::store ( os, format );
- os << maxDepth << endl;
- os << restrictedCodebookSize << endl;
- os << clusterforest->getMaxClassNo() << endl;
- clusterforest->store ( os, format );
- os << endl;
- */
- // done
- os << this->createEndTag( "CodebookRandomForest" ) << std::endl;
- }
- }
|