00001 /*!@file Learn/SVMClassifier.H Support Vector Machine Classifier module */ 00002 // //////////////////////////////////////////////////////////////////// // 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00004 // University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // //////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00033 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Learn/SVMClassifier.H $ 00034 // $Id: SVMClassifier.H 14581 2011-03-08 07:18:09Z dparks $ 00035 // 00036 00037 #ifndef SVMCLASSIFIER_H_DEFINED 00038 #define SVMCLASSIFIER_H_DEFINED 00039 00040 #include <map> 00041 00042 #include "Component/ModelComponent.H" 00043 #include "Component/ModelParam.H" 00044 #include "Component/OptionManager.H" 00045 #include "Image/Image.H" 00046 00047 #include "svm.h" 00048 00049 namespace nub { template <class T> class ref; } 00050 00051 // ###################################################################### 00052 //! SVM Classifier Class 00053 class SVMClassifier 00054 { 00055 00056 public: 00057 //! Constructor 00058 SVMClassifier(float gamma=0.00078125, int C=32); 00059 00060 //! Destructor 00061 ~SVMClassifier(); 00062 00063 //! Train 00064 void train(std::string outputFileName, int id, std::vector<float> &feature); 00065 void train(std::string outputFileName, int id, float *&feature, unsigned int fdim); 00066 void train(std::string outputFilename, int id, float **&feature, unsigned int fdim1, unsigned int fdim2); 00067 00068 //! Train the SVM using the data in trainingData, with the ground truth data in dataClasses 00069 // trainingData is a matrix which contains column vectors of data points, where each row represents 00070 // a dimension of your data 00071 // dataClasses is a vector which defines the class type of each data point in the trainingData 00072 // (Your dataClasses vector should have as many elements as there are columns in your trainingData) 00073 void train(Image<double> trainingData, std::vector<double> dataClasses); 00074 void train(std::vector<std::vector<float> > trainingDta, std::vector<float> dataClasses); 00075 00076 //! Predict the class of a data point 00077 // dataPoint should be a column vector with each row representing a dimension of the data 00078 std::map<int,double> predictPDF(const svm_node* dataPointNodes); 00079 std::map<int,double> predictPDF(std::vector<float> &feature); 00080 00081 //! Predict 00082 double predict(Image<double> dataPoint, double *probability=NULL); 00083 double predict(std::vector<float> &feature, double *probability=NULL); 00084 double predict(float * &feature, unsigned int fdim, double *probability=NULL); 00085 double predict(float **&feature, unsigned int fdim1, unsigned int fdim2, double *probability=NULL); 00086 00087 void readModel(std::string modelFileName); //! Read model file into memory 00088 void writeModel(std::string modelFileName); //! Write model file into memory 00089 void readRange(std::string rangeFileName); //! Read a range file to scale the feature vector, if needed 00090 float rescaleValue(float value, unsigned int index); //! If range is enabled, rescale the value to 00091 00092 protected: 00093 int _getBestLabel(std::map<int,double> pdf, double *probability); 00094 double _predict(struct svm_node *node, unsigned int fdim, double * probability); 00095 void _train(svm_problem& trainingProblem); 00096 00097 struct svm_parameter itsSVMParams; 00098 struct svm_model* itsSVMModel; 00099 bool itsSVMRangeEnabled; 00100 std::vector<double> itsSVMFeatureRangeMax; 00101 std::vector<double> itsSVMFeatureRangeMin; 00102 double itsSVMFeatureRangeUpper; 00103 double itsSVMFeatureRangeLower; 00104 }; 00105 00106 #endif 00107 00108 00109 // ###################################################################### 00110 /* So things look consistent in everyone's emacs... */ 00111 /* Local Variables: */ 00112 /* indent-tabs-mode: nil */ 00113 /* End: */ 00114