KSquare Utilities
svm.h
Go to the documentation of this file.
1 #ifndef _LIBSVM_H
2 #define _LIBSVM_H
3 #include <iostream>
4 
5 //#pragma warning (disable:4786)
6 
7 #include <set>
8 #include <vector>
9 
10 #include "KKException.h"
11 #include "KKStr.h"
12 #include "RunLog.h"
13 #include "XmlStream.h"
14 
15 /**
16  *@namespace SVM233
17  *@brief This is version 2.33 of "Chih-Chung Chang" and "Chih-Jen Lin" Support vector Machine; the class "ModelOldSVM" calls ths version.
18  *@brief This is a vesion 2.33 of "Chih-Chung Chang" and "Chih-Jen Lin" libSVM (Support Vector Machine); the class "ModelOldSVM" calls ths version.
19  *@details
20  *<br/>
21  * libSVM is a Support Vector Machine implementation done by "Chih-Chung Chang" and "Chih-Jen Lin". It
22  * was downloaded from http://www.csie.ntu.edu.tw/~cjlin/libsvm/. The source code was modified by
23  * Tong Luo and Kurt Kramer.
24  */
25 namespace SVM233
26 {
27  const kkint32 SCALE = 100;
28 
29 
30  struct svm_node
31  {
33  {
34  index = -1;
35  value = 0.0;
36  }
37 
39  double value;
40  };
41 
42 
43  struct svm_problem
44  {
45  svm_problem ();
46  ~svm_problem ();
47 
49  double* y;
50  VectorKKStr exampleNames; /*!< allows the user to provide names to the labels */
51  kkint32* index; //luo add
52  struct svm_node** x;
53  double* W;
54 
56 
57  //luo: the list of data in this bin
58  //struct svm_node **elements;
59  };
60 
61 
62  enum { C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR }; /* svm_type */
63  enum { LINEAR, POLY, RBF, SIGMOID }; /* kernel_type */
64 
66  {
67  public:
68  svm_parameter ();
69  svm_parameter (const svm_parameter& parameters);
70  svm_parameter (KKStr& paramStr);
71  svm_parameter& operator= (const svm_parameter& right);
72 
73  KKStr ToCmdLineStr () const;
74  KKStr ToTabDelStr () const;
75  void ParseTabDelStr (const KKStr& _str);
76 
77  double coef0; /* for poly/sigmoid */
78  double degree; /* for poly */
79  double gamma; /* for poly/rbf/sigmoid */
82 
83  /* these are for training only */
84  double C; /* for C_SVC, EPSILON_SVR and NU_SVR */
85  double cache_size; /* in MB */
86  double eps; /* stopping criteria */
87  kkint32 nr_weight; /* for C_SVC */
88  double nu; /* for NU_SVC, ONE_CLASS, and NU_SVR */
89  double p; /* for EPSILON_SVR */
90  kkint32 probability; /* do probability estimates */
91  kkint32 shrinking; /* use the shrinking heuristics */
92  double* weight; /* for C_SVC */
93  kkint32* weight_label; /* for C_SVC */
94 
95 
96  //luo add
97  float A;
99  float cBoost;
100  double confidence;
103  double* featureWeight;
107  float sample; //random sample ratio
109  float threshold; //threshold for merging two training images
110 
111 
112  // luo
113 
114  void Gamma (double _gamma) {gamma = _gamma;}
115  void KernalType (kkint32 _kernalType) {kernel_type = _kernalType;}
116 
117  double Gamma () const {return gamma;}
118  kkint32 KernalType () const {return kernel_type;}
119 
121 
122  void ProcessSvmParameter (KKStr cmd,
123  KKStr value,
124  double valueNum,
125  bool& parmUsed
126  );
127  }; /* svm_parameter */
128 
129 
130 
131 
132 //
133 // SvmModel233
134 //
136 {
137  svm_parameter param; // parameter
138  kkint32 nr_class; // number of classes, = 2 in regression/one class svm
139  kkint32 l; // total #SV
140  VectorKKStr exampleNames; /*!< allows the user to provide names to the labels */
141  svm_node** SV; /*!< SVs (SV[l]) Support vector feature numbers */
142  double** sv_coef; // coefficients for SVs in decision functions (sv_coef[n-1][l])
143  double* rho; // constants in decision functions (rho[n*(n-1)/2])
144 
145  // for classification only
146 
147  kkint32* label; // label of each class (label[n])
148  kkint32* nSV; /**< number of SVs for each class (nSV[n])
149  * nSV[0] + nSV[1] + ... + nSV[n-1] = l
150  */
151 
152  //luo add
156 
158  double* margin;
159  double weight;
160 
162  double* featureWeight;
163  //luo
164 
165  double* kValueTable;
166 
167  svm_node* xSpace; // Needed when we load from data file.
168 
169  bool valid; /**< Set to false if model is InValid; example look at ReadXML */
170 
172 
173  SvmModel233 ();
174 
175  ~SvmModel233 ();
176 
177  SvmModel233 (const SvmModel233& _model)
178  {
179  throw KKException ("SvmModel233::SvmModel233 (const SvmModel233& _model) ***ERROR*** Not Supported");
180  }
181 
182 
184 
185 
187 
188 
189  virtual void ReadXML (XmlStream& s,
190  XmlTagConstPtr tag,
191  VolConstBool& cancelFlag,
192  RunLog& log
193  );
194 
195 
196  virtual void WriteXML (const KKStr& varName,
197  std::ostream& o
198  ) const;
199 
200 
201 private:
202  void Dispose ();
203 }; /* SvmModel233 */
204 
205 
208 
209 
210 
211 
212 struct SvmModel233* svm_train (const struct svm_problem* prob,
213  const struct svm_parameter* param
214  );
215 
216 
217 
218 struct SvmModel233* Svm_Load_Model (std::istream& f,
219  RunLog& log
220  );
221 
222 /*
223 kkint32 svm_save_model (const char* model_file_name,
224  const struct SvmModel233* model
225  );
226 */
227 
228 void Svm_Save_Model (std::ostream& o,
229  const SvmModel233* model
230  );
231 
232 
233 kkint32 svm_get_nr_class (const struct SvmModel233* model);
234 
235 
236 void svm_get_labels (const struct SvmModel233* model,
237  kkint32* label
238  );
239 
240 void svm_GetSupportVectorStatistics (const struct SvmModel233* model,
241  kkint32& numSVs, // The number of training examp
242  kkint32& totalNumSVs
243  );
244 
245 
246 double svm_predict (const struct SvmModel233* model,
247  const struct svm_node* x
248  );
249 
250 
251 
252 /**
253  *@brief Predicts a class for the specified training example.
254  *@param[in] model A previously trained model.
255  *@param[in] x Example that we want to make prediction on.
256  *@param[out] dist Entry for each class-pair indicating the distance from the decision boundary.
257  *@param[out] winners A list of one or more classes that won the highest number of votes; that is for each pair of classes
258  * there is a vote and it is possible for there t be a tie for winner.
259  *@param[in] excludeSupportVectorIDX Index of training example that should be excluded from computation; if less than zero will
260  * be ignored; this would be the same index specified when training the model to ignore.
261  *@returns The predicted class; the won that won the most amount of votes; if there is a tie the 1st one will be returned.
262  */
263 double svm_predict (const struct SvmModel233* model,
264  const svm_node* x,
265  std::vector<double>& dist,
268  );
269 
270 
271 /**
272  *@param[in] model A previously trained model.
273  *@param[in] x Example that we want to make prediction on.
274  *@param[out] dist Distance from decision boundary.
275  *@param[in] excludeSupportVectorIDX Index of support vector that should be excluded form computation; if less than zero will be ignored.
276  *@returns The predicted class; Zero(0) or One(1); if (dist less-or-equal 0) the class Zero otherwise class One.
277  */
278 double svm_predictTwoClasses (const SvmModel233* model,
279  const svm_node* x,
280  double& dist,
281  kkint32 excludeSupportVectorIDX
282  );
283 
285  kkint32 excludeSupportVectorIDX
286  );
287 
288 
289 void svm_destroy_model (struct SvmModel233 *model);
290 
291 const char* svm_check_parameter (const struct svm_problem *prob, const struct svm_parameter *param);
292 
293 
294 //luo add
295 void svm_margin (SvmModel233 *model); // compute the margin
296 //double svm_dist (const SvmModel233* model,
297 // const svm_node *x
298 // ); // compute the normalized distance
299 //luo
300 
301 
302 } /* SVM233 */
303 
304 
305 #endif /* _LIBSVM_H */
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
double ** sv_coef
Definition: svm.h:142
kkint32 dim
Definition: svm.h:161
kkint32 MemoryConsumedEstimated() const
Definition: svm.cpp:886
kkint32 KernalType() const
Definition: svm.h:118
double Gamma() const
Definition: svm.h:117
kkint32 dimSelect
Definition: svm.h:102
void ParseTabDelStr(const KKStr &_str)
Definition: svm.cpp:1176
__int32 kkint32
Definition: KKBaseTypes.h:88
void svm_GetSupportVectorStatistics(const struct SvmModel233 *model, kkint32 &numSVs, kkint32 &totalNumSVs)
Extract Support Vector statistics .
Definition: svm.cpp:4776
kkint32 boosting
Definition: svm.h:98
kkint32 svm_get_nr_class(const struct SvmModel233 *model)
XmlElementTemplate< SvmModel233 > XmlElementSvmModel233
Definition: svm.h:206
decision_function svm_train_one(const svm_problem *prob, const svm_parameter *param, double Cp, double Cn, std::set< kkint32 > &BSVIndex)
Definition: svm.cpp:3154
kkint32 svm_type
Definition: svm.h:81
double * margin
Definition: svm.h:158
kkint32 probability
Definition: svm.h:90
double * featureWeight
Definition: svm.h:103
kkint32 * nSV
Definition: svm.h:148
KKStr ToCmdLineStr() const
Definition: svm.cpp:1069
double weight
Definition: svm.h:159
const kkint32 SCALE
Definition: svm.h:27
double * featureWeight
Definition: svm.h:162
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
Definition: svm.cpp:144
bool weOwnContents
Definition: svm.h:55
kkint32 l
Definition: svm.h:139
kkint32 sampleSV
Definition: svm.h:108
kkint32 l
Definition: svm.h:48
KKStr SupportVectorName(kkint32 svIDX)
Definition: svm.cpp:134
struct SvmModel233 * svm_train(const struct svm_problem *prob, const struct svm_parameter *param)
kkint32 * SVIndex
Definition: svm.h:153
kkint32 nr_class
Definition: svm.h:138
double cache_size
Definition: svm.h:85
double svm_predictTwoClasses(const SvmModel233 *model, const svm_node *x, double &dist, kkint32 excludeSupportVectorIDX)
Definition: svm.cpp:4218
double * weight
Definition: svm.h:92
kkint32 * label
Definition: svm.h:147
double degree
Definition: svm.h:78
kkint32 * weight_label
Definition: svm.h:93
double value
Definition: svm.h:39
struct SvmModel233 * Svm_Load_Model(std::istream &f, RunLog &log)
kkint32 nr_class
Definition: svm.h:105
svm_parameter(KKStr &paramStr)
Definition: svm.cpp:983
kkint16 index
Definition: svm.h:38
kkint32 hist
Definition: svm.h:104
kkint32 shrinking
Definition: svm.h:91
struct svm_node ** x
Definition: svm.h:52
svm_node * xSpace
Definition: svm.h:167
KKStr ToTabDelStr() const
Definition: svm.cpp:1104
XmlTag const * XmlTagConstPtr
Definition: KKStr.h:45
double * W
Definition: svm.h:53
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
Definition: XmlStream.h:46
kkint32 * nonSVIndex
Definition: svm.h:154
void KernalType(kkint32 _kernalType)
Definition: svm.h:115
double * y
Definition: svm.h:49
bool weOwnXspace
Definition: svm.h:171
kkint32 nr_weight
Definition: svm.h:87
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
kkint32 MemoryConsumedEstimated() const
Definition: svm.cpp:70
svm_parameter param
Definition: svm.h:137
double svm_predict(const struct SvmModel233 *model, const struct svm_node *x)
kkint32 numSVM
Definition: svm.h:106
VectorKKStr exampleNames
Definition: svm.h:50
double confidence
Definition: svm.h:100
kkint32 * index
Definition: svm.h:51
std::set< kkint32 > BSVIndex
Definition: svm.h:157
VectorKKStr exampleNames
Definition: svm.h:140
void ProcessSvmParameter(KKStr cmd, KKStr value, double valueNum, bool &parmUsed)
Definition: svm.cpp:900
KKException(const char *_exceptionStr)
Definition: KKException.cpp:38
void svm_get_labels(const struct SvmModel233 *model, kkint32 *label)
svm_node ** SV
Definition: svm.h:141
kkint32 numNonSV
Definition: svm.h:155
XmlElementSvmModel233 * XmlElementSvmModel233Ptr
Definition: svm.h:207
Used for logging messages.
Definition: RunLog.h:49
void svm_margin(SvmModel233 *model)
Definition: svm.cpp:4650
void Svm_Save_Model(std::ostream &o, const SvmModel233 *model)
kkint32 kernel_type
Definition: svm.h:80
const char * svm_check_parameter(const struct svm_problem *prob, const struct svm_parameter *param)
double gamma
Definition: svm.h:79
svm_parameter & operator=(const svm_parameter &right)
Definition: svm.cpp:828
svm_parameter(const svm_parameter &parameters)
Definition: svm.cpp:768
double coef0
Definition: svm.h:77
double * kValueTable
Definition: svm.h:165
SvmModel233(const SvmModel233 &_model)
Definition: svm.h:177
void svm_destroy_model(struct SvmModel233 *model)
Definition: svm.cpp:4442
double * rho
Definition: svm.h:143
svm_problem * svm_BuildProbFromTwoClassModel(const SvmModel233 *model, kkint32 excludeSupportVectorIDX)
Definition: svm.cpp:4356
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
Definition: svm.cpp:234
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163
void Gamma(double _gamma)
Definition: svm.h:114