Turi Create  4.0
od_evaluation.hpp
1 /* Copyright © 2018 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 
7 #ifndef TURI_OBJECT_DETECTION_OD_EVALUATION_H_
8 #define TURI_OBJECT_DETECTION_OD_EVALUATION_H_
9 
10 #include <vector>
11 
12 #include <model_server/lib/variant.hpp>
13 #include <ml/neural_net/image_augmentation.hpp>
14 
15 namespace turi {
16 namespace object_detection {
17 
18 /**
19  * Performs class-independent non-maximum suppression on the given predictions.
20  *
21  * \param predictions A collection of possibly overlapping predictions
22  * \param iou_threshold The maximum allowed overlap (computed as the ratio
23  * between the intersection area and the union area) between any two
24  * predictions for the same class
25  * \return A subset of the given predictions, removing overlapping results,
26  * greedily preferring those with the highest confidence.
27  */
28 std::vector<neural_net::image_annotation> apply_non_maximum_suppression(
29  std::vector<neural_net::image_annotation> predictions, float iou_threshold);
30 
31 /**
32  * Helper class for computing AP and mAP metrics.
33  */
35 public:
36 
37  /**
38  * \param class_labels Each prediction and ground truth annotation must have a
39  nonnegative identifier indexing into this list.
40  * \param iou_thresholds The IOU (intersection over union) thresholds at which
41  * to compute the average precisions. This threshold determines
42  * whether a predicted bounding box and a ground truth bounding box
43  * are considered to match.
44  */
46  std::vector<float> iou_thresholds);
47 
48  // Variant of above that uses the default list of iou_thresholds, ranging from
49  // 0.5 to 0.95 with a step size of 0.05.
50  explicit average_precision_calculator(flex_list class_labels);
51 
52  /**
53  * Registers the predictions and ground truth annotations for one image.
54  */
55  void add_row(const std::vector<neural_net::image_annotation>& predictions,
56  const std::vector<neural_net::image_annotation>& ground_truth);
57 
58  /**
59  * Computes the average precision for each combination of class and requested
60  * IOU threshold.
61  *
62  * \return A map of evaluation results keyed by metric.
63  *
64  * The average precision can be interpreted as the area under the
65  * precision-recall curve.
66  *
67  * average_precision_50 is a dictionary mapping class label to the average
68  * precision for that class label at 50% IOU.
69  *
70  * average_precision is a dictionary mapping class label to the average
71  * precision for that class label, average across IOU thresholds from 50% to
72  * 95%.
73  *
74  * mean_average_precision_50 is the mean across class labels of the
75  * average_precision_50 values.
76  *
77  * mean_average_precision is the mean across class labels of the
78  * average_precision values.
79  */
80  variant_map_type evaluate();
81 
82 private:
83 
84  std::map<float, float> evaluate_class(size_t identifier);
85 
86  // Representation of one model prediction (for a given class).
87  struct prediction {
88  prediction(float confidence, neural_net::image_box bounding_box,
89  size_t row_index)
90  : confidence(confidence), bounding_box(std::move(bounding_box)),
91  row_index(row_index)
92  {}
93 
94  float confidence;
95  neural_net::image_box bounding_box;
96  size_t row_index;
97  };
98 
99  // All the data relevant to computing average precision for a single class.
100  struct class_data {
101 
102  // All the predictions with the class's label.
103  std::vector<prediction> predictions;
104 
105  // All the ground truth bounding boxes for the class.
106  std::vector<neural_net::image_box> ground_truth_boxes;
107 
108  // Given row index `i`, the elements of ground_truth_boxes associated with
109  // that row begin with `ground_truth_boxes[ground_truth_indices[i - 1]]` and
110  // end just before `ground_truth_boxes[ground_truth_indices[i]]`.
111  std::vector<size_t> ground_truth_indices;
112  };
113 
114  flex_list class_labels_;
115  std::vector<class_data> data_;
116  std::vector<float> iou_thresholds_;
117 };
118 
119 } // object_detection
120 } // turi
121 
122 #endif // TURI_OBJECT_DETECTION_OD_EVALUATION_H_
void add_row(const std::vector< neural_net::image_annotation > &predictions, const std::vector< neural_net::image_annotation > &ground_truth)
average_precision_calculator(flex_list class_labels, std::vector< float > iou_thresholds)
std::vector< flexible_type > flex_list