| 151 | // ---------------------------------------------------------------------------------------- |
| 152 | |
| 153 | int 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 |
nothing calls this directly
no test coverage detected