Meta Byte Track
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cocoeval.h 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
  2. #pragma once
  3. #include <pybind11/numpy.h>
  4. #include <pybind11/pybind11.h>
  5. #include <pybind11/stl.h>
  6. #include <pybind11/stl_bind.h>
  7. #include <vector>
  8. namespace py = pybind11;
  9. namespace COCOeval {
  10. // Annotation data for a single object instance in an image
  11. struct InstanceAnnotation {
  12. InstanceAnnotation(
  13. uint64_t id,
  14. double score,
  15. double area,
  16. bool is_crowd,
  17. bool ignore)
  18. : id{id}, score{score}, area{area}, is_crowd{is_crowd}, ignore{ignore} {}
  19. uint64_t id;
  20. double score = 0.;
  21. double area = 0.;
  22. bool is_crowd = false;
  23. bool ignore = false;
  24. };
  25. // Stores intermediate results for evaluating detection results for a single
  26. // image that has D detected instances and G ground truth instances. This stores
  27. // matches between detected and ground truth instances
  28. struct ImageEvaluation {
  29. // For each of the D detected instances, the id of the matched ground truth
  30. // instance, or 0 if unmatched
  31. std::vector<uint64_t> detection_matches;
  32. // The detection score of each of the D detected instances
  33. std::vector<double> detection_scores;
  34. // Marks whether or not each of G instances was ignored from evaluation (e.g.,
  35. // because it's outside area_range)
  36. std::vector<bool> ground_truth_ignores;
  37. // Marks whether or not each of D instances was ignored from evaluation (e.g.,
  38. // because it's outside aRng)
  39. std::vector<bool> detection_ignores;
  40. };
  41. template <class T>
  42. using ImageCategoryInstances = std::vector<std::vector<std::vector<T>>>;
  43. // C++ implementation of COCO API cocoeval.py::COCOeval.evaluateImg(). For each
  44. // combination of image, category, area range settings, and IOU thresholds to
  45. // evaluate, it matches detected instances to ground truth instances and stores
  46. // the results into a vector of ImageEvaluation results, which will be
  47. // interpreted by the COCOeval::Accumulate() function to produce precion-recall
  48. // curves. The parameters of nested vectors have the following semantics:
  49. // image_category_ious[i][c][d][g] is the intersection over union of the d'th
  50. // detected instance and g'th ground truth instance of
  51. // category category_ids[c] in image image_ids[i]
  52. // image_category_ground_truth_instances[i][c] is a vector of ground truth
  53. // instances in image image_ids[i] of category category_ids[c]
  54. // image_category_detection_instances[i][c] is a vector of detected
  55. // instances in image image_ids[i] of category category_ids[c]
  56. std::vector<ImageEvaluation> EvaluateImages(
  57. const std::vector<std::array<double, 2>>& area_ranges, // vector of 2-tuples
  58. int max_detections,
  59. const std::vector<double>& iou_thresholds,
  60. const ImageCategoryInstances<std::vector<double>>& image_category_ious,
  61. const ImageCategoryInstances<InstanceAnnotation>&
  62. image_category_ground_truth_instances,
  63. const ImageCategoryInstances<InstanceAnnotation>&
  64. image_category_detection_instances);
  65. // C++ implementation of COCOeval.accumulate(), which generates precision
  66. // recall curves for each set of category, IOU threshold, detection area range,
  67. // and max number of detections parameters. It is assumed that the parameter
  68. // evaluations is the return value of the functon COCOeval::EvaluateImages(),
  69. // which was called with the same parameter settings params
  70. py::dict Accumulate(
  71. const py::object& params,
  72. const std::vector<ImageEvaluation>& evalutations);
  73. } // namespace COCOeval