1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_TOOLS_EVALUATION_STAGES_OBJECT_DETECTION_AVERAGE_PRECISION_STAGE_H_
16 #define TENSORFLOW_LITE_TOOLS_EVALUATION_STAGES_OBJECT_DETECTION_AVERAGE_PRECISION_STAGE_H_
17 
18 #include <vector>
19 
20 #include "tensorflow/lite/tools/evaluation/evaluation_stage.h"
21 #include "tensorflow/lite/tools/evaluation/proto/evaluation_config.pb.h"
22 #include "tensorflow/lite/tools/evaluation/proto/evaluation_stages.pb.h"
23 #include "tensorflow/lite/tools/evaluation/stages/utils/image_metrics.h"
24 
25 namespace tflite {
26 namespace evaluation {
27 
28 // EvaluationStage to compute Average Precision for Object Detection Task.
29 // Computes Average Precision per-IoU threshold (averaged across all classes),
30 // and then mean Average Precision (mAP) as the average AP value across all
31 // thresholds.
32 class ObjectDetectionAveragePrecisionStage : public EvaluationStage {
33  public:
ObjectDetectionAveragePrecisionStage(const EvaluationStageConfig & config)34   explicit ObjectDetectionAveragePrecisionStage(
35       const EvaluationStageConfig& config)
36       : EvaluationStage(config) {}
37 
38   TfLiteStatus Init() override;
39 
40   TfLiteStatus Run() override;
41 
42   EvaluationStageMetrics LatestMetrics() override;
43 
44   // Call before Run().
SetEvalInputs(const ObjectDetectionResult & predicted_objects,const ObjectDetectionResult & ground_truth_objects)45   void SetEvalInputs(const ObjectDetectionResult& predicted_objects,
46                      const ObjectDetectionResult& ground_truth_objects) {
47     predicted_objects_ = predicted_objects;
48     ground_truth_objects_ = ground_truth_objects;
49   }
50 
51  private:
52   int num_classes_ = -1;
53   ObjectDetectionResult predicted_objects_;
54   ObjectDetectionResult ground_truth_objects_;
55   int current_image_index_ = 0;
56 
57   // One inner vector per class for ground truth objects.
58   std::vector<std::vector<image::Detection>> ground_truth_object_vectors_;
59   // One inner vector per class for predicted objects.
60   std::vector<std::vector<image::Detection>> predicted_object_vectors_;
61 };
62 
63 }  // namespace evaluation
64 }  // namespace tflite
65 
66 #endif  // TENSORFLOW_LITE_TOOLS_EVALUATION_STAGES_OBJECT_DETECTION_AVERAGE_PRECISION_STAGE_H_
67