faster-coco-eval 1.6.8__tar.gz → 1.7.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. {faster_coco_eval-1.6.8/faster_coco_eval.egg-info → faster_coco_eval-1.7.0}/PKG-INFO +2 -2
  2. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/README.md +1 -1
  3. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/csrc/faster_eval_api/coco_eval/cocoeval.cpp +72 -24
  4. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/csrc/faster_eval_api/coco_eval/cocoeval.h +10 -34
  5. faster_coco_eval-1.7.0/csrc/faster_eval_api/coco_eval/dataset.cpp +309 -0
  6. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/csrc/faster_eval_api/coco_eval/dataset.h +38 -25
  7. faster_coco_eval-1.7.0/csrc/faster_eval_api/coco_eval/types.h +40 -0
  8. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/csrc/faster_eval_api/faster_eval_api.cpp +20 -13
  9. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/csrc/mask_api/src/mask.cpp +201 -87
  10. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/csrc/mask_api/src/rle.cpp +69 -26
  11. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/core/cocoeval.py +61 -28
  12. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/core/faster_eval_api.py +19 -20
  13. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/utils/pytorch/coco_eval.py +27 -4
  14. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/version.py +1 -1
  15. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0/faster_coco_eval.egg-info}/PKG-INFO +2 -2
  16. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval.egg-info/SOURCES.txt +4 -0
  17. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/setup.py +1 -0
  18. faster_coco_eval-1.7.0/tests/test_dataset.py +254 -0
  19. faster_coco_eval-1.7.0/tests/test_extra_utils.py +161 -0
  20. faster_coco_eval-1.7.0/tests/test_ranges.py +109 -0
  21. faster_coco_eval-1.7.0/tests/test_simple_extra.py +167 -0
  22. faster_coco_eval-1.6.8/csrc/faster_eval_api/coco_eval/dataset.cpp +0 -246
  23. faster_coco_eval-1.6.8/tests/test_dataset.py +0 -66
  24. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/LICENSE +0 -0
  25. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/MANIFEST.in +0 -0
  26. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/csrc/mask_api/mask_api.cpp +0 -0
  27. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/csrc/mask_api/src/mask.h +0 -0
  28. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/__init__.py +0 -0
  29. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/core/__init__.py +0 -0
  30. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/core/coco.py +0 -0
  31. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/core/mask.py +0 -0
  32. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/extra/__init__.py +0 -0
  33. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/extra/curves.py +0 -0
  34. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/extra/display.py +0 -0
  35. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/extra/draw.py +0 -0
  36. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/extra/extra.py +0 -0
  37. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/extra/utils.py +0 -0
  38. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/utils/__init__.py +0 -0
  39. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/utils/pytorch/__init__.py +0 -0
  40. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval/utils/pytorch/coco_dataset.py +0 -0
  41. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval.egg-info/dependency_links.txt +0 -0
  42. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval.egg-info/requires.txt +0 -0
  43. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/faster_coco_eval.egg-info/top_level.txt +0 -0
  44. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/pyproject.toml +0 -0
  45. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/requirements/docs.txt +0 -0
  46. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/requirements/extra.txt +0 -0
  47. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/requirements/runtime.txt +0 -0
  48. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/requirements/tests.txt +0 -0
  49. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/setup.cfg +0 -0
  50. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_basic.py +0 -0
  51. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_boundary.py +0 -0
  52. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_coco_metric.py +0 -0
  53. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_cocoapi_fake_data.py +0 -0
  54. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_crowdpose.py +0 -0
  55. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_extra_draw.py +0 -0
  56. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_init_pycocotools.py +0 -0
  57. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_keypoints.py +0 -0
  58. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_lvis_metric.py +0 -0
  59. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_mask_api.py +0 -0
  60. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_torchmetrics.py +0 -0
  61. {faster_coco_eval-1.6.8 → faster_coco_eval-1.7.0}/tests/test_world_evalutor.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: faster-coco-eval
3
- Version: 1.6.8
3
+ Version: 1.7.0
4
4
  Summary: Faster interpretation of the original COCOEval
5
5
  Home-page: https://github.com/MiXaiLL76/faster_coco_eval
6
6
  Author: MiXaiLL76
@@ -145,7 +145,7 @@ Visualization of testing **colab_example.ipynb** available in directory [example
145
145
  | bbox | 5.812 | 22.72 | 3.909 |
146
146
  | segm | 7.413 | 24.434 | 3.296 |
147
147
 
148
- ## Feautures
148
+ ## Features
149
149
 
150
150
  This library provides not only validation functions, but also error visualization functions. Including visualization of errors in the image.
151
151
  You can study in more detail in the [examples](https://mixaill76.github.io/faster_coco_eval/examples.html) and [Wiki](https://github.com/MiXaiLL76/faster_coco_eval/wiki).
@@ -98,7 +98,7 @@ Visualization of testing **colab_example.ipynb** available in directory [example
98
98
  | bbox | 5.812 | 22.72 | 3.909 |
99
99
  | segm | 7.413 | 24.434 | 3.296 |
100
100
 
101
- ## Feautures
101
+ ## Features
102
102
 
103
103
  This library provides not only validation functions, but also error visualization functions. Including visualization of errors in the image.
104
104
  You can study in more detail in the [examples](https://mixaill76.github.io/faster_coco_eval/examples.html) and [Wiki](https://github.com/MiXaiLL76/faster_coco_eval/wiki).
@@ -189,17 +189,12 @@ std::vector<ImageEvaluation> EvaluateImages(
189
189
  const std::vector<std::array<double, 2>> &area_ranges, int max_detections,
190
190
  const std::vector<double> &iou_thresholds,
191
191
  const ImageCategoryInstances<std::vector<double>> &image_category_ious,
192
- const ImageCategoryInstances<InstanceAnnotation>
193
- &image_category_ground_truth_instances,
194
- const ImageCategoryInstances<InstanceAnnotation>
195
- &image_category_detection_instances) {
192
+ const LightweightDataset &gt_dataset, const LightweightDataset &dt_dataset,
193
+ const std::vector<double> &img_ids, const std::vector<double> &cat_ids,
194
+ bool useCats) {
196
195
  const int num_area_ranges = (const int)area_ranges.size();
197
- const int num_images =
198
- (const int)image_category_ground_truth_instances.size();
199
- const int num_categories =
200
- (const int)(image_category_ious.size() > 0
201
- ? image_category_ious[0].size()
202
- : 0);
196
+ const int num_images = (const int)img_ids.size();
197
+ const int num_categories = useCats ? (const int)cat_ids.size() : 1;
203
198
  std::vector<uint64_t> detection_sorted_indices;
204
199
  std::vector<uint64_t> ground_truth_sorted_indices;
205
200
  std::vector<bool> ignores;
@@ -211,12 +206,48 @@ std::vector<ImageEvaluation> EvaluateImages(
211
206
  // ImageEvaluation object
212
207
  for (auto i = 0; i < num_images; ++i) {
213
208
  for (auto c = 0; c < num_categories; ++c) {
214
- const std::vector<InstanceAnnotation>
215
- &ground_truth_instances =
216
- image_category_ground_truth_instances[i][c];
217
- const std::vector<InstanceAnnotation>
218
- &detection_instances =
219
- image_category_detection_instances[i][c];
209
+ // Read annotations on-demand from datasets
210
+ double img_id = img_ids[i];
211
+
212
+ std::vector<InstanceAnnotation> ground_truth_instances;
213
+ std::vector<InstanceAnnotation> detection_instances;
214
+
215
+ if (useCats) {
216
+ double cat_id = cat_ids[c];
217
+ ground_truth_instances =
218
+ gt_dataset.get_cpp_annotations(img_id,
219
+ cat_id);
220
+ detection_instances =
221
+ dt_dataset.get_cpp_annotations(img_id,
222
+ cat_id);
223
+ } else {
224
+ // When useCats=False, merge all categories for
225
+ // this image
226
+ for (size_t j = 0; j < cat_ids.size(); ++j) {
227
+ double cat_id = cat_ids[j];
228
+ std::vector<InstanceAnnotation>
229
+ gt_anns =
230
+ gt_dataset.get_cpp_annotations(
231
+ img_id, cat_id);
232
+ std::vector<InstanceAnnotation>
233
+ dt_anns =
234
+ dt_dataset.get_cpp_annotations(
235
+ img_id, cat_id);
236
+
237
+ ground_truth_instances.insert(
238
+ ground_truth_instances.end(),
239
+ std::make_move_iterator(
240
+ gt_anns.begin()),
241
+ std::make_move_iterator(
242
+ gt_anns.end()));
243
+ detection_instances.insert(
244
+ detection_instances.end(),
245
+ std::make_move_iterator(
246
+ dt_anns.begin()),
247
+ std::make_move_iterator(
248
+ dt_anns.end()));
249
+ }
250
+ }
220
251
 
221
252
  SortInstancesByDetectionScore(
222
253
  detection_instances, &detection_sorted_indices);
@@ -241,6 +272,24 @@ std::vector<ImageEvaluation> EvaluateImages(
241
272
  num_images +
242
273
  a * num_images + i]);
243
274
  }
275
+
276
+ // Clear cache entries to free memory after processing
277
+ // all area_ranges
278
+ if (useCats) {
279
+ double cat_id = cat_ids[c];
280
+ gt_dataset.clear_cache_entry(img_id, cat_id);
281
+ dt_dataset.clear_cache_entry(img_id, cat_id);
282
+ } else {
283
+ // When useCats=False, clear cache for all
284
+ // categories used
285
+ for (size_t j = 0; j < cat_ids.size(); ++j) {
286
+ double cat_id = cat_ids[j];
287
+ gt_dataset.clear_cache_entry(img_id,
288
+ cat_id);
289
+ dt_dataset.clear_cache_entry(img_id,
290
+ cat_id);
291
+ }
292
+ }
244
293
  }
245
294
  }
246
295
 
@@ -614,10 +663,9 @@ py::dict Accumulate(const py::object &params,
614
663
  py::dict EvaluateAccumulate(
615
664
  const py::object &params,
616
665
  const ImageCategoryInstances<std::vector<double>> &image_category_ious,
617
- const ImageCategoryInstances<InstanceAnnotation>
618
- &image_category_ground_truth_instances,
619
- const ImageCategoryInstances<InstanceAnnotation>
620
- &image_category_detection_instances) {
666
+ const LightweightDataset &gt_dataset, const LightweightDataset &dt_dataset,
667
+ const std::vector<double> &img_ids, const std::vector<double> &cat_ids,
668
+ bool useCats) {
621
669
  const std::vector<int> max_detections =
622
670
  list_to_vec<int>(params.attr("maxDets"));
623
671
  const std::vector<std::array<double, 2>> area_ranges =
@@ -625,10 +673,10 @@ py::dict EvaluateAccumulate(
625
673
  const std::vector<double> iou_thresholds =
626
674
  list_to_vec<double>(params.attr("iouThrs"));
627
675
 
628
- std::vector<ImageEvaluation> result = EvaluateImages(
629
- area_ranges, max_detections.back(), iou_thresholds,
630
- image_category_ious, image_category_ground_truth_instances,
631
- image_category_detection_instances);
676
+ std::vector<ImageEvaluation> result =
677
+ EvaluateImages(area_ranges, max_detections.back(), iou_thresholds,
678
+ image_category_ious, gt_dataset, dt_dataset, img_ids,
679
+ cat_ids, useCats);
632
680
  return Accumulate(params, result);
633
681
  }
634
682
 
@@ -8,38 +8,16 @@
8
8
 
9
9
  #include <vector>
10
10
 
11
+ #include "types.h"
12
+
11
13
  namespace py = pybind11;
12
14
 
13
15
  namespace coco_eval {
14
16
 
15
17
  namespace COCOeval {
16
18
 
17
- // Annotation data for a single object instance in an image
18
- struct InstanceAnnotation {
19
- InstanceAnnotation(uint64_t id, double score, double area,
20
- bool is_crowd, bool ignore, bool lvis_mark)
21
- : id{id},
22
- score{score},
23
- area{area},
24
- is_crowd{is_crowd},
25
- ignore{ignore},
26
- lvis_mark{lvis_mark} {}
27
- uint64_t id;
28
- double score = 0.;
29
- double area = 0.;
30
- bool is_crowd = false;
31
- bool ignore = false;
32
- bool lvis_mark = false;
33
- };
34
-
35
- // Stores the match between a detected instance and a ground truth instance
36
- struct MatchedAnnotation {
37
- MatchedAnnotation(uint64_t dt_id, uint64_t gt_id, double iou)
38
- : dt_id{dt_id}, gt_id{gt_id}, iou{iou} {}
39
- uint64_t dt_id;
40
- uint64_t gt_id;
41
- double iou;
42
- };
19
+ // Forward declaration
20
+ class LightweightDataset;
43
21
 
44
22
  // Stores intermediate results for evaluating detection results for a single
45
23
  // image that has D detected instances and G ground truth instances. This stores
@@ -85,10 +63,9 @@ std::vector<ImageEvaluation> EvaluateImages(
85
63
  &area_ranges, // vector of 2-tuples
86
64
  int max_detections, const std::vector<double> &iou_thresholds,
87
65
  const ImageCategoryInstances<std::vector<double>> &image_category_ious,
88
- const ImageCategoryInstances<InstanceAnnotation>
89
- &image_category_ground_truth_instances,
90
- const ImageCategoryInstances<InstanceAnnotation>
91
- &image_category_detection_instances);
66
+ const LightweightDataset &gt_dataset, const LightweightDataset &dt_dataset,
67
+ const std::vector<double> &img_ids, const std::vector<double> &cat_ids,
68
+ bool useCats);
92
69
 
93
70
  // C++ implementation of COCOeval.accumulate(), which generates precision
94
71
  // recall curves for each set of category, IOU threshold, detection area range,
@@ -101,10 +78,9 @@ py::dict Accumulate(const py::object &params,
101
78
  py::dict EvaluateAccumulate(
102
79
  const py::object &params,
103
80
  const ImageCategoryInstances<std::vector<double>> &image_category_ious,
104
- const ImageCategoryInstances<InstanceAnnotation>
105
- &image_category_ground_truth_instances,
106
- const ImageCategoryInstances<InstanceAnnotation>
107
- &image_category_detection_instances);
81
+ const LightweightDataset &gt_dataset, const LightweightDataset &dt_dataset,
82
+ const std::vector<double> &img_ids, const std::vector<double> &cat_ids,
83
+ bool useCats);
108
84
 
109
85
  long double calc_auc(const std::vector<long double> &recall_list,
110
86
  const std::vector<long double> &precision_list);
@@ -0,0 +1,309 @@
1
+ #include <time.h>
2
+
3
+ #include <algorithm>
4
+ #include <cstdint>
5
+ #include <numeric>
6
+
7
+ // clang-format off
8
+ #include "cocoeval.h"
9
+ #include "dataset.h"
10
+ // clang-format on
11
+
12
+ using namespace pybind11::literals;
13
+
14
+ namespace coco_eval {
15
+
16
+ namespace COCOeval {
17
+
18
+ // Store reference to annotation instead of copying data
19
+ void LightweightDataset::append_ref(double img_id, double cat_id,
20
+ py::object ann_ref) {
21
+ const std::pair<int64_t, int64_t> key{static_cast<int64_t>(img_id),
22
+ static_cast<int64_t>(cat_id)};
23
+ annotation_refs[key].emplace_back(ann_ref);
24
+ }
25
+
26
+ // Remove all stored references and clear cache
27
+ void LightweightDataset::clean() {
28
+ annotation_refs.clear();
29
+ cpp_cache.clear();
30
+
31
+ // Reclaim memory by swapping with empty containers
32
+ std::unordered_map<std::pair<int64_t, int64_t>, std::vector<py::object>,
33
+ hash_pair>()
34
+ .swap(annotation_refs);
35
+ std::unordered_map<std::pair<int64_t, int64_t>,
36
+ std::vector<InstanceAnnotation>, hash_pair>()
37
+ .swap(cpp_cache);
38
+ }
39
+
40
+ // Get dataset size (number of (img_id, cat_id) pairs with annotations)
41
+ size_t LightweightDataset::size() const { return annotation_refs.size(); }
42
+
43
+ // Serialize dataset contents to a tuple for pickle support
44
+ py::tuple LightweightDataset::make_tuple() const {
45
+ // Create a list of (img_id, cat_id, annotation_list) tuples
46
+ py::list serialized_data;
47
+ for (const auto &kv : annotation_refs) {
48
+ auto key = kv.first;
49
+ auto ann_list = kv.second;
50
+
51
+ py::list py_ann_list;
52
+ for (const auto &ann : ann_list) {
53
+ py_ann_list.append(ann);
54
+ }
55
+
56
+ serialized_data.append(py::make_tuple(
57
+ static_cast<double>(key.first),
58
+ static_cast<double>(key.second), py_ann_list));
59
+ }
60
+
61
+ return py::make_tuple(static_cast<int>(annotation_refs.size()),
62
+ serialized_data);
63
+ }
64
+
65
+ // Load dataset state from a Python tuple (for unpickling)
66
+ void LightweightDataset::load_tuple(py::tuple pickle_data) {
67
+ if (pickle_data.size() != 2)
68
+ throw std::runtime_error(
69
+ "Invalid state! Tuple must have 2 elements.");
70
+
71
+ // Get size and data from tuple
72
+ int expected_size = pickle_data[0].cast<int>();
73
+ py::list serialized_data = pickle_data[1].cast<py::list>();
74
+
75
+ // Clear existing data and reserve memory
76
+ annotation_refs.clear();
77
+ cpp_cache.clear();
78
+ annotation_refs.reserve(expected_size);
79
+
80
+ // Reconstruct data from serialized list
81
+ for (auto item : serialized_data) {
82
+ py::tuple entry = item.cast<py::tuple>();
83
+ if (entry.size() != 3) continue;
84
+
85
+ double img_id = entry[0].cast<double>();
86
+ double cat_id = entry[1].cast<double>();
87
+ py::list ann_list = entry[2].cast<py::list>();
88
+
89
+ std::pair<int64_t, int64_t> key{static_cast<int64_t>(img_id),
90
+ static_cast<int64_t>(cat_id)};
91
+
92
+ std::vector<py::object> annotations;
93
+ for (auto ann : ann_list) {
94
+ annotations.emplace_back(
95
+ py::reinterpret_borrow<py::object>(ann));
96
+ }
97
+
98
+ annotation_refs[key] = std::move(annotations);
99
+ }
100
+ }
101
+
102
+ // Get all Python dict annotations for a given image/category pair
103
+ std::vector<py::dict> LightweightDataset::get(double img_id, double cat_id) {
104
+ const std::pair<int64_t, int64_t> key(static_cast<int64_t>(img_id),
105
+ static_cast<int64_t>(cat_id));
106
+ auto it = annotation_refs.find(key);
107
+ if (it != annotation_refs.end()) {
108
+ std::vector<py::dict> result;
109
+ result.reserve(it->second.size());
110
+
111
+ for (const auto &py_ann : it->second) {
112
+ // Convert py::object to py::dict
113
+ result.emplace_back(py_ann.cast<py::dict>());
114
+ }
115
+
116
+ return result;
117
+ } else {
118
+ return {};
119
+ }
120
+ }
121
+
122
+ // Helper method to convert py::object to InstanceAnnotation
123
+ InstanceAnnotation LightweightDataset::parse_py_annotation(
124
+ const py::object &ann) const {
125
+ uint64_t id = 0;
126
+ double score = 0.0;
127
+ double area = 0.0;
128
+ bool is_crowd = false;
129
+ bool ignore = false;
130
+ bool lvis_mark = false;
131
+
132
+ // Extract values from Python dict with safe type handling
133
+ py::dict ann_dict = ann.cast<py::dict>();
134
+
135
+ try {
136
+ if (ann_dict.contains("id")) {
137
+ id = ann_dict["id"].cast<uint64_t>();
138
+ }
139
+ } catch (const std::exception &) {
140
+ }
141
+
142
+ try {
143
+ if (ann_dict.contains("score")) {
144
+ score = ann_dict["score"].cast<double>();
145
+ }
146
+ } catch (const std::exception &) {
147
+ }
148
+
149
+ try {
150
+ if (ann_dict.contains("area")) {
151
+ area = ann_dict["area"].cast<double>();
152
+ }
153
+ } catch (const std::exception &) {
154
+ }
155
+
156
+ try {
157
+ if (ann_dict.contains("is_crowd")) {
158
+ is_crowd = ann_dict["is_crowd"].cast<bool>();
159
+ } else if (ann_dict.contains("iscrowd")) {
160
+ is_crowd = ann_dict["iscrowd"].cast<bool>();
161
+ }
162
+ } catch (const std::exception &) {
163
+ }
164
+
165
+ try {
166
+ if (ann_dict.contains("ignore")) {
167
+ ignore = ann_dict["ignore"].cast<bool>();
168
+ }
169
+ } catch (const std::exception &) {
170
+ }
171
+
172
+ try {
173
+ if (ann_dict.contains("lvis_mark")) {
174
+ lvis_mark = ann_dict["lvis_mark"].cast<bool>();
175
+ }
176
+ } catch (const std::exception &) {
177
+ }
178
+
179
+ // Construct and return the annotation.
180
+ return InstanceAnnotation(id, score, area, is_crowd, ignore, lvis_mark);
181
+ }
182
+
183
+ // Get C++ annotation objects with caching for performance
184
+ std::vector<InstanceAnnotation> LightweightDataset::get_cpp_annotations(
185
+ double img_id, double cat_id) const {
186
+ const std::pair<int64_t, int64_t> key(static_cast<int64_t>(img_id),
187
+ static_cast<int64_t>(cat_id));
188
+
189
+ // Check cache first
190
+ auto cache_it = cpp_cache.find(key);
191
+ if (cache_it != cpp_cache.end()) {
192
+ return cache_it->second;
193
+ }
194
+
195
+ // If not in cache, get from annotation_refs and convert
196
+ auto it = annotation_refs.find(key);
197
+ if (it != annotation_refs.end()) {
198
+ std::vector<InstanceAnnotation> result;
199
+ result.reserve(it->second.size());
200
+
201
+ // Convert each Python annotation to InstanceAnnotation
202
+ for (const auto &py_ann : it->second) {
203
+ result.emplace_back(parse_py_annotation(py_ann));
204
+ }
205
+
206
+ // Cache the result for future use
207
+ cpp_cache[key] = result;
208
+ return result;
209
+ } else {
210
+ return {};
211
+ }
212
+ }
213
+
214
+ // Clear cache entry for specific (img_id, cat_id) to free memory
215
+ void LightweightDataset::clear_cache_entry(double img_id, double cat_id) const {
216
+ const std::pair<int64_t, int64_t> key(static_cast<int64_t>(img_id),
217
+ static_cast<int64_t>(cat_id));
218
+ cpp_cache.erase(key);
219
+ }
220
+
221
+ // Get all C++ annotation objects for provided img_ids and cat_ids
222
+ std::vector<std::vector<std::vector<InstanceAnnotation>>>
223
+ LightweightDataset::get_cpp_instances(const std::vector<double> &img_ids,
224
+ const std::vector<double> &cat_ids,
225
+ const bool &useCats) {
226
+ std::vector<std::vector<std::vector<InstanceAnnotation>>> result;
227
+ result.reserve(img_ids.size()); // Reserve space for image indices
228
+
229
+ for (size_t i = 0; i < img_ids.size(); ++i) {
230
+ int64_t img_id = static_cast<int64_t>(img_ids[i]);
231
+ if (useCats) {
232
+ std::vector<std::vector<InstanceAnnotation>>
233
+ cat_results;
234
+ cat_results.reserve(
235
+ cat_ids.size()); // Reserve space for categories
236
+
237
+ for (size_t j = 0; j < cat_ids.size(); ++j) {
238
+ int64_t cat_id =
239
+ static_cast<int64_t>(cat_ids[j]);
240
+ cat_results.emplace_back(
241
+ get_cpp_annotations(img_id, cat_id));
242
+ }
243
+ result.emplace_back(std::move(cat_results));
244
+ } else {
245
+ // Single vector to merge all categories for this image
246
+ std::vector<InstanceAnnotation> merged;
247
+ for (size_t j = 0; j < cat_ids.size(); ++j) {
248
+ int64_t cat_id =
249
+ static_cast<int64_t>(cat_ids[j]);
250
+ std::vector<InstanceAnnotation> anns =
251
+ get_cpp_annotations(img_id, cat_id);
252
+ merged.insert(
253
+ merged.end(),
254
+ std::make_move_iterator(anns.begin()),
255
+ std::make_move_iterator(anns.end()));
256
+ }
257
+ // Wrap merged vector in an outer vector for consistency
258
+ result.emplace_back(1, std::move(merged));
259
+ }
260
+ }
261
+ return result;
262
+ }
263
+
264
+ // Get all Python dict annotations for provided img_ids and cat_ids
265
+ std::vector<std::vector<std::vector<py::dict>>>
266
+ LightweightDataset::get_instances(const std::vector<double> &img_ids,
267
+ const std::vector<double> &cat_ids,
268
+ const bool &useCats) {
269
+ std::vector<std::vector<std::vector<py::dict>>> result;
270
+ result.reserve(img_ids.size()); // Reserve space for images
271
+
272
+ for (size_t i = 0; i < img_ids.size(); ++i) {
273
+ int64_t img_id = static_cast<int64_t>(img_ids[i]);
274
+
275
+ if (useCats) {
276
+ std::vector<std::vector<py::dict>> cat_results;
277
+ cat_results.reserve(
278
+ cat_ids.size()); // Reserve space for categories
279
+
280
+ for (size_t j = 0; j < cat_ids.size(); ++j) {
281
+ int64_t cat_id =
282
+ static_cast<int64_t>(cat_ids[j]);
283
+ cat_results.emplace_back(get(img_id, cat_id));
284
+ }
285
+ result.emplace_back(std::move(cat_results));
286
+ } else {
287
+ // Single vector to merge all categories for this image
288
+ std::vector<py::dict> merged;
289
+ for (size_t j = 0; j < cat_ids.size(); ++j) {
290
+ int64_t cat_id =
291
+ static_cast<int64_t>(cat_ids[j]);
292
+
293
+ std::vector<py::dict> anns =
294
+ get(img_id, cat_id);
295
+ merged.insert(
296
+ merged.end(),
297
+ std::make_move_iterator(anns.begin()),
298
+ std::make_move_iterator(anns.end()));
299
+ }
300
+ // Wrap merged vector in an outer vector for consistency
301
+ result.emplace_back(1, std::move(merged));
302
+ }
303
+ }
304
+ return result;
305
+ }
306
+
307
+ } // namespace COCOeval
308
+
309
+ } // namespace coco_eval
@@ -6,6 +6,8 @@
6
6
  #include <unordered_map>
7
7
  #include <vector>
8
8
 
9
+ #include "types.h"
10
+
9
11
  namespace py = pybind11;
10
12
 
11
13
  namespace coco_eval {
@@ -26,57 +28,68 @@ struct hash_pair {
26
28
  }
27
29
  };
28
30
 
29
- class Dataset {
31
+ class LightweightDataset {
30
32
  public:
31
- Dataset() {
32
- // Reserve initial space to reduce rehashing, improving memory
33
- // and performance.
34
- data.reserve(8192);
35
- // Optionally, you can set max_load_factor to lower value if
36
- // memory is less critical: data.max_load_factor(0.25f);
33
+ LightweightDataset() {
34
+ // Reserve initial space to reduce rehashing
35
+ annotation_refs.reserve(8192);
37
36
  }
38
37
 
39
- // Append a new annotation for (img_id, cat_id) key.
40
- void append(double img_id, double cat_id, const py::dict &ann);
38
+ // Store reference to annotation instead of copying data
39
+ void append_ref(double img_id, double cat_id, py::object ann_ref);
41
40
 
42
- // Remove all stored annotations and free memory.
41
+ // Remove all stored references and clear cache
43
42
  void clean();
44
43
 
45
- // Get dataset size (number of (img_id, cat_id) pairs with annotations).
44
+ // Get dataset size (number of (img_id, cat_id) pairs with annotations)
46
45
  size_t size() const;
47
46
 
48
- // Pickle support: Serialize dataset contents to a tuple.
47
+ // Pickle support: Serialize dataset contents to a tuple
49
48
  py::tuple make_tuple() const;
50
49
 
51
- // Pickle support: Load dataset contents from a tuple.
50
+ // Pickle support: Load dataset contents from a tuple
52
51
  void load_tuple(py::tuple pickle_data);
53
52
 
54
- // Get all Python dict annotations for a given image/category pair.
53
+ // Get all Python dict annotations for a given image/category pair
55
54
  std::vector<py::dict> get(double img_id, double cat_id);
56
55
 
57
- // Get C++ annotation objects for a given image/category pair.
58
- std::vector<InstanceAnnotation> get_cpp_annotations(double img_id,
59
- double cat_id);
56
+ // Get C++ annotation objects with caching for performance
57
+ std::vector<InstanceAnnotation> get_cpp_annotations(
58
+ double img_id, double cat_id) const;
59
+
60
+ // Clear cache entry for specific (img_id, cat_id) to free memory
61
+ void clear_cache_entry(double img_id, double cat_id) const;
60
62
 
61
- // Get all C++ annotation objects for provided img_ids and cat_ids. If
62
- // useCats is false, cat_ids is ignored.
63
+ // Get all C++ annotation objects for provided img_ids and cat_ids
63
64
  std::vector<std::vector<std::vector<InstanceAnnotation>>>
64
65
  get_cpp_instances(const std::vector<double> &img_ids,
65
66
  const std::vector<double> &cat_ids,
66
67
  const bool &useCats);
67
68
 
68
- // Get all Python dict annotations for provided img_ids and cat_ids. If
69
- // useCats is false, cat_ids is ignored.
69
+ // Get all Python dict annotations for provided img_ids and cat_ids
70
70
  std::vector<std::vector<std::vector<py::dict>>> get_instances(
71
71
  const std::vector<double> &img_ids,
72
72
  const std::vector<double> &cat_ids, const bool &useCats);
73
73
 
74
+ // Legacy compatibility - same as append_ref but with different
75
+ // signature
76
+ void append(double img_id, double cat_id, const py::dict &ann) {
77
+ append_ref(img_id, cat_id, py::cast<py::object>(ann));
78
+ }
79
+
74
80
  private:
75
- // Use unordered_map to store annotations for (img_id, cat_id) pairs.
76
- // Custom hash functor is used.
77
- std::unordered_map<std::pair<int64_t, int64_t>, std::vector<py::dict>,
81
+ // Lightweight storage: only references to Python objects
82
+ std::unordered_map<std::pair<int64_t, int64_t>, std::vector<py::object>,
78
83
  hash_pair>
79
- data;
84
+ annotation_refs;
85
+
86
+ // Cache for frequently accessed InstanceAnnotation objects
87
+ mutable std::unordered_map<std::pair<int64_t, int64_t>,
88
+ std::vector<InstanceAnnotation>, hash_pair>
89
+ cpp_cache;
90
+
91
+ // Helper method to convert py::object to InstanceAnnotation
92
+ InstanceAnnotation parse_py_annotation(const py::object &ann) const;
80
93
  };
81
94
  } // namespace COCOeval
82
95
  } // namespace coco_eval