MCPcopy Create free account
hub / github.com/davisking/dlib / main

Function main

examples/custom_trainer_ex.cpp:147–226  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

145// ----------------------------------------------------------------------------------------
146
147int main()
148{
149 std::vector<sample_type> samples;
150 std::vector<string> labels;
151
152 // First, get our labeled set of training data
153 generate_data(samples, labels);
154
155 cout << "samples.size(): "<< samples.size() << endl;
156
157 // Define the trainer we will use. The second template argument specifies the type
158 // of label used, which is string in this case.
159 typedef one_vs_one_trainer<any_trainer<sample_type>, string> ovo_trainer;
160
161
162 ovo_trainer trainer;
163
164 // Now tell the one_vs_one_trainer that, by default, it should use the simple_custom_trainer
165 // to solve the individual binary classification subproblems.
166 trainer.set_trainer(simple_custom_trainer());
167
168 // Next, to make things a little more interesting, we will setup the one_vs_one_trainer
169 // to use kernel ridge regression to solve the upper_left vs lower_right binary classification
170 // subproblem.
171 typedef radial_basis_kernel<sample_type> rbf_kernel;
172 krr_trainer<rbf_kernel> rbf_trainer;
173 rbf_trainer.set_kernel(rbf_kernel(0.1));
174 trainer.set_trainer(rbf_trainer, "upper_left", "lower_right");
175
176
177 // Now let's do 5-fold cross-validation using the one_vs_one_trainer we just setup.
178 // As an aside, always shuffle the order of the samples before doing cross validation.
179 // For a discussion of why this is a good idea see the svm_ex.cpp example.
180 randomize_samples(samples, labels);
181 cout << "cross validation: \n" << cross_validate_multiclass_trainer(trainer, samples, labels, 5) << endl;
182 // This dataset is very easy and everything is correctly classified. Therefore, the output of
183 // cross validation is the following confusion matrix.
184 /*
185 50 0 0 0
186 0 50 0 0
187 0 0 50 0
188 0 0 0 50
189 */
190
191
192 // We can also obtain the decision rule as always.
193 one_vs_one_decision_function<ovo_trainer> df = trainer.train(samples, labels);
194
195 cout << "predicted label: "<< df(samples[0]) << ", true label: "<< labels[0] << endl;
196 cout << "predicted label: "<< df(samples[90]) << ", true label: "<< labels[90] << endl;
197 // The output is:
198 /*
199 predicted label: upper_right, true label: upper_right
200 predicted label: lower_left, true label: lower_left
201 */
202
203
204 // Finally, let's save our multiclass decision rule to disk. Remember that we have

Callers

nothing calls this directly

Calls 11

randomize_samplesFunction · 0.85
generate_dataFunction · 0.70
serializeFunction · 0.70
deserializeFunction · 0.70
sizeMethod · 0.45
set_trainerMethod · 0.45
set_kernelMethod · 0.45
trainMethod · 0.45

Tested by

no test coverage detected