-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdlib_trainers.h
368 lines (293 loc) · 12.9 KB
/
dlib_trainers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Author: Sebastian Boettcher
*
* Conveniance wrappers and tools for dlib trainers.
*
*/
#ifndef _DLIB_TRAINERS_H_
#define _DLIB_TRAINERS_H_
#include <dlib/svm.h>
#include <dlib/svm_threaded.h>
#include <map>
#include "enum.h"
using namespace std;
using namespace dlib;
/*
######## ## ## ## ## ## ## ######
## ### ## ## ## ### ### ## ##
## #### ## ## ## #### #### ##
###### ## ## ## ## ## ## ### ## ######
## ## #### ## ## ## ## ##
## ## ### ## ## ## ## ## ##
######## ## ## ####### ## ## ######
*/
BETTER_ENUM(TrainerType, int,
TEMPLATE,
BINARY,
MULTICLASS,
REGRESSION
)
BETTER_ENUM(TrainerName, int,
TEMPLATE,
// multiclass
ONE_VS_ONE,
ONE_VS_ALL,
SVM_MULTICLASS_LINEAR,
// binary
RVM,
SVM_C,
SVM_C_LINEAR,
SVM_C_LINEAR_DCD,
SVM_C_EKM,
SVM_NU,
// regression
KRR,
RBF_NETWORK,
RR,
RVM_REG,
SVR,
SVR_LINEAR
)
multimap<TrainerType, TrainerName> type_name_map = {
{TrainerType::MULTICLASS, TrainerName::ONE_VS_ONE},
{TrainerType::MULTICLASS, TrainerName::ONE_VS_ALL},
{TrainerType::MULTICLASS, TrainerName::SVM_MULTICLASS_LINEAR},
{TrainerType::BINARY, TrainerName::RVM},
{TrainerType::BINARY, TrainerName::SVM_C},
{TrainerType::BINARY, TrainerName::SVM_C_LINEAR},
{TrainerType::BINARY, TrainerName::SVM_C_LINEAR_DCD},
{TrainerType::BINARY, TrainerName::SVM_C_EKM},
{TrainerType::BINARY, TrainerName::SVM_NU},
{TrainerType::REGRESSION, TrainerName::KRR},
{TrainerType::REGRESSION, TrainerName::RBF_NETWORK},
{TrainerType::REGRESSION, TrainerName::RR},
{TrainerType::REGRESSION, TrainerName::RVM_REG},
{TrainerType::REGRESSION, TrainerName::SVR},
{TrainerType::REGRESSION, TrainerName::SVR_LINEAR}
};
void printTrainers() {
for (TrainerType t_type : TrainerType::_values()) {
if (type_name_map.count(t_type))
cout << t_type << endl;
for (auto const & kv : type_name_map)
if (kv.first == t_type)
cout << "\t" << kv.second << endl;
}
}
bool operator==(const std::string& name_str, const TrainerName& name) {
if (!TrainerName::_is_valid_nocase(name_str.c_str()))
return false;
return TrainerName::_from_string_nocase(name_str.c_str()) == name;
}
bool classifierExists(string name) {
for (TrainerName tname : TrainerName::_values())
if (name == tname)
return true;
return false;
}
bool classifierIsType(string name, TrainerType type) {
if (classifierExists(name))
for (auto it = type_name_map.equal_range(type).first; it != type_name_map.equal_range(type).second; ++it)
if (name == it->second)
return true;
return false;
}
std::vector<string> classifierGetType(TrainerType type) {
std::vector<string> ret;
for (auto it = type_name_map.equal_range(type).first; it != type_name_map.equal_range(type).second; ++it)
ret.push_back(it->second._to_string());
return ret;
}
/*
######## ## ## ######## ######## ######## ######## ######## ######
## ## ## ## ## ## ## ## ## ## ## ##
## #### ## ## ## ## ## ## ## ##
## ## ######## ###### ## ## ###### ###### ######
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ##
## ## ## ######## ######## ######## ## ######
*/
// typedef for one sample, init as 0,1 ; can be cast to arbitrary num_rows
typedef matrix<double, 0, 1> sample_type;
typedef string label_type;
typedef std::vector<sample_type> v_sample_type;
typedef std::vector<label_type> v_label_type;
// any typedefs
typedef any_trainer<sample_type, label_type> a_tr;
typedef any_decision_function<sample_type, label_type> a_df;
// kernel typedefs
typedef histogram_intersection_kernel<sample_type> hist_kernel;
typedef linear_kernel<sample_type> lin_kernel;
typedef radial_basis_kernel<sample_type> rbf_kernel;
typedef polynomial_kernel<sample_type> poly_kernel;
typedef sigmoid_kernel<sample_type> sig_kernel;
#define KERNEL_TYPE "list", "hist", "lin", "rbf", "poly", "sig"
// individual trainer typedefs
// one vs one trainer typedefs
typedef one_vs_one_trainer<any_trainer<sample_type>, label_type> ovo_trainer_type;
typedef one_vs_one_decision_function<ovo_trainer_type> ovo_trained_function_type;
typedef one_vs_one_decision_function<ovo_trainer_type, decision_function<offset_kernel<hist_kernel>>> ovo_trained_function_type_hist_df;
typedef one_vs_one_decision_function<ovo_trainer_type, decision_function<offset_kernel<lin_kernel>>> ovo_trained_function_type_lin_df;
typedef one_vs_one_decision_function<ovo_trainer_type, decision_function<lin_kernel>> ovo_trained_function_type_lin_no_df;
typedef one_vs_one_decision_function<ovo_trainer_type, decision_function<offset_kernel<rbf_kernel>>> ovo_trained_function_type_rbf_df;
typedef one_vs_one_decision_function<ovo_trainer_type, decision_function<offset_kernel<poly_kernel>>> ovo_trained_function_type_poly_df;
typedef one_vs_one_decision_function<ovo_trainer_type, decision_function<offset_kernel<sig_kernel>>> ovo_trained_function_type_sig_df;
// one vs all trainer typedefs
typedef one_vs_all_trainer<any_trainer<sample_type>, label_type> ova_trainer_type;
typedef one_vs_all_decision_function<ova_trainer_type> ova_trained_function_type;
typedef one_vs_all_decision_function<ova_trainer_type, decision_function<offset_kernel<hist_kernel>>> ova_trained_function_type_hist_df;
typedef one_vs_all_decision_function<ova_trainer_type, decision_function<offset_kernel<lin_kernel>>> ova_trained_function_type_lin_df;
typedef one_vs_all_decision_function<ova_trainer_type, decision_function<lin_kernel>> ova_trained_function_type_lin_no_df;
typedef one_vs_all_decision_function<ova_trainer_type, decision_function<offset_kernel<rbf_kernel>>> ova_trained_function_type_rbf_df;
typedef one_vs_all_decision_function<ova_trainer_type, decision_function<offset_kernel<poly_kernel>>> ova_trained_function_type_poly_df;
typedef one_vs_all_decision_function<ova_trainer_type, decision_function<offset_kernel<sig_kernel>>> ova_trained_function_type_sig_df;
// svm multiclass linear trainer typedefs
typedef svm_multiclass_linear_trainer<lin_kernel, label_type> svm_ml_trainer_type;
typedef multiclass_linear_decision_function<lin_kernel, label_type> svm_ml_trained_function_type;
/*
######## ######## ## ## ######## ## ### ######## ########
## ## ### ### ## ## ## ## ## ## ##
## ## #### #### ## ## ## ## ## ## ##
## ###### ## ### ## ######## ## ## ## ## ######
## ## ## ## ## ## ######### ## ##
## ## ## ## ## ## ## ## ## ##
## ######## ## ## ## ######## ## ## ## ########
*/
//_______________________________________________________________________________________________________
class trainer_template {
public:
trainer_template() {}
TrainerType getTrainerType() { return m_trainer_type; }
TrainerName getTrainerName() { return m_trainer_name; }
void setVerbosity(bool verbose) { m_verbose = verbose; }
string getKernel() { return m_kernel; }
a_tr getTrainer() {
if (m_trainer.is_empty()) {
cerr << "Trainer not set!" << endl;
exit(-1);
}
return m_trainer;
}
a_df train(const v_sample_type& all_samples, const v_label_type& all_labels) const {
if (m_trainer.is_empty()) {
cerr << "Trainer not set!" << endl;
exit(-1);
}
return m_trainer.train(all_samples, all_labels);
}
virtual matrix<double> crossValidation(const v_sample_type& samples, const v_label_type& labels, const long folds) = 0;
protected:
void setTrainerType(TrainerType type) { m_trainer_type = type; }
void setTrainerName(TrainerName name) { m_trainer_name = name; }
a_tr m_trainer;
bool m_verbose = false;
string m_kernel = "n/a";
private:
TrainerType m_trainer_type = TrainerType::TEMPLATE;
TrainerName m_trainer_name = TrainerName::TEMPLATE;
};
/*
* ### TRAINER DECLARATIONS ###
*/
/*
####### ## ## ######## ## ## ###### ####### ## ## ########
## ## ### ## ## ## ## ## ## ## ## ### ## ##
## ## #### ## ## ## ## ## ## ## #### ## ##
## ## ## ## ## ###### ## ## ###### ## ## ## ## ## ######
## ## ## #### ## ## ## ## ## ## ## #### ##
## ## ## ### ## ## ## ## ## ## ## ## ### ##
####### ## ## ######## ### ###### ####### ## ## ########
*/
//_______________________________________________________________________________________________________
class ovo_trainer : public trainer_template {
public:
typedef ovo_trainer_type T;
ovo_trainer(bool verbose = false, int num_threads = 4, string kernel = "", any_trainer<sample_type> bin_tr = krr_trainer<rbf_kernel>()) {
setTrainerType(TrainerType::MULTICLASS);
setTrainerName(TrainerName::ONE_VS_ONE);
m_verbose = verbose;
m_kernel = kernel;
m_trainer.clear();
m_trainer.get<T>();
m_trainer.cast_to<T>().set_trainer(bin_tr);
m_trainer.cast_to<T>().set_num_threads(num_threads);
if (m_verbose)
m_trainer.cast_to<T>().be_verbose();
}
matrix<double> crossValidation(const v_sample_type& samples, const v_label_type& labels, const long folds = 5) {
if (m_trainer.is_empty()) {
cerr << "Trainer not set!" << endl;
exit(-1);
}
return cross_validate_multiclass_trainer(m_trainer.cast_to<T>(), samples, labels, folds);
}
};
/*
####### ## ## ######## ## ## ###### ### ## ##
## ## ### ## ## ## ## ## ## ## ## ## ##
## ## #### ## ## ## ## ## ## ## ## ##
## ## ## ## ## ###### ## ## ###### ## ## ## ##
## ## ## #### ## ## ## ## ######### ## ##
## ## ## ### ## ## ## ## ## ## ## ## ##
####### ## ## ######## ### ###### ## ## ######## ########
*/
//_______________________________________________________________________________________________________
class ova_trainer : public trainer_template {
public:
typedef ova_trainer_type T;
ova_trainer(bool verbose = false, int num_threads = 4, string kernel = "", any_trainer<sample_type> bin_tr = krr_trainer<rbf_kernel>()) {
setTrainerType(TrainerType::MULTICLASS);
setTrainerName(TrainerName::ONE_VS_ALL);
m_verbose = verbose;
m_kernel = kernel;
m_trainer.clear();
m_trainer.get<T>();
m_trainer.cast_to<T>().set_trainer(bin_tr);
m_trainer.cast_to<T>().set_num_threads(num_threads);
if (m_verbose)
m_trainer.cast_to<T>().be_verbose();
}
matrix<double> crossValidation(const v_sample_type& samples, const v_label_type& labels, const long folds = 5) {
if (m_trainer.is_empty()) {
cerr << "Trainer not set!" << endl;
exit(-1);
}
return cross_validate_multiclass_trainer(m_trainer.cast_to<T>(), samples, labels, folds);
}
};
/*
###### ## ## ## ## ## #### ## ## ######## ### ########
## ## ## ## ### ### ## ## ### ## ## ## ## ## ##
## ## ## #### #### ## ## #### ## ## ## ## ## ##
###### ## ## ## ### ## ## ## ## ## ## ###### ## ## ########
## ## ## ## ## ## ## ## #### ## ######### ## ##
## ## ## ## ## ## ## ## ## ### ## ## ## ## ##
###### ### ## ## ######## #### ## ## ######## ## ## ## ##
*/
//_______________________________________________________________________________________________________
class svm_ml_trainer : public trainer_template {
public:
typedef svm_ml_trainer_type T;
svm_ml_trainer(bool verbose = false, int num_threads = 4, bool nonneg = false, double epsilon = 0.001, int iterations = 10000, double regularization = 1) {
setTrainerType(TrainerType::MULTICLASS);
setTrainerName(TrainerName::SVM_MULTICLASS_LINEAR);
m_verbose = verbose;
m_trainer.clear();
m_trainer.get<T>();
m_trainer.cast_to<T>().set_learns_nonnegative_weights(nonneg);
m_trainer.cast_to<T>().set_epsilon(epsilon);
m_trainer.cast_to<T>().set_max_iterations(iterations);
m_trainer.cast_to<T>().set_c(regularization);
m_trainer.cast_to<T>().set_num_threads(num_threads);
if (m_verbose)
m_trainer.cast_to<T>().be_verbose();
}
matrix<double> crossValidation(const v_sample_type& samples, const v_label_type& labels, const long folds = 5) {
if (m_trainer.is_empty()) {
cerr << "Trainer not set!" << endl;
exit(-1);
}
return cross_validate_multiclass_trainer(m_trainer.cast_to<T>(), samples, labels, folds);
}
};
#endif // _DLIB_TRAINERS_H_