File size: 1,883 Bytes
8bb3bd1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
"""Definition of the BDD100K dataset."""
import os
import os.path as osp
from typing import List
import numpy as np
from mmdet.datasets import DATASETS, CocoDataset
from scalabel.label.io import save
from scalabel.label.transforms import bbox_to_box2d
from scalabel.label.typing import Frame, Label
@DATASETS.register_module()
class BDD100KDetDataset(CocoDataset): # type: ignore
"""BDD100K Dataset for detecion."""
CLASSES = [
"pedestrian",
"rider",
"car",
"truck",
"bus",
"train",
"motorcycle",
"bicycle",
"traffic light",
"traffic sign",
]
def convert_format(
self, results: List[List[np.ndarray]], out_dir: str # type: ignore
) -> None:
"""Format the results to the BDD100K prediction format."""
assert isinstance(results, list), "results must be a list"
assert len(results) == len(
self
), f"Length of res and dset not equal: {len(results)} != {len(self)}"
if not os.path.exists(out_dir):
os.makedirs(out_dir)
frames = []
ann_id = 0
for img_idx in range(len(self)):
img_name = self.data_infos[img_idx]["file_name"]
frame = Frame(name=img_name, labels=[])
frames.append(frame)
result = results[img_idx]
for cat_idx, bboxes in enumerate(result):
for bbox in bboxes:
ann_id += 1
label = Label(
id=ann_id,
score=bbox[-1],
box2d=bbox_to_box2d(self.xyxy2xywh(bbox)),
category=self.CLASSES[cat_idx],
)
frame.labels.append(label) # type: ignore
out_path = osp.join(out_dir, "det.json")
save(out_path, frames)
|