libsvm_classifier.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2016, Open Source Robotics Foundation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Open Source Robotics Foundation nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
38 #ifndef FCL_TEST_LIBSVM_CLASSIFIER_H
39 #define FCL_TEST_LIBSVM_CLASSIFIER_H
40 
41 #include "fcl/learning/classifier.h"
42 #include <libsvm/svm.h>
43 
44 namespace fcl
45 {
46 
47 
48 template<std::size_t N>
49 class LibSVMClassifier : public SVMClassifier<N>
50 {
51 public:
53  {
56  param.degree = 3;
57  param.gamma = 0; // 1/num_features
58  param.coef0 = 0;
59  param.nu = 0.5;
60  param.cache_size = 100; // can change
61  param.C = 1;
62  param.eps = 1e-3;
63  param.p = 0.1;
64  param.shrinking = 1; // use shrinking
65  param.probability = 0;
66  param.nr_weight = 0;
67  param.weight_label = NULL;
68  param.weight = NULL;
69 
70  param.nr_weight = 2;
71  param.weight_label = (int *)realloc(param.weight_label, sizeof(int) * param.nr_weight);
72  param.weight = (double *)realloc(param.weight, sizeof(double) * param.nr_weight);
73  param.weight_label[0] = -1;
74  param.weight_label[1] = 1;
75  param.weight[0] = 1;
76  param.weight[1] = 1;
77 
78  model = NULL;
79  x_space = NULL;
80  problem.x = NULL;
81  problem.y = NULL;
82  problem.W = NULL;
83  }
84 
85 
86  void setCSVM() { param.svm_type = C_SVC; }
87  void setNuSVM() { param.svm_type = NU_SVC; }
88  void setC(FCL_REAL C) { param.C = C; }
89  void setNu(FCL_REAL nu) { param.nu = nu; }
92  void setProbability(bool use_probability) { param.probability = use_probability; }
93  virtual void setScaler(const Scaler<N>& scaler_)
94  {
95  scaler = scaler_;
96  }
97 
99  {
100  param.weight[0] = c;
101  }
102 
104  {
105  param.weight[1] = c;
106  }
107 
108  void setEPS(FCL_REAL e)
109  {
110  param.eps = e;
111  }
112 
113  void setGamma(FCL_REAL gamma)
114  {
115  param.gamma = gamma;
116  }
117 
119  {
122  delete [] x_space;
123  delete [] problem.x;
124  delete [] problem.y;
125  delete [] problem.W;
126  }
127 
128  virtual void learn(const std::vector<Item<N> >& data)
129  {
130  if(data.size() == 0) return;
131 
133  if(param.gamma == 0) param.gamma = 1.0 / N;
134 
135  problem.l = data.size();
136  if(problem.y) delete [] problem.y;
137  problem.y = new double [problem.l];
138  if(problem.x) delete [] problem.x;
139  problem.x = new svm_node* [problem.l];
140  if(problem.W) delete [] problem.W;
141  problem.W = new double [problem.l];
142  if(x_space) delete [] x_space;
143  x_space = new svm_node [(N + 1) * problem.l];
144 
145  for(std::size_t i = 0; i < data.size(); ++i)
146  {
147  svm_node* cur_x_space = x_space + (N + 1) * i;
148  Vecnf<N> q_scaled = scaler.scale(data[i].q);
149  for(std::size_t j = 0; j < N; ++j)
150  {
151  cur_x_space[j].index = j + 1;
152  cur_x_space[j].value = q_scaled[j];
153  }
154  cur_x_space[N].index = -1;
155 
156  problem.x[i] = cur_x_space;
157  problem.y[i] = (data[i].label ? 1 : -1);
158  problem.W[i] = data[i].w;
159  }
160 
163  }
164 
165  virtual std::vector<PredictResult> predict(const std::vector<Vecnf<N> >& qs) const
166  {
167  std::vector<PredictResult> predict_results;
168 
169  int nr_class = svm_get_nr_class(model);
170  double* prob_estimates = NULL;
171 
172  svm_node* x = (svm_node*)malloc((N + 1) * sizeof(svm_node));
173  if(param.probability)
174  prob_estimates = (double*)malloc(nr_class * sizeof(double));
175 
176  Vecnf<N> v;
177  for(std::size_t i = 0; i < qs.size(); ++i)
178  {
179  v = scaler.scale(qs[i]);
180  for(std::size_t j = 0; j < N; ++j)
181  {
182  x[j].index = j + 1;
183  x[j].value = v[j];
184  }
185  x[N].index = -1;
186 
187  double predict_label;
188 
189  if(param.probability)
190  {
191  predict_label = svm_predict_probability(model, x, prob_estimates);
192  predict_label = (predict_label > 0) ? 1 : 0;
193  predict_results.push_back(PredictResult(predict_label, *prob_estimates));
194  }
195  else
196  {
197  predict_label = svm_predict(model, x);
198  predict_label = (predict_label > 0) ? 1 : 0;
199  predict_results.push_back(PredictResult(predict_label));
200  }
201  }
202 
203  if(param.probability) free(prob_estimates);
204  free(x);
205 
206  return predict_results;
207  }
208 
209  virtual PredictResult predict(const Vecnf<N>& q) const
210  {
211  return (predict(std::vector<Vecnf<N> >(1, q)))[0];
212  }
213 
214  void save(const std::string& filename) const
215  {
216  if(model)
217  svm_save_model(filename.c_str(), model);
218  }
219 
220  virtual std::vector<Item<N> > getSupportVectors() const
221  {
222  std::vector<Item<N> > results;
223  Item<N> item;
224  for(std::size_t i = 0; i < (std::size_t)model->l; ++i)
225  {
226  for(std::size_t j = 0; j < N; ++j)
227  item.q[j] = model->SV[i][j].value;
228  item.q = scaler.unscale(item.q);
229  int id = model->sv_indices[i] - 1;
230  item.label = (problem.y[id] > 0);
231  results.push_back(item);
232  }
233 
234  return results;
235  }
236 
242 
243  Scaler<N> scaler;
244 };
245 
246 
247 }
248 
249 #endif
fcl::LibSVMClassifier::predict
virtual std::vector< PredictResult > predict(const std::vector< Vecnf< N > > &qs) const
Definition: libsvm_classifier.h:165
svm_problem::y
double * y
Definition: svm.h:56
fcl::LibSVMClassifier::setNegativeWeight
void setNegativeWeight(FCL_REAL c)
Definition: libsvm_classifier.h:98
fcl::FCL_REAL
FCL_DEPRECATED double FCL_REAL
Definition: types.h:53
fcl::LibSVMClassifier::learn
virtual void learn(const std::vector< Item< N > > &data)
Definition: libsvm_classifier.h:128
svm_parameter::weight_label
int * weight_label
Definition: svm.h:77
svm_parameter::kernel_type
int kernel_type
Definition: svm.h:67
fcl::LibSVMClassifier::getSupportVectors
virtual std::vector< Item< N > > getSupportVectors() const
Definition: libsvm_classifier.h:220
svm_parameter::eps
double eps
Definition: svm.h:74
svm_model::sv_indices
int * sv_indices
Definition: svm.h:98
svm_model::l
int l
Definition: svm.h:92
RBF
@ RBF
Definition: svm.h:62
fcl::LibSVMClassifier::setCSVM
void setCSVM()
Definition: libsvm_classifier.h:86
svm_node::index
int index
Definition: svm.h:49
fcl::LibSVMClassifier::setEPS
void setEPS(FCL_REAL e)
Definition: libsvm_classifier.h:108
fcl::LibSVMClassifier::predict
virtual PredictResult predict(const Vecnf< N > &q) const
Definition: libsvm_classifier.h:209
fcl::LibSVMClassifier::setNuSVM
void setNuSVM()
Definition: libsvm_classifier.h:87
svm_problem::l
int l
Definition: svm.h:55
svm_parameter::C
double C
Definition: svm.h:75
NU_SVC
@ NU_SVC
Definition: svm.h:61
svm_get_nr_class
int svm_get_nr_class(const svm_model *model)
Definition: svm.cpp:2586
svm_node
Definition: svm.h:47
fcl::LibSVMClassifier::setLinearClassifier
void setLinearClassifier()
Definition: libsvm_classifier.h:90
svm_parameter::cache_size
double cache_size
Definition: svm.h:73
svm_model::SV
struct svm_node ** SV
Definition: svm.h:93
svm_parameter::coef0
double coef0
Definition: svm.h:70
svm_problem
Definition: svm.h:53
svm_predict
double svm_predict(const svm_model *model, const svm_node *x)
Definition: svm.cpp:2744
svm.h
svm_parameter::probability
int probability
Definition: svm.h:82
svm_free_and_destroy_model
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
Definition: svm.cpp:3170
svm_parameter
Definition: svm.h:64
svm_parameter::gamma
double gamma
Definition: svm.h:69
fcl::LibSVMClassifier::hyperw_normsqr
double hyperw_normsqr
Definition: libsvm_classifier.h:241
svm_parameter::nr_weight
int nr_weight
Definition: svm.h:76
fcl::LibSVMClassifier::setGamma
void setGamma(FCL_REAL gamma)
Definition: libsvm_classifier.h:113
svm_train
svm_model * svm_train(const svm_problem *prob, const svm_parameter *param)
Definition: svm.cpp:2196
fcl::LibSVMClassifier::setC
void setC(FCL_REAL C)
Definition: libsvm_classifier.h:88
svm_parameter::nu
double nu
Definition: svm.h:79
fcl::LibSVMClassifier
Definition: libsvm_classifier.h:49
fcl::LibSVMClassifier::LibSVMClassifier
LibSVMClassifier()
Definition: libsvm_classifier.h:52
fcl::LibSVMClassifier::setScaler
virtual void setScaler(const Scaler< N > &scaler_)
Definition: libsvm_classifier.h:93
svm_save_model
int svm_save_model(const char *model_file_name, const svm_model *model)
Definition: svm.cpp:2808
fcl::LibSVMClassifier::setProbability
void setProbability(bool use_probability)
Definition: libsvm_classifier.h:92
fcl::LibSVMClassifier::model
svm_model * model
Definition: libsvm_classifier.h:240
svm_parameter::degree
int degree
Definition: svm.h:68
LINEAR
@ LINEAR
Definition: svm.h:62
svm_problem::W
double * W
Definition: svm.h:58
svm_problem::x
struct svm_node ** x
Definition: svm.h:57
svm_hyper_w_normsqr_twoclass
double svm_hyper_w_normsqr_twoclass(const struct svm_model *model)
Definition: svm.cpp:2622
C_SVC
@ C_SVC
Definition: svm.h:61
svm_parameter::svm_type
int svm_type
Definition: svm.h:66
svm_node::value
double value
Definition: svm.h:50
svm_parameter::weight
double * weight
Definition: svm.h:78
svm_predict_probability
double svm_predict_probability(const svm_model *model, const svm_node *x, double *prob_estimates)
Definition: svm.cpp:2759
svm_parameter::p
double p
Definition: svm.h:80
fcl::LibSVMClassifier::~LibSVMClassifier
~LibSVMClassifier()
Definition: libsvm_classifier.h:118
fcl::LibSVMClassifier::scaler
Scaler< N > scaler
Definition: libsvm_classifier.h:243
svm_model
Definition: svm.h:88
svm_parameter::shrinking
int shrinking
Definition: svm.h:81
fcl::LibSVMClassifier::x_space
svm_node * x_space
Definition: libsvm_classifier.h:239
fcl::LibSVMClassifier::param
svm_parameter param
Definition: libsvm_classifier.h:237
fcl::LibSVMClassifier::setPositiveWeight
void setPositiveWeight(FCL_REAL c)
Definition: libsvm_classifier.h:103
fcl::LibSVMClassifier::setNonLinearClassifier
void setNonLinearClassifier()
Definition: libsvm_classifier.h:91
svm_destroy_param
void svm_destroy_param(svm_parameter *param)
Definition: svm.cpp:3180
fcl::LibSVMClassifier::save
void save(const std::string &filename) const
Definition: libsvm_classifier.h:214
fcl::LibSVMClassifier::setNu
void setNu(FCL_REAL nu)
Definition: libsvm_classifier.h:89
fcl
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
fcl::LibSVMClassifier::problem
svm_problem problem
Definition: libsvm_classifier.h:238


fcl
Author(s):
autogenerated on Tue Dec 5 2023 03:40:48