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

Function main

examples/assignment_learning_ex.cpp:153–209  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

151// ----------------------------------------------------------------------------------------
152
153int main()
154{
155 try
156 {
157 // Get a small bit of training data.
158 std::vector<sample_type> samples;
159 std::vector<label_type> labels;
160 make_data(samples, labels);
161
162
163 structural_assignment_trainer<feature_extractor> trainer;
164 // This is the common SVM C parameter. Larger values encourage the
165 // trainer to attempt to fit the data exactly but might overfit.
166 // In general, you determine this parameter by cross-validation.
167 trainer.set_c(10);
168 // This trainer can use multiple CPU cores to speed up the training.
169 // So set this to the number of available CPU cores.
170 trainer.set_num_threads(4);
171
172 // Do the training and save the results in assigner.
173 assignment_function<feature_extractor> assigner = trainer.train(samples, labels);
174
175
176 // Test the assigner on our data. The output will indicate that it makes the
177 // correct associations on all samples.
178 cout << "Test the learned assignment function: " << endl;
179 for (unsigned long i = 0; i < samples.size(); ++i)
180 {
181 // Predict the assignments for the LHS and RHS in samples[i].
182 std::vector<long> predicted_assignments = assigner(samples[i]);
183 cout << "true labels: " << trans(mat(labels[i]));
184 cout << "predicted labels: " << trans(mat(predicted_assignments)) << endl;
185 }
186
187 // We can also use this tool to compute the percentage of assignments predicted correctly.
188 cout << "training accuracy: " << test_assignment_function(assigner, samples, labels) << endl;
189
190
191 // Since testing on your training data is a really bad idea, we can also do 5-fold cross validation.
192 // Happily, this also indicates that all associations were made correctly.
193 randomize_samples(samples, labels);
194 cout << "cv accuracy: " << cross_validate_assignment_trainer(trainer, samples, labels, 5) << endl;
195
196
197
198 // Finally, the assigner can be serialized to disk just like most dlib objects.
199 serialize("assigner.dat") << assigner;
200
201 // recall from disk
202 deserialize("assigner.dat") >> assigner;
203 }
204 catch (std::exception& e)
205 {
206 cout << "EXCEPTION THROWN" << endl;
207 cout << e.what() << endl;
208 }
209}
210

Callers

nothing calls this directly

Calls 13

test_assignment_functionFunction · 0.85
randomize_samplesFunction · 0.85
make_dataFunction · 0.70
serializeFunction · 0.70
deserializeFunction · 0.70
transFunction · 0.50
matFunction · 0.50
set_cMethod · 0.45
set_num_threadsMethod · 0.45
trainMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected