1) Simplifying the logic of use_iscrowd and compatibility with other datasets.

2) Making `get_coco_class_index_mapping` return the inversed mapping, to simplify its usage.
This commit is contained in:
rafaelpadilla
2025-06-01 15:52:01 +00:00
parent 52980a6aa7
commit cff974555e
3 changed files with 9 additions and 19 deletions
-6
View File
@@ -563,7 +563,6 @@ class DetectionDataset(BaseDataset):
images_directory_path: str,
annotations_path: str,
force_masks: bool = False,
use_iscrowd: bool = False,
) -> DetectionDataset:
"""
Creates a Dataset instance from COCO formatted data.
@@ -575,10 +574,6 @@ class DetectionDataset(BaseDataset):
force_masks (bool): If True,
forces masks to be loaded for all annotations,
regardless of whether they are present.
use_iscrowd (bool): If True,
uses COCO's property `iscrowd` in all annotations,
regardless of whether they are present. If not presented, `iscrowd=0`
will be used.
Returns:
DetectionDataset: A DetectionDataset instance containing
the loaded images and annotations.
@@ -608,7 +603,6 @@ class DetectionDataset(BaseDataset):
images_directory_path=images_directory_path,
annotations_path=annotations_path,
force_masks=force_masks,
use_iscrowd=use_iscrowd,
)
return DetectionDataset(classes=classes, images=images, annotations=annotations)
+5 -7
View File
@@ -105,6 +105,7 @@ def coco_annotations_to_detections(
xyxy = np.asarray(xyxy)
xyxy[:, 2:4] += xyxy[:, 0:2]
data = dict()
if use_iscrowd:
iscrowd = [
image_annotation["iscrowd"] for image_annotation in image_annotations
@@ -113,10 +114,6 @@ def coco_annotations_to_detections(
data = dict(
iscrowd=np.asarray(iscrowd, dtype=int), area=np.asarray(area, dtype=float)
)
else:
iscrowd = [0] * len(image_annotations)
area = None
data = dict()
if with_masks:
mask = coco_annotations_to_masks(
@@ -182,14 +179,15 @@ def get_coco_class_index_mapping(annotations_path: str) -> Dict[int, int]:
class_mapping = build_coco_class_index_mapping(
coco_categories=coco_data["categories"], target_classes=classes
)
return class_mapping
inv_class_mapping = {v: k for k, v in class_mapping.items()}
return inv_class_mapping
def load_coco_annotations(
images_directory_path: str,
annotations_path: str,
force_masks: bool = False,
use_iscrowd: bool = False,
# use_iscrowd: bool = True,
) -> Tuple[List[str], List[str], Dict[str, Detections]]:
coco_data = read_json_file(file_path=annotations_path)
classes = coco_categories_to_classes(coco_categories=coco_data["categories"])
@@ -219,7 +217,7 @@ def load_coco_annotations(
image_annotations=image_annotations,
resolution_wh=(image_width, image_height),
with_masks=force_masks,
use_iscrowd=use_iscrowd,
use_iscrowd=True,
)
annotation = map_detections_class_id(
@@ -666,9 +666,6 @@ class COCOEvaluator:
dt_sorted = np.argsort([-d["score"] for d in dt], kind="stable")
dt = [dt[i] for i in dt_sorted[0:max_det]]
# Get the iscrowd flag for each gt
# iscrowd = [int(o["iscrowd"]) for o in gt]
# Load computed ious for the given image and category
ious = (
self.ious[img_id, cat_id][:, gt_sorted]
@@ -1230,15 +1227,16 @@ class MeanAveragePrecision(Metric):
for target in image_targets:
xyxy = target[0] # or xyxy = prediction[0]; xyxy[2:4] -= xyxy[0:2]
xywh = [xyxy[0], xyxy[1], xyxy[2] - xyxy[0], xyxy[3] - xyxy[1]]
# Get "area" and "iscrowd" from data
# Get "area" and "iscrowd" (default 0) from data
data = target[5]
if self._class_mapping is not None:
category_id = self._class_mapping[target[3].item()]
else:
category_id = target[3].item()
dict_annotation = {
"area": data.get("area"),
"iscrowd": data.get("iscrowd"),
"area": data.get("area", 0),
"iscrowd": data.get("iscrowd", 0),
"image_id": image_id,
"bbox": xywh,
"category_id": category_id,