Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed speed of COCO dataset parsing #1888

Merged
merged 6 commits into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import dataclasses
import json
import os
from collections import defaultdict

import numpy as np
from typing import List, Optional, Tuple
Expand Down Expand Up @@ -208,16 +209,16 @@ def parse_coco_into_detection_annotations(
category_names = np.array([category["name"] for category in coco["categories"]], dtype=str)

# Extract box annotations
ann_box_xyxy = xywh_to_xyxy_inplace(np.array([annotation["bbox"] for annotation in coco["annotations"]], dtype=np.float32), image_shape=None)
ann_box_xyxy = xywh_to_xyxy_inplace(np.array([annotation["bbox"] for annotation in coco["annotations"]], dtype=np.float32).reshape(-1, 4), image_shape=None)

ann_category_id = np.array([annotation["category_id"] for annotation in coco["annotations"]], dtype=int)
ann_iscrowd = np.array([annotation["iscrowd"] for annotation in coco["annotations"]], dtype=bool)
ann_image_ids = np.array([annotation["image_id"] for annotation in coco["annotations"]], dtype=int)
ann_category_id = np.array([annotation["category_id"] for annotation in coco["annotations"]], dtype=int).reshape(-1)
ann_iscrowd = np.array([annotation["iscrowd"] for annotation in coco["annotations"]], dtype=bool).reshape(-1)
ann_image_ids = np.array([annotation["image_id"] for annotation in coco["annotations"]], dtype=int).reshape(-1)

# Extract image stuff
img_ids = np.array([img["id"] for img in coco["images"]], dtype=int)
img_paths = np.array([img["file_name"] if "file_name" in img else "{:012}".format(img["id"]) + ".jpg" for img in coco["images"]], dtype=str)
img_width_height = np.array([(img["width"], img["height"]) for img in coco["images"]], dtype=int)
img_ids = [img["id"] for img in coco["images"]]
img_paths = [img["file_name"] if "file_name" in img else "{:012}".format(img["id"]) + ".jpg" for img in coco["images"]]
img_width_height = [(img["width"], img["height"]) for img in coco["images"]]

# Now, we can drop the annotations that belongs to the excluded classes
if int(class_ids_to_ignore is not None) + int(exclude_classes is not None) + int(include_classes is not None) > 1:
Expand Down Expand Up @@ -273,9 +274,15 @@ def parse_coco_into_detection_annotations(

annotations = []

for img_id, image_path, (image_width, image_height) in zip(img_ids, img_paths, img_width_height):
mask = ann_image_ids == img_id
img_id2ann_box_xyxy = defaultdict(list)
img_id2ann_iscrowd = defaultdict(list)
img_id2ann_category_id = defaultdict(list)
for ann_image_id, _ann_box_xyxy, _ann_iscrowd, _ann_category_id in zip(ann_image_ids, ann_box_xyxy, ann_iscrowd, ann_category_id):
img_id2ann_box_xyxy[ann_image_id].append(_ann_box_xyxy)
img_id2ann_iscrowd[ann_image_id].append(_ann_iscrowd)
img_id2ann_category_id[ann_image_id].append(_ann_category_id)

for img_id, image_path, (image_width, image_height) in zip(img_ids, img_paths, img_width_height):
if image_path_prefix is not None:
image_path = os.path.join(image_path_prefix, image_path)

Expand All @@ -284,9 +291,9 @@ def parse_coco_into_detection_annotations(
image_path=image_path,
image_width=image_width,
image_height=image_height,
ann_boxes_xyxy=ann_box_xyxy[mask],
ann_is_crowd=ann_iscrowd[mask],
ann_labels=ann_category_id[mask],
ann_boxes_xyxy=np.asarray(img_id2ann_box_xyxy[img_id], dtype=np.float32).reshape(-1, 4),
ann_is_crowd=np.asarray(img_id2ann_iscrowd[img_id], dtype=bool).reshape(-1),
ann_labels=np.asarray(img_id2ann_category_id[img_id], dtype=int).reshape(-1),
)
annotations.append(ann)

Expand Down
Loading