faster-coco-eval 1.5.4__tar.gz → 1.5.6__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 (38) hide show
  1. {faster_coco_eval-1.5.4/faster_coco_eval.egg-info → faster_coco_eval-1.5.6}/PKG-INFO +30 -4
  2. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/README.md +13 -2
  3. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/csrc/faster_eval_api/coco_eval/cocoeval.cpp +52 -25
  4. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/csrc/faster_eval_api/coco_eval/cocoeval.h +15 -5
  5. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/csrc/faster_eval_api/faster_eval_api.cpp +2 -1
  6. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/core/coco.py +102 -33
  7. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/core/cocoeval.py +202 -169
  8. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/core/faster_eval_api.py +96 -137
  9. faster_coco_eval-1.5.6/faster_coco_eval/extra/curves.py +187 -0
  10. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/extra/display.py +42 -12
  11. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/extra/draw.py +199 -34
  12. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/extra/extra.py +21 -2
  13. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/version.py +1 -1
  14. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6/faster_coco_eval.egg-info}/PKG-INFO +30 -4
  15. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval.egg-info/SOURCES.txt +4 -1
  16. faster_coco_eval-1.5.6/faster_coco_eval.egg-info/requires.txt +20 -0
  17. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/requirements/runtime.txt +1 -1
  18. faster_coco_eval-1.5.6/requirements/tests.txt +4 -0
  19. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/setup.py +6 -0
  20. faster_coco_eval-1.5.6/tests/test_coco_metric.py +193 -0
  21. faster_coco_eval-1.5.6/tests/test_lvis_metric.py +62 -0
  22. faster_coco_eval-1.5.4/faster_coco_eval/extra/curves.py +0 -63
  23. faster_coco_eval-1.5.4/faster_coco_eval.egg-info/requires.txt +0 -4
  24. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/LICENSE +0 -0
  25. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/MANIFEST.in +0 -0
  26. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/csrc/mask/common/maskApi.c +0 -0
  27. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/csrc/mask/common/maskApi.h +0 -0
  28. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/csrc/mask/pycocotools/_mask.pyx +0 -0
  29. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/__init__.py +0 -0
  30. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/core/__init__.py +0 -0
  31. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/core/mask.py +0 -0
  32. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/extra/__init__.py +0 -0
  33. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval/extra/utils.py +0 -0
  34. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval.egg-info/dependency_links.txt +0 -0
  35. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/faster_coco_eval.egg-info/top_level.txt +0 -0
  36. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/pyproject.toml +0 -0
  37. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/requirements/extra.txt +0 -0
  38. {faster_coco_eval-1.5.4 → faster_coco_eval-1.5.6}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: faster-coco-eval
3
- Version: 1.5.4
3
+ Version: 1.5.6
4
4
  Summary: Faster interpretation of the original COCOEval
5
5
  Home-page: https://github.com/MiXaiLL76/faster_coco_eval
6
6
  Author: MiXaiLL76
@@ -18,10 +18,25 @@ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
18
  Requires-Python: >=3.6
19
19
  Description-Content-Type: text/markdown
20
20
  License-File: LICENSE
21
- Requires-Dist: numpy
21
+ Requires-Dist: numpy<2
22
22
  Requires-Dist: plotly
23
23
  Requires-Dist: pandas
24
24
  Requires-Dist: Pillow
25
+ Provides-Extra: extra
26
+ Requires-Dist: numpy<2; extra == "extra"
27
+ Requires-Dist: plotly; extra == "extra"
28
+ Requires-Dist: pandas; extra == "extra"
29
+ Requires-Dist: Pillow; extra == "extra"
30
+ Requires-Dist: opencv-python-headless; extra == "extra"
31
+ Provides-Extra: tests
32
+ Requires-Dist: numpy<2; extra == "tests"
33
+ Requires-Dist: plotly; extra == "tests"
34
+ Requires-Dist: pandas; extra == "tests"
35
+ Requires-Dist: Pillow; extra == "tests"
36
+ Requires-Dist: numpy<2; extra == "tests"
37
+ Requires-Dist: pycocotools; extra == "tests"
38
+ Requires-Dist: pytest; extra == "tests"
39
+ Requires-Dist: parameterized; extra == "tests"
25
40
 
26
41
  # Faster-COCO-Eval
27
42
 
@@ -52,8 +67,8 @@ for coco's AP metrics, especially when dealing with a high number of instances i
52
67
  For our use case with a test dataset of 5000 images from the coco val dataset.
53
68
  Testing was carried out using the mmdetection framework and the eval_metric.py script. The indicators are presented below.
54
69
 
55
- Visualization of testing **colab_example.ipynb** available in directory [examples/comparison](https://github.com/MiXaiLL76/faster_coco_eval/blob/main/examples/comparison/mmdet/colab_example.ipynb)
56
- [colab_example.ipynb in google collab](https://drive.google.com/file/d/13raC0nRsil797ITJSv3FTRvUvfEK9SD-/view?usp=sharing)
70
+ Visualization of testing **colab_example.ipynb** available in directory [examples/comparison](https://nbviewer.org/github/MiXaiLL76/faster_coco_eval/blob/main/examples/comparison/mmdet/colab_example.ipynb)
71
+ [colab_example.ipynb in google collab](https://colab.research.google.com/drive/1qj392oIU8fmeyIFHCtxCrLA8PQQAMoIs)
57
72
  Tested with rtmdet model bbox + segm
58
73
 
59
74
  ### Summary for 5000 imgs
@@ -72,10 +87,21 @@ You can study in more detail in the [examples](https://github.com/MiXaiLL76/fast
72
87
 
73
88
  Code examples for using the library are available on the [Wiki](https://github.com/MiXaiLL76/faster_coco_eval/wiki)
74
89
 
90
+ ### Examples
91
+
92
+ - [Eval example](https://nbviewer.org/github/MiXaiLL76/faster_coco_eval/blob/main/examples/eval_example.ipynb)
93
+ - [Curve example](https://nbviewer.org/github/MiXaiLL76/faster_coco_eval/blob/main/examples/curve_example.ipynb)
94
+ - [CED Keypoint example](https://nbviewer.org/github/MiXaiLL76/faster_coco_eval/blob/main/examples/ced_example.ipynb)
95
+ - [LVIS example](https://nbviewer.org/github/MiXaiLL76/faster_coco_eval/blob/main/examples/lvis_example.ipynb)
96
+
75
97
  ## Update history
76
98
 
77
99
  Available via link [history.md](https://github.com/MiXaiLL76/faster_coco_eval/blob/main/history.md)
78
100
 
101
+ ## Star History
102
+
103
+ [![Star History Chart](https://api.star-history.com/svg?repos=MiXaiLL76/faster_coco_eval&type=Date)](https://star-history.com/#MiXaiLL76/faster_coco_eval&Date)
104
+
79
105
  ## License
80
106
 
81
107
  The original module was licensed with apache 2, I will continue with the same license.
@@ -27,8 +27,8 @@ for coco's AP metrics, especially when dealing with a high number of instances i
27
27
  For our use case with a test dataset of 5000 images from the coco val dataset.
28
28
  Testing was carried out using the mmdetection framework and the eval_metric.py script. The indicators are presented below.
29
29
 
30
- Visualization of testing **colab_example.ipynb** available in directory [examples/comparison](https://github.com/MiXaiLL76/faster_coco_eval/blob/main/examples/comparison/mmdet/colab_example.ipynb)
31
- [colab_example.ipynb in google collab](https://drive.google.com/file/d/13raC0nRsil797ITJSv3FTRvUvfEK9SD-/view?usp=sharing)
30
+ Visualization of testing **colab_example.ipynb** available in directory [examples/comparison](https://nbviewer.org/github/MiXaiLL76/faster_coco_eval/blob/main/examples/comparison/mmdet/colab_example.ipynb)
31
+ [colab_example.ipynb in google collab](https://colab.research.google.com/drive/1qj392oIU8fmeyIFHCtxCrLA8PQQAMoIs)
32
32
  Tested with rtmdet model bbox + segm
33
33
 
34
34
  ### Summary for 5000 imgs
@@ -47,10 +47,21 @@ You can study in more detail in the [examples](https://github.com/MiXaiLL76/fast
47
47
 
48
48
  Code examples for using the library are available on the [Wiki](https://github.com/MiXaiLL76/faster_coco_eval/wiki)
49
49
 
50
+ ### Examples
51
+
52
+ - [Eval example](https://nbviewer.org/github/MiXaiLL76/faster_coco_eval/blob/main/examples/eval_example.ipynb)
53
+ - [Curve example](https://nbviewer.org/github/MiXaiLL76/faster_coco_eval/blob/main/examples/curve_example.ipynb)
54
+ - [CED Keypoint example](https://nbviewer.org/github/MiXaiLL76/faster_coco_eval/blob/main/examples/ced_example.ipynb)
55
+ - [LVIS example](https://nbviewer.org/github/MiXaiLL76/faster_coco_eval/blob/main/examples/lvis_example.ipynb)
56
+
50
57
  ## Update history
51
58
 
52
59
  Available via link [history.md](https://github.com/MiXaiLL76/faster_coco_eval/blob/main/history.md)
53
60
 
61
+ ## Star History
62
+
63
+ [![Star History Chart](https://api.star-history.com/svg?repos=MiXaiLL76/faster_coco_eval&type=Date)](https://star-history.com/#MiXaiLL76/faster_coco_eval&Date)
64
+
54
65
  ## License
55
66
 
56
67
  The original module was licensed with apache 2, I will continue with the same license.
@@ -4,7 +4,6 @@
4
4
  #include <algorithm>
5
5
  #include <cstdint>
6
6
  #include <numeric>
7
-
8
7
  using namespace pybind11::literals;
9
8
 
10
9
  namespace coco_eval
@@ -82,13 +81,13 @@ namespace coco_eval
82
81
  const int num_detections = detection_sorted_indices.size();
83
82
  // std::vector<uint64_t> ground_truth_matches(
84
83
  // num_iou_thresholds * num_ground_truth, 0);
85
- std::vector<uint64_t> &ground_truth_matches = results->ground_truth_matches;
84
+ std::vector<int64_t> &ground_truth_matches = results->ground_truth_matches;
86
85
  ground_truth_matches.resize(num_iou_thresholds * num_ground_truth, 0);
87
86
 
88
- std::vector<int> &ground_truth_orig_id = results->ground_truth_orig_id;
87
+ std::vector<int64_t> &ground_truth_orig_id = results->ground_truth_orig_id;
89
88
  ground_truth_orig_id.resize(num_iou_thresholds * num_ground_truth, -1);
90
89
 
91
- std::vector<uint64_t> &detection_matches = results->detection_matches;
90
+ std::vector<int64_t> &detection_matches = results->detection_matches;
92
91
 
93
92
  std::vector<bool> &detection_ignores = results->detection_ignores;
94
93
  std::vector<bool> &ground_truth_ignores = results->ground_truth_ignores;
@@ -142,7 +141,7 @@ namespace coco_eval
142
141
  ground_truth_instances[ground_truth_sorted_indices[match]].id;
143
142
  ground_truth_matches[t * num_ground_truth + match] =
144
143
  detection_instances[detection_sorted_indices[d]].id;
145
- ground_truth_orig_id[t * num_ground_truth + match] = (int)ground_truth_instances[ground_truth_sorted_indices[match]].id;
144
+ ground_truth_orig_id[t * num_ground_truth + match] = (long)ground_truth_instances[ground_truth_sorted_indices[match]].id;
146
145
  }
147
146
 
148
147
  // set unmatched detections outside of area range to ignore
@@ -151,7 +150,7 @@ namespace coco_eval
151
150
  detection_ignores[t * num_detections + d] =
152
151
  detection_ignores[t * num_detections + d] ||
153
152
  (detection_matches[t * num_detections + d] == 0 &&
154
- (detection.area < area_range[0] || detection.area > area_range[1]));
153
+ (detection.area < area_range[0] || detection.area > area_range[1] || detection.lvis_mark));
155
154
  }
156
155
  }
157
156
 
@@ -533,7 +532,7 @@ namespace coco_eval
533
532
 
534
533
  time_t rawtime;
535
534
  struct tm local_time;
536
- std::array<char, 200> buffer;
535
+ char buffer[200];
537
536
  time(&rawtime);
538
537
 
539
538
  #ifdef _WIN32
@@ -541,14 +540,13 @@ namespace coco_eval
541
540
  #else
542
541
  localtime_r(&rawtime, &local_time);
543
542
  #endif
544
- strftime(
545
- buffer.data(), 200, "%Y-%m-%d %H:%num_max_detections:%S", &local_time);
543
+ strftime(buffer, 200, "%Y-%m-%d %H:%S", &local_time);
546
544
 
547
545
  int evaluations_size = static_cast<int>(evaluations.size());
548
546
 
549
- std::vector<uint64_t> out_detection_matches = {};
550
- std::vector<uint64_t> out_ground_truth_matches = {};
551
- std::vector<int> out_ground_truth_orig_id = {};
547
+ std::vector<int64_t> out_detection_matches = {};
548
+ std::vector<int64_t> out_ground_truth_matches = {};
549
+ std::vector<int64_t> out_ground_truth_orig_id = {};
552
550
  for (auto eval : evaluations)
553
551
  {
554
552
  out_detection_matches.insert(out_detection_matches.end(), eval.detection_matches.begin(), eval.detection_matches.end());
@@ -556,24 +554,53 @@ namespace coco_eval
556
554
  out_ground_truth_orig_id.insert(out_ground_truth_orig_id.end(), eval.ground_truth_orig_id.begin(), eval.ground_truth_orig_id.end());
557
555
  }
558
556
 
557
+ std::vector<int64_t> counts = {
558
+ num_iou_thresholds,
559
+ num_recall_thresholds,
560
+ num_categories,
561
+ num_area_ranges,
562
+ num_max_detections};
563
+
564
+ std::vector<int64_t> recall_counts = {
565
+ num_iou_thresholds,
566
+ num_categories,
567
+ num_area_ranges,
568
+ num_max_detections};
569
+ std::vector<int64_t> matches_shape = {num_iou_thresholds * num_area_ranges, -1};
570
+
559
571
  return py::dict(
560
572
  "params"_a = params,
561
- "counts"_a = std::vector<int64_t>({num_iou_thresholds,
562
- num_recall_thresholds,
563
- num_categories,
564
- num_area_ranges,
565
- num_max_detections}),
566
- "date"_a = buffer,
567
- "precision"_a = precisions_out,
568
- "recall"_a = recalls_out,
569
- "scores"_a = scores_out,
570
- "detection_matches"_a = out_detection_matches,
571
- // "detection_ignores"_a = out_detection_ignores,
572
- "ground_truth_matches"_a = out_ground_truth_matches,
573
- "ground_truth_orig_id"_a = out_ground_truth_orig_id,
573
+ "counts"_a = counts,
574
+ "date"_a = py::str(buffer),
575
+
576
+ // precision and scores are num_iou_thresholds X num_recall_thresholds X num_categories X num_area_ranges X num_max_detections
577
+ "precision"_a = py::array(precisions_out.size(), precisions_out.data()).reshape(counts),
578
+ "scores"_a = py::array(scores_out.size(), scores_out.data()).reshape(counts),
579
+
580
+ // recall is num_iou_thresholds X num_categories X num_area_ranges X num_max_detections
581
+ "recall"_a = py::array(recalls_out.size(), recalls_out.data()).reshape(recall_counts),
582
+
583
+ "detection_matches"_a = py::array(out_detection_matches.size(), out_detection_matches.data()).reshape(matches_shape),
584
+ "ground_truth_matches"_a = py::array(out_ground_truth_matches.size(), out_ground_truth_matches.data()).reshape(matches_shape),
585
+ "ground_truth_orig_id"_a = py::array(out_ground_truth_orig_id.size(), out_ground_truth_orig_id.data()).reshape(matches_shape),
574
586
  "evaluations_size"_a = evaluations_size);
575
587
  }
576
588
 
589
+ py::dict EvaluateAccumulate(
590
+ const py::object &params,
591
+ const ImageCategoryInstances<std::vector<double>> &image_category_ious,
592
+ const ImageCategoryInstances<InstanceAnnotation> &
593
+ image_category_ground_truth_instances,
594
+ const ImageCategoryInstances<InstanceAnnotation> &
595
+ image_category_detection_instances)
596
+ {
597
+ const std::vector<int> max_detections = list_to_vec<int>(params.attr("maxDets"));
598
+ const std::vector<std::array<double, 2>> area_ranges = list_to_vec<std::array<double, 2>>(params.attr("areaRng"));
599
+ const std::vector<double> iou_thresholds = list_to_vec<double>(params.attr("iouThrs"));
600
+
601
+ std::vector<ImageEvaluation> result = EvaluateImages(area_ranges, max_detections.back(), iou_thresholds, image_category_ious, image_category_ground_truth_instances, image_category_detection_instances);
602
+ return Accumulate(params, result);
603
+ }
577
604
  } // namespace COCOeval
578
605
 
579
606
  } // namespace coco_eval
@@ -23,13 +23,15 @@ namespace coco_eval
23
23
  double score,
24
24
  double area,
25
25
  bool is_crowd,
26
- bool ignore)
27
- : id{id}, score{score}, area{area}, is_crowd{is_crowd}, ignore{ignore} {}
26
+ bool ignore,
27
+ bool lvis_mark)
28
+ : id{id}, score{score}, area{area}, is_crowd{is_crowd}, ignore{ignore}, lvis_mark{lvis_mark} {}
28
29
  uint64_t id;
29
30
  double score = 0.;
30
31
  double area = 0.;
31
32
  bool is_crowd = false;
32
33
  bool ignore = false;
34
+ bool lvis_mark = false;
33
35
  };
34
36
 
35
37
  // Stores intermediate results for evaluating detection results for a single
@@ -39,10 +41,10 @@ namespace coco_eval
39
41
  {
40
42
  // For each of the D detected instances, the id of the matched ground truth
41
43
  // instance, or 0 if unmatched
42
- std::vector<uint64_t> detection_matches;
44
+ std::vector<int64_t> detection_matches;
43
45
 
44
- std::vector<uint64_t> ground_truth_matches;
45
- std::vector<int> ground_truth_orig_id;
46
+ std::vector<int64_t> ground_truth_matches;
47
+ std::vector<int64_t> ground_truth_orig_id;
46
48
  // The detection score of each of the D detected instances
47
49
  std::vector<double> detection_scores;
48
50
 
@@ -90,5 +92,13 @@ namespace coco_eval
90
92
  const py::object &params,
91
93
  const std::vector<ImageEvaluation> &evalutations);
92
94
 
95
+ py::dict EvaluateAccumulate(
96
+ const py::object &params,
97
+ const ImageCategoryInstances<std::vector<double>> &image_category_ious,
98
+ const ImageCategoryInstances<InstanceAnnotation> &
99
+ image_category_ground_truth_instances,
100
+ const ImageCategoryInstances<InstanceAnnotation> &
101
+ image_category_detection_instances);
102
+
93
103
  } // namespace COCOeval
94
104
  } // namespace coco_eval
@@ -48,7 +48,8 @@ namespace coco_eval
48
48
  m.def("get_compiler_version", &get_compiler_version, "get_compiler_version");
49
49
  m.def("COCOevalAccumulate", &COCOeval::Accumulate, "COCOeval::Accumulate");
50
50
  m.def("COCOevalEvaluateImages", &COCOeval::EvaluateImages, "COCOeval::EvaluateImages");
51
- pybind11::class_<COCOeval::InstanceAnnotation>(m, "InstanceAnnotation").def(pybind11::init<uint64_t, double, double, bool, bool>());
51
+ m.def("COCOevalEvaluateAccumulate", &COCOeval::EvaluateAccumulate, "COCOeval::EvaluateAccumulate");
52
+ pybind11::class_<COCOeval::InstanceAnnotation>(m, "InstanceAnnotation").def(pybind11::init<uint64_t, double, double, bool, bool, bool>());
52
53
  pybind11::class_<COCOeval::ImageEvaluation>(m, "ImageEvaluation").def(pybind11::init<>());
53
54
 
54
55
  #ifdef VERSION_INFO
@@ -45,7 +45,6 @@ __version__ = "2.0"
45
45
  # Licensed under the Simplified BSD License [see bsd.txt]
46
46
 
47
47
  import copy
48
- import itertools
49
48
  import json
50
49
  import logging
51
50
  import time
@@ -105,8 +104,11 @@ class COCO:
105
104
  def createIndex(self):
106
105
  # create index
107
106
  logger.debug("creating index...")
108
- anns, cats, imgs = {}, {}, {}
107
+ anns, cats, imgs, annToImgs = {}, {}, {}, {}
109
108
  imgToAnns, catToImgs = defaultdict(list), defaultdict(list)
109
+ imgCatToAnnsIdx = defaultdict(dict)
110
+ imgToAnnsIdx = defaultdict(dict)
111
+
110
112
  annsImgIds_dict = {}
111
113
  if "images" in self.dataset:
112
114
  for img in self.dataset["images"]:
@@ -120,6 +122,15 @@ class COCO:
120
122
  if annsImgIds_dict.get(ann["image_id"]):
121
123
  imgToAnns[ann["image_id"]].append(ann)
122
124
  anns[ann["id"]] = ann
125
+ annToImgs[ann["id"]] = ann["image_id"]
126
+ imgCatToAnnsIdx[(ann["image_id"], ann["category_id"])][
127
+ ann["id"]
128
+ ] = len(
129
+ imgCatToAnnsIdx[(ann["image_id"], ann["category_id"])]
130
+ )
131
+ imgToAnnsIdx[ann["image_id"]][ann["id"]] = len(
132
+ imgToAnnsIdx[ann["image_id"]]
133
+ )
123
134
 
124
135
  if "categories" in self.dataset:
125
136
  for cat in self.dataset["categories"]:
@@ -135,6 +146,9 @@ class COCO:
135
146
  self.anns = anns
136
147
  self.imgToAnns = imgToAnns
137
148
  self.catToImgs = catToImgs
149
+ self.annToImgs = annToImgs
150
+ self.imgCatToAnnsIdx = imgCatToAnnsIdx
151
+ self.imgToAnnsIdx = imgToAnnsIdx
138
152
  self.imgs = imgs
139
153
  self.cats = cats
140
154
 
@@ -160,40 +174,40 @@ class COCO:
160
174
  :return: ids (int array) : integer array of ann ids
161
175
 
162
176
  """
177
+
178
+ anns = []
179
+
180
+ if len(imgIds) != 0:
181
+ for img_id in imgIds:
182
+ anns.extend(self.img_ann_map[img_id])
183
+ else:
184
+ anns = self.dataset["annotations"]
185
+
186
+ # return early if no more filtering required
187
+ if (len(catIds) == 0) and (len(areaRng) == 0) and (iscrowd is None):
188
+ return [_ann["id"] for _ann in anns]
189
+
190
+ cat_ids = set(catIds)
191
+
192
+ if len(areaRng) == 0:
193
+ areaRng = [0, float("inf")]
194
+
163
195
  imgIds = imgIds if _isArrayLike(imgIds) else [imgIds]
164
196
  catIds = catIds if _isArrayLike(catIds) else [catIds]
165
197
 
166
- if len(imgIds) == len(catIds) == len(areaRng) == 0:
167
- anns = self.dataset["annotations"]
168
- else:
169
- if not len(imgIds) == 0:
170
- lists = [
171
- self.imgToAnns[imgId]
172
- for imgId in imgIds
173
- if imgId in self.imgToAnns
174
- ]
175
- anns = list(itertools.chain.from_iterable(lists))
176
- else:
177
- anns = self.dataset["annotations"]
178
- anns = (
179
- anns
180
- if len(catIds) == 0
181
- else [ann for ann in anns if ann["category_id"] in catIds]
182
- )
183
- anns = (
184
- anns
185
- if len(areaRng) == 0
186
- else [
187
- ann
188
- for ann in anns
189
- if ann["area"] > areaRng[0] and ann["area"] < areaRng[1]
190
- ]
191
- )
192
- if iscrowd is not None:
193
- ids = [ann["id"] for ann in anns if ann["iscrowd"] == iscrowd]
194
- else:
195
- ids = [ann["id"] for ann in anns]
196
- return ids
198
+ if iscrowd is None:
199
+ iscrowd = False
200
+
201
+ ann_ids = [
202
+ _ann["id"]
203
+ for _ann in anns
204
+ if _ann["category_id"] in cat_ids
205
+ and _ann["area"] > areaRng[0]
206
+ and _ann["area"] < areaRng[1]
207
+ and int(_ann.get("iscrowd", 0)) == int(iscrowd)
208
+ ]
209
+
210
+ return ann_ids
197
211
 
198
212
  def getCatIds(self, catNms=[], supNms=[], catIds=[]):
199
213
  """Filtering parameters.
@@ -453,21 +467,64 @@ class COCO:
453
467
  return m
454
468
 
455
469
  def get_ann_ids(self, img_ids=[], cat_ids=[], area_rng=[], iscrowd=None):
470
+ """Get ann ids that satisfy given filter conditions.
471
+
472
+ :param img_ids (int array) : get anns for given imgs
473
+ :param cat_ids (int array) : get anns for given cats
474
+ :param area_rng (float array) : get anns for given area range
475
+ (e.g. [0 inf])
476
+ :return: ids (int array) : integer array of ann ids
477
+
478
+ """
456
479
  return self.getAnnIds(img_ids, cat_ids, area_rng, iscrowd)
457
480
 
458
481
  def get_cat_ids(self, cat_names=[], sup_names=[], cat_ids=[]):
482
+ """Get cat ids that satisfy given filter conditions.
483
+
484
+ :param cat_names (str array) : get cats for given cat names
485
+ :param sup_names (str array) : get cats for given supercategory
486
+ names
487
+ :param cat_ids (int array) : get cats for given cat ids
488
+ :return: ids (int array) : integer array of cat ids
489
+
490
+ """
459
491
  return self.getCatIds(cat_names, sup_names, cat_ids)
460
492
 
461
493
  def get_img_ids(self, img_ids=[], cat_ids=[]):
494
+ """Get img ids that satisfy given filter conditions.
495
+
496
+ :param img_ids (int array) : get imgs for given ids
497
+ :param cat_ids (int array) : get imgs with all given cats
498
+ :return: ids (int array) : integer array of img ids
499
+
500
+ """
462
501
  return self.getImgIds(img_ids, cat_ids)
463
502
 
464
503
  def load_anns(self, ids):
504
+ """Load anns with the specified ids.
505
+
506
+ :param ids (int array) : integer ids specifying anns
507
+ :return: anns (object array) : loaded ann objects
508
+
509
+ """
465
510
  return self.loadAnns(ids)
466
511
 
467
512
  def load_cats(self, ids):
513
+ """Load cats with the specified ids.
514
+
515
+ :param ids (int array) : integer ids specifying cats
516
+ :return: cats (object array) : loaded cat objects
517
+
518
+ """
468
519
  return self.loadCats(ids)
469
520
 
470
521
  def load_imgs(self, ids):
522
+ """Load anns with the specified ids.
523
+
524
+ :param ids (int array) : integer ids specifying img
525
+ :return: imgs (object array) : loaded img objects
526
+
527
+ """
471
528
  return self.loadImgs(ids)
472
529
 
473
530
  @property
@@ -477,3 +534,15 @@ class COCO:
477
534
  @property
478
535
  def cat_img_map(self):
479
536
  return self.catToImgs
537
+
538
+ @property
539
+ def ann_img_map(self):
540
+ return self.annToImgs
541
+
542
+ @property
543
+ def img_ann_idx_map(self):
544
+ return self.imgToAnnsIdx
545
+
546
+ @property
547
+ def img_cat_ann_idx_map(self):
548
+ return self.imgCatToAnnsIdx