Browse Source

Added virtual functions for VecClassifier and FeaturePoolClassifier, changes on VC-NN-classifier

Alexander Freytag 12 years ago
parent
commit
9143002676

+ 22 - 0
cbaselib/LabeledSet.cpp

@@ -468,4 +468,26 @@ void LabeledSetVector::getFlatRepresentation ( VVector & vecSet, NICE::Vector &
 
 }
 
+void LabeledSetVector::removePointersToDataWithoutDeletion()
+{
+  //remove pointers in the order-struct if needed
+  if ( ! this->selection ) {
+    for ( Permutation::iterator i  = this->insertOrder.begin();
+          i != this->insertOrder.end();
+          i++ )
+    {
+      i->second = NULL;
+    }
+  }
+  
+  //remove pointers in normal map
+  for ( std::map< int, std::vector<NICE::Vector *> >::iterator iter = this->begin(); iter != this->end(); ++iter )
+  {
+    for ( int j = 0; j < (int)iter->second.size(); j++ )
+    {
+      iter->second[j] = NULL;
+    }
+  }  
+}
+
 #endif

+ 5 - 0
cbaselib/LabeledSet.h

@@ -165,6 +165,11 @@ class LabeledSetVector :
      * @param vecSetLabels labels (output)
      */
     void getFlatRepresentation ( NICE::VVector & vecSet, NICE::Vector & vecSetLabels ) const;
+    
+    /**
+     * @brief set all pointers to the data to NULL, i.e., keep the data in storage, but remove the pointers of this data struct
+     */
+    void removePointersToDataWithoutDeletion();
 
     friend class LabeledSetSelection<LabeledSetVector>;
 };

+ 6 - 0
classifier/classifierbase/FeaturePoolClassifier.cpp

@@ -78,3 +78,9 @@ void FeaturePoolClassifier::setComplexity ( int size )
     fprintf (stderr, "FeaturePoolClassifier::setComplexity: not yet implemented in subordinate class\n");
     exit(-1);
 }
+
+void FeaturePoolClassifier::addMultipleExamples( OBJREC::Examples & newExamples)
+{
+    fprintf (stderr, "FeaturePoolClassifier::addMultipleExamples: not yet implemented in subordinate class\n");
+    exit(-1);
+}

+ 3 - 0
classifier/classifierbase/FeaturePoolClassifier.h

@@ -50,6 +50,9 @@ class FeaturePoolClassifier : public NICE::Persistent
 
 	/** set complexity for the next training process e.g. number of weak classifiers */
 	virtual void setComplexity ( int size );
+  
+  /** add multiple examples given in the OBJREC::Examples data structure*/
+  virtual void addMultipleExamples( OBJREC::Examples & newExamples);
 
 };
 

+ 7 - 0
classifier/classifierbase/VecClassifier.h

@@ -15,6 +15,7 @@
 #include "vislearning/cbaselib/ClassificationResult.h"
 
 #define ROADWORKS fthrow(NICE::Exception, "clone(): not yet implemented!");
+#define ROADWORKSADD fthrow(NICE::Exception, "teach (int classno, const NICE::Vector & x ): not yet implemented!");
 
 namespace OBJREC
 {
@@ -56,6 +57,12 @@ class VecClassifier : public NICE::Persistent
     {
       ROADWORKS;
     };
+    
+    virtual void teach (int classno, const NICE::Vector & x )
+    {
+      ROADWORKSADD;
+    };
+
 };
 
 #undef ROADWORKS

+ 72 - 49
classifier/vclassifier/VCNearestNeighbour.cpp

@@ -57,27 +57,27 @@ ClassificationResult VCNearestNeighbour::classify ( const NICE::Vector & x ) con
     priority_queue< pair<double, int> > examples;
     LOOP_ALL(teachSet) 
     {
-	EACH(classno,y)
-
-	double distance = distancefunc->calculate ( x, y );
-
-	if ( isnan(distance) )
-	{
-	    fprintf (stderr, "VCNearestNeighbour::classify: NAN value found !!\n");
-	    cerr << x << endl;
-	    cerr << y << endl;
-	}
-
-	if ( mindists[classno] > distance )
-	    mindists[classno] = distance;
-
-	if ( mindist > distance )
-	{
-	    minclass = classno;
-	    mindist  = distance;
-	}
-	if ( K > 1 ) 
-	    examples.push ( pair<double, int> ( -distance, classno ) );
+      EACH(classno,y)
+
+      double distance = distancefunc->calculate ( x, y );
+
+      if ( isnan(distance) )
+      {
+          fprintf (stderr, "VCNearestNeighbour::classify: NAN value found !!\n");
+          cerr << x << endl;
+          cerr << y << endl;
+      }
+
+      if ( mindists[classno] > distance )
+          mindists[classno] = distance;
+
+      if ( mindist > distance )
+      {
+          minclass = classno;
+          mindist  = distance;
+      }
+      if ( K > 1 ) 
+        examples.push ( pair<double, int> ( -distance, classno ) );
     }
 
     if ( mindist == 0.0 )
@@ -88,24 +88,28 @@ ClassificationResult VCNearestNeighbour::classify ( const NICE::Vector & x ) con
 		fprintf (stderr, "class %d : %f\n", i, mindists.get(i) );
 #endif
 
-    if ( K > 1 ) {
-		FullVector votes ( maxClassNo + 1 );
-		votes.set(0.0);
-		for ( int k = 0 ; k < K ; k++ )
-		{
-			const pair<double, int> & t = examples.top();
-			votes[ t.second ]++;
-			examples.pop();
-		}
-		votes.normalize();
-		return ClassificationResult ( votes.maxElement(), votes );
-    } else {
-		for ( int i = 0 ; i < mindists.size() ; i++ )
-		{
-			mindists[i] = 1.0 / (mindists[i] + 1.0);
-		}
-		mindists.normalize();
-		return ClassificationResult ( minclass, mindists );
+    if ( K > 1 )
+    {
+      FullVector votes ( maxClassNo + 1 );
+      votes.set(0.0);
+      for ( int k = 0 ; k < K ; k++ )
+      {
+        const pair<double, int> & t = examples.top();
+        votes[ t.second ]++;
+        examples.pop();
+      }
+      votes.normalize();
+      return ClassificationResult ( votes.maxElement(), votes );
+    }
+    else
+    {
+      //do we really want to do this? Only useful, if we want to obtain probability like scores      
+//       for ( int i = 0 ; i < mindists.size() ; i++ )
+//       {
+//         mindists[i] = 1.0 / (mindists[i] + 1.0);
+//       }
+      //mindists.normalize();
+      return ClassificationResult ( minclass, mindists );
     }
 }
 
@@ -114,24 +118,45 @@ void VCNearestNeighbour::teach ( const LabeledSetVector & _teachSet )
 {
     fprintf (stderr, "teach using all !\n");
     maxClassNo = _teachSet.getMaxClassno();
-    teachSet = _teachSet;
+    //NOTE this is crucial if we clear _teachSet afterwards!
+    //therefore, take care NOT to call _techSet.clear() somewhere out of this method
+    this->teachSet = _teachSet;
+    
+    std::cerr << "number of known training samples: " << this->teachSet.begin()->second.size() << std::endl;
+    
+//     //just for testing - remove everything but the first element
+//     map< int, vector<NICE::Vector *> >::iterator it = this->teachSet.begin();
+//     it++;
+//     this->teachSet.erase(it, this->teachSet.end());
+//     std::cerr << "keep " << this->teachSet.size() << " elements" << std::endl;
+    
+    
 }
 
 
 void VCNearestNeighbour::teach ( int classno, const NICE::Vector & x )
 {
-    fprintf (stderr, "teach!\n");
+    std::cerr << "VCNearestNeighbour::teach one new example" << std::endl;
+    
     for ( size_t i = 0 ; i < x.size() ; i++ )
-	if ( isnan(x[i]) ) 
-	{
-	    fprintf (stderr, "There is a NAN value in within this vector: x[%d] = %f\n", (int)i, x[i]);
-	    cerr << x << endl;
-	    exit(-1);
-	}
+      if ( isnan(x[i]) ) 
+      {
+          fprintf (stderr, "There is a NAN value in within this vector: x[%d] = %f\n", (int)i, x[i]);
+          cerr << x << endl;
+          exit(-1);
+      }
     
     if ( classno > maxClassNo ) maxClassNo = classno;
 
     teachSet.add ( classno, x );
+    
+    std::cerr << "adden class " << classno << " with feature " << x << std::endl;
+    int tmpCnt(0);
+    for (LabeledSetVector::const_iterator it = this->teachSet.begin(); it != this->teachSet.end(); it++)
+    {
+      tmpCnt += it->second.size();
+    }
+    std::cerr << "number of known training samples: " << tmpCnt << std::endl;
 }
 
 void VCNearestNeighbour::finishTeaching()
@@ -159,5 +184,3 @@ void VCNearestNeighbour::restore ( std::istream & is, int format )
     teachSet.restore ( is, format );
     maxClassNo = teachSet.getMaxClassno();
 }
-
-

+ 1 - 0
classifier/vclassifier/VCNearestNeighbour.h

@@ -56,6 +56,7 @@ class VCNearestNeighbour : public VecClassifier
 		void store ( std::ostream & os, int format = 0 ) const;
 
 		void restore ( std::istream & is, int format = 0 );
+    
 };