separation_oracle() is more interesting. dlib's structural SVM solver will call separation_oracle() many times during the optimization. Each time it will give it the current value of the parameter weights and separation_oracle() is supposed to find the label that most violates the structural SVM objective function for the idx-th sample. Then the separation oracle reports the corresponding PSI v
| 306 | // - #loss == LOSS(idx,Y) |
| 307 | // - #psi == PSI(X,Y) |
| 308 | virtual void separation_oracle ( |
| 309 | const long idx, |
| 310 | const column_vector& current_solution, |
| 311 | scalar_type& loss, |
| 312 | feature_vector_type& psi |
| 313 | ) const |
| 314 | { |
| 315 | // Note that the solver will use multiple threads to make concurrent calls to |
| 316 | // separation_oracle(), therefore, you must implement it in a thread safe manner |
| 317 | // (or disable threading by inheriting from structural_svm_problem instead of |
| 318 | // structural_svm_problem_threaded). However, if your separation oracle is not |
| 319 | // very fast to execute you can get a very significant speed boost by using the |
| 320 | // threaded solver. In general, all you need to do to make your separation oracle |
| 321 | // thread safe is to make sure it does not modify any global variables or members |
| 322 | // of three_class_classifier_problem. So it is usually easy to make thread safe. |
| 323 | |
| 324 | column_vector scores(3); |
| 325 | |
| 326 | // compute scores for each of the three classifiers |
| 327 | scores = dot(rowm(current_solution, range(0,2)), samples[idx]), |
| 328 | dot(rowm(current_solution, range(3,5)), samples[idx]), |
| 329 | dot(rowm(current_solution, range(6,8)), samples[idx]); |
| 330 | |
| 331 | // Add in the loss-augmentation. Recall that we maximize LOSS(idx,y) + F(X,y) in |
| 332 | // the separate oracle, not just F(X,y) as we normally would in predict_label(). |
| 333 | // Therefore, we must add in this extra amount to account for the loss-augmentation. |
| 334 | // For our simple multi-class classifier, we incur a loss of 1 if we don't predict |
| 335 | // the correct label and a loss of 0 if we get the right label. |
| 336 | if (labels[idx] != 0) |
| 337 | scores(0) += 1; |
| 338 | if (labels[idx] != 1) |
| 339 | scores(1) += 1; |
| 340 | if (labels[idx] != 2) |
| 341 | scores(2) += 1; |
| 342 | |
| 343 | // Now figure out which classifier has the largest loss-augmented score. |
| 344 | const int max_scoring_label = index_of_max(scores); |
| 345 | // And finally record the loss that was associated with that predicted label. |
| 346 | // Again, the loss is 1 if the label is incorrect and 0 otherwise. |
| 347 | if (max_scoring_label == labels[idx]) |
| 348 | loss = 0; |
| 349 | else |
| 350 | loss = 1; |
| 351 | |
| 352 | // Finally, compute the PSI vector corresponding to the label we just found and |
| 353 | // store it into psi for output. |
| 354 | psi = make_psi(samples[idx], max_scoring_label); |
| 355 | } |
| 356 | |
| 357 | private: |
| 358 |
nothing calls this directly
no test coverage detected