()
| 32 | |
| 33 | |
| 34 | def main(): |
| 35 | # In this example, we have three types of samples: class 0, 1, or 2. That |
| 36 | # is, each of our sample vectors falls into one of three classes. To keep |
| 37 | # this example very simple, each sample vector is zero everywhere except at |
| 38 | # one place. The non-zero dimension of each vector determines the class of |
| 39 | # the vector. So for example, the first element of samples has a class of 1 |
| 40 | # because samples[0][1] is the only non-zero element of samples[0]. |
| 41 | samples = [[0, 2, 0], [1, 0, 0], [0, 4, 0], [0, 0, 3]] |
| 42 | # Since we want to use a machine learning method to learn a 3-class |
| 43 | # classifier we need to record the labels of our samples. Here samples[i] |
| 44 | # has a class label of labels[i]. |
| 45 | labels = [1, 0, 1, 2] |
| 46 | |
| 47 | # Now that we have some training data we can tell the structural SVM to |
| 48 | # learn the parameters of our 3-class classifier model. The details of this |
| 49 | # will be explained later. For now, just note that it finds the weights |
| 50 | # (i.e. a vector of real valued parameters) such that predict_label(weights, |
| 51 | # sample) always returns the correct label for a sample vector. |
| 52 | problem = ThreeClassClassifierProblem(samples, labels) |
| 53 | weights = dlib.solve_structural_svm_problem(problem) |
| 54 | |
| 55 | # Print the weights and then evaluate predict_label() on each of our |
| 56 | # training samples. Note that the correct label is predicted for each |
| 57 | # sample. |
| 58 | print(weights) |
| 59 | for k, s in enumerate(samples): |
| 60 | print("Predicted label for sample[{0}]: {1}".format( |
| 61 | k, predict_label(weights, s))) |
| 62 | |
| 63 | |
| 64 | def predict_label(weights, sample): |
no test coverage detected