Point Cloud Library (PCL)  1.14.0-dev
svm.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  * Copyright (c) 2000-2012 Chih-Chung Chang and Chih-Jen Lin
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of copyright holders nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #pragma once
40 
41 #define LIBSVM_VERSION 311
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 extern int libsvm_version;
48 
49 struct svm_node {
50  int index;
51  double value;
52 };
53 
54 struct svm_problem {
55  int l;
56  double* y;
57 
58  struct svm_node** x;
59 };
60 
61 struct svm_scaling {
62  // index = 1 if usable, index = 0 if not
63 
64  struct svm_node* obj;
65 
66  // max features scaled
67  int max{0};
68 
69  svm_scaling() = default;
70 };
71 
72 enum { C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR }; /* svm_type */
73 enum { LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED }; /* kernel_type */
74 
75 struct svm_parameter {
76  int svm_type;
78  int degree; /* for poly */
79  double gamma; /* for poly/rbf/sigmoid */
80  double coef0; /* for poly/sigmoid */
81 
82  /* these are for training only */
83  double cache_size; /* in MB */
84  double eps; /* stopping criteria */
85  double C; /* for C_SVC, EPSILON_SVR and NU_SVR */
86  int nr_weight; /* for C_SVC */
87  int* weight_label; /* for C_SVC */
88  double* weight; /* for C_SVC */
89  double nu; /* for NU_SVC, ONE_CLASS, and NU_SVR */
90  double p; /* for EPSILON_SVR */
91  int shrinking; /* use the shrinking heuristics */
92  int probability; /* do probability estimates */
93 };
94 
95 //
96 // svm_model
97 //
98 
99 struct svm_model {
100 
101  struct svm_parameter param; /* parameter */
102  int nr_class; /* number of classes, = 2 in regression/one class svm */
103  int l; /* total #SV */
104 
105  struct svm_node** SV; /* SVs (SV[l]) */
106  double** sv_coef; /* coefficients for SVs in decision functions (sv_coef[k-1][l]) */
107  double* rho; /* constants in decision functions (rho[k*(k-1)/2]) */
108  double* probA; /* pariwise probability information */
109  double* probB;
110 
111  /* for classification only */
112 
113  int* label; /* label of each class (label[k]) */
114  int* nSV; /* number of SVs for each class (nSV[k]) */
115  /* nSV[0] + nSV[1] + ... + nSV[k-1] = l */
116  /* XXX */
117  int free_sv; /* 1 if svm_model is created by svm_load_model*/
118  /* 0 if svm_model is created by svm_train */
119 
120  /* for scaling */
121 
122  struct svm_node* scaling;
123 };
124 
125 struct svm_model*
126 svm_train(const struct svm_problem* prob, const struct svm_parameter* param);
127 void
128 svm_cross_validation(const struct svm_problem* prob,
129  const struct svm_parameter* param,
130  int nr_fold,
131  double* target);
132 
133 int
134 svm_save_model(const char* model_file_name, const struct svm_model* model);
135 
136 struct svm_model*
137 svm_load_model(const char* model_file_name);
138 
139 int
140 svm_get_svm_type(const struct svm_model* model);
141 
142 int
143 svm_get_nr_class(const struct svm_model* model);
144 
145 void
146 svm_get_labels(const struct svm_model* model, int* label);
147 
148 double
149 svm_get_svr_probability(const struct svm_model* model);
150 
151 double
152 svm_predict_values(const struct svm_model* model,
153  const struct svm_node* x,
154  double* dec_values);
155 double
156 svm_predict(const struct svm_model* model, const struct svm_node* x);
157 
158 double
159 svm_predict_probability(const struct svm_model* model,
160  const struct svm_node* x,
161  double* prob_estimates);
162 
163 void
164 svm_free_model_content(struct svm_model* model_ptr);
165 
166 void
167 svm_free_and_destroy_model(struct svm_model** model_ptr_ptr);
168 
169 void
170 svm_destroy_param(struct svm_parameter* param);
171 
172 const char*
173 svm_check_parameter(const struct svm_problem* prob, const struct svm_parameter* param);
174 
175 int
176 svm_check_probability_model(const struct svm_model* model);
177 
178 void
179 svm_set_print_string_function(void (*print_func)(const char*));
180 
181 #ifdef __cplusplus
182 }
183 
184 #endif
Definition: svm.h:99
double * rho
Definition: svm.h:107
int * nSV
Definition: svm.h:114
int free_sv
Definition: svm.h:117
int nr_class
Definition: svm.h:102
struct svm_node * scaling
Definition: svm.h:122
double * probB
Definition: svm.h:109
struct svm_parameter param
Definition: svm.h:101
struct svm_node ** SV
Definition: svm.h:105
double ** sv_coef
Definition: svm.h:106
int l
Definition: svm.h:103
int * label
Definition: svm.h:113
double * probA
Definition: svm.h:108
Definition: svm.h:49
double value
Definition: svm.h:51
int index
Definition: svm.h:50
double cache_size
Definition: svm.h:83
int * weight_label
Definition: svm.h:87
double eps
Definition: svm.h:84
double coef0
Definition: svm.h:80
int svm_type
Definition: svm.h:76
double p
Definition: svm.h:90
int kernel_type
Definition: svm.h:77
int nr_weight
Definition: svm.h:86
double nu
Definition: svm.h:89
double gamma
Definition: svm.h:79
double C
Definition: svm.h:85
int probability
Definition: svm.h:92
int shrinking
Definition: svm.h:91
int degree
Definition: svm.h:78
double * weight
Definition: svm.h:88
int l
Definition: svm.h:55
double * y
Definition: svm.h:56
struct svm_node ** x
Definition: svm.h:58
svm_scaling()=default
struct svm_node * obj
Definition: svm.h:64
int max
Definition: svm.h:67