Browse Source

VCNearestClassMean added, files added

Michael Kemmler 13 years ago
parent
commit
d173f5a220

+ 110 - 0
classifier/vclassifier/VCNearestClassMean.cpp

@@ -0,0 +1,110 @@
+#ifdef NICE_USELIB_ICE
+
+#include <iostream>
+
+#include "vislearning/classifier/vclassifier/VCNearestClassMean.h"
+
+using namespace OBJREC;
+
+using namespace std;
+
+using namespace NICE;
+
+VCNearestClassMean::VCNearestClassMean( const Config *_conf, NICE::VectorDistance<double> *_distancefunc  ) 
+: VecClassifier ( _conf ), distancefunc (_distancefunc)
+{    
+    if ( _distancefunc == NULL )
+		distancefunc = new EuclidianDistance<double>();
+}
+
+VCNearestClassMean::~VCNearestClassMean()
+{
+    clear();
+}
+
+/** classify using simple vector */
+
+ClassificationResult VCNearestClassMean::classify ( const NICE::Vector & x ) const
+{
+     double min_distance= std::numeric_limits<double>::max();
+     int min_class = -1;
+     FullVector scores ( classNo.size() );
+     
+     for(uint i=0;i<this->classNo.size();i++)
+     {
+          double distance = distancefunc->calculate ( x, means[i] );
+          scores[i] = - distance;
+          if ( distance < min_distance)
+          {
+               min_distance = distance;
+               min_class = classNo[i];
+          }
+     }
+     
+     return ClassificationResult ( min_class, scores );
+}
+
+
+void VCNearestClassMean::teach ( const LabeledSetVector & _teachSet )
+{
+
+    _teachSet.getClasses ( this->classNo );
+
+    //initialize means
+    NICE::Vector zero( _teachSet.dimension() );
+    for(uint d=0;d<zero.size();d++) zero[d]=0.0;
+    for(uint c=0;c<this->classNo.size();c++)
+    {
+	means.push_back(zero);
+    }
+
+    //add all class-specific vectors 
+    int index=0;
+    LOOP_ALL(_teachSet)
+    {
+	EACH(classno,x);
+	//determine index
+	for(uint c=0;c<this->classNo.size();c++)
+        {
+		if(classno==classNo[c]) index=c;
+        }
+	for(uint d=0;d<zero.size();d++)
+        {
+	   means[index][d]+=x[d];
+        }
+    }
+
+    //normalize vectors
+    for(uint c=0;c<this->classNo.size();c++)
+    {
+	for(uint d=0;d<zero.size();d++)
+        {
+	   means[c][d]/=_teachSet.count(this->classNo[c]);
+        }
+    }
+
+}
+
+void VCNearestClassMean::finishTeaching()
+{
+//nothing more to do
+}
+
+void VCNearestClassMean::clear ()
+{
+//nothing to do
+}
+
+void VCNearestClassMean::store ( std::ostream & os, int format ) const
+{
+    fprintf (stderr, "NOT YET IMPLEMENTED\n");
+    exit(-1);
+}
+
+void VCNearestClassMean::restore ( std::istream & is, int format )
+{
+    fprintf (stderr, "NOT YET IMPLEMENTED\n");
+    exit(-1);
+}
+
+#endif

+ 55 - 0
classifier/vclassifier/VCNearestClassMean.h

@@ -0,0 +1,55 @@
+/** 
+* @file VCNearestClassMean.h
+* @brief Nearest Class Mean Classifier (Naive Bayes with identity covariance matrix for all classes) -> code is based on VCSimpleGaussian and VCNearestNeighbor
+* @author Erik Rodner + Mi.Ke
+* @date 12/05/2007
+
+*/
+
+#ifndef VCNEARESTCLASSMEANINCLUDE
+#define VCNEARESTCLASSMEANINCLUDE
+
+#include "vislearning/classifier/classifierbase/VecClassifier.h"
+#include <core/vector/Distance.h>
+
+#include <image_nonvis.h>
+
+namespace OBJREC {
+
+/** Simple Gaussian Classifier */
+class VCNearestClassMean : public VecClassifier
+{
+    public:
+	std::vector<NICE::Vector> means;
+	std::vector<int> classNo;
+	NICE::VectorDistance<double> *distancefunc;
+	std::map<int, ice::Statistics *> statistics;
+
+    public:
+  
+	/** simple constructor */
+	VCNearestClassMean( const NICE::Config *conf, NICE::VectorDistance<double> *distancefunc = NULL );
+      
+	/** simple destructor */
+	virtual ~VCNearestClassMean();
+ 
+	/** classify using simple vector */
+	ClassificationResult classify ( const NICE::Vector & x ) const;
+
+	/** classify using a simple vector */
+	void teach ( const LabeledSetVector & teachSet );
+	
+	void finishTeaching();
+
+	void clear ();
+
+	void store ( std::ostream & os, int format = 0 ) const;
+
+	void restore ( std::istream & is, int format = 0 );
+
+};
+
+
+} // namespace
+
+#endif