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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) OpenMMLab. All rights reserved.
|
| 2 |
+
import copy
|
| 3 |
+
import os.path as osp
|
| 4 |
+
from typing import List, Union
|
| 5 |
+
|
| 6 |
+
from mmengine.fileio import get_local_path
|
| 7 |
+
|
| 8 |
+
from mmdet.registry import DATASETS
|
| 9 |
+
from .api_wrappers import COCO
|
| 10 |
+
from .base_det_dataset import BaseDetDataset
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@DATASETS.register_module()
|
| 14 |
+
class DaveDataset(BaseDetDataset):
|
| 15 |
+
"""Dataset for COCO."""
|
| 16 |
+
|
| 17 |
+
METAINFO = {
|
| 18 |
+
'classes':
|
| 19 |
+
('AgricultureVehicle', 'Animal', 'Bicycle', 'Bus', 'Car', 'ConstructionVehicle', 'EgoVehicle', 'MotorBike', 'MotorizedTricycle', 'MultiWheeler', 'Pedestrian', 'Scooter', 'Tractor', 'TriCycle', 'Truck', 'Van'),
|
| 20 |
+
# palette is a list of color tuples, which is used for visualization.
|
| 21 |
+
'palette':
|
| 22 |
+
[(220, 20, 60), (119, 11, 32), (0, 0, 142), (0, 0, 230), (106, 0, 228),
|
| 23 |
+
(0, 60, 100), (0, 80, 100), (0, 0, 70), (0, 0, 192), (250, 170, 30),
|
| 24 |
+
(100, 170, 30), (220, 220, 0), (175, 116, 175), (250, 0, 30),
|
| 25 |
+
(165, 42, 42), (255, 77, 255)]
|
| 26 |
+
}
|
| 27 |
+
COCOAPI = COCO
|
| 28 |
+
# ann_id is unique in coco dataset.
|
| 29 |
+
ANN_ID_UNIQUE = True
|
| 30 |
+
|
| 31 |
+
def load_data_list(self) -> List[dict]:
|
| 32 |
+
"""Load annotations from an annotation file named as ``self.ann_file``
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
List[dict]: A list of annotation.
|
| 36 |
+
""" # noqa: E501
|
| 37 |
+
with get_local_path(
|
| 38 |
+
self.ann_file, backend_args=self.backend_args) as local_path:
|
| 39 |
+
self.coco = self.COCOAPI(local_path)
|
| 40 |
+
# The order of returned `cat_ids` will not
|
| 41 |
+
# change with the order of the `classes`
|
| 42 |
+
self.cat_ids = self.coco.get_cat_ids(
|
| 43 |
+
cat_names=self.metainfo['classes'])
|
| 44 |
+
self.cat2label = {cat_id: i for i, cat_id in enumerate(self.cat_ids)}
|
| 45 |
+
self.cat_img_map = copy.deepcopy(self.coco.cat_img_map)
|
| 46 |
+
|
| 47 |
+
img_ids = self.coco.get_img_ids()
|
| 48 |
+
data_list = []
|
| 49 |
+
total_ann_ids = []
|
| 50 |
+
for img_id in img_ids:
|
| 51 |
+
raw_img_info = self.coco.load_imgs([img_id])[0]
|
| 52 |
+
raw_img_info['img_id'] = img_id
|
| 53 |
+
|
| 54 |
+
ann_ids = self.coco.get_ann_ids(img_ids=[img_id])
|
| 55 |
+
raw_ann_info = self.coco.load_anns(ann_ids)
|
| 56 |
+
total_ann_ids.extend(ann_ids)
|
| 57 |
+
|
| 58 |
+
parsed_data_info = self.parse_data_info({
|
| 59 |
+
'raw_ann_info':
|
| 60 |
+
raw_ann_info,
|
| 61 |
+
'raw_img_info':
|
| 62 |
+
raw_img_info
|
| 63 |
+
})
|
| 64 |
+
data_list.append(parsed_data_info)
|
| 65 |
+
if self.ANN_ID_UNIQUE:
|
| 66 |
+
assert len(set(total_ann_ids)) == len(
|
| 67 |
+
total_ann_ids
|
| 68 |
+
), f"Annotation ids in '{self.ann_file}' are not unique!"
|
| 69 |
+
|
| 70 |
+
del self.coco
|
| 71 |
+
|
| 72 |
+
return data_list
|
| 73 |
+
|
| 74 |
+
def parse_data_info(self, raw_data_info: dict) -> Union[dict, List[dict]]:
|
| 75 |
+
"""Parse raw annotation to target format.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
raw_data_info (dict): Raw data information load from ``ann_file``
|
| 79 |
+
|
| 80 |
+
Returns:
|
| 81 |
+
Union[dict, List[dict]]: Parsed annotation.
|
| 82 |
+
"""
|
| 83 |
+
img_info = raw_data_info['raw_img_info']
|
| 84 |
+
ann_info = raw_data_info['raw_ann_info']
|
| 85 |
+
|
| 86 |
+
data_info = {}
|
| 87 |
+
|
| 88 |
+
# TODO: need to change data_prefix['img'] to data_prefix['img_path']
|
| 89 |
+
img_path = osp.join(self.data_prefix['img'], img_info['file_name'])
|
| 90 |
+
if self.data_prefix.get('seg', None):
|
| 91 |
+
seg_map_path = osp.join(
|
| 92 |
+
self.data_prefix['seg'],
|
| 93 |
+
img_info['file_name'].rsplit('.', 1)[0] + self.seg_map_suffix)
|
| 94 |
+
else:
|
| 95 |
+
seg_map_path = None
|
| 96 |
+
data_info['img_path'] = img_path
|
| 97 |
+
data_info['img_id'] = img_info['img_id']
|
| 98 |
+
data_info['seg_map_path'] = seg_map_path
|
| 99 |
+
data_info['height'] = img_info['height']
|
| 100 |
+
data_info['width'] = img_info['width']
|
| 101 |
+
|
| 102 |
+
if self.return_classes:
|
| 103 |
+
data_info['text'] = self.metainfo['classes']
|
| 104 |
+
data_info['caption_prompt'] = self.caption_prompt
|
| 105 |
+
data_info['custom_entities'] = True
|
| 106 |
+
|
| 107 |
+
instances = []
|
| 108 |
+
for i, ann in enumerate(ann_info):
|
| 109 |
+
instance = {}
|
| 110 |
+
|
| 111 |
+
if ann.get('ignore', False):
|
| 112 |
+
continue
|
| 113 |
+
x1, y1, w, h = ann['bbox']
|
| 114 |
+
inter_w = max(0, min(x1 + w, img_info['width']) - max(x1, 0))
|
| 115 |
+
inter_h = max(0, min(y1 + h, img_info['height']) - max(y1, 0))
|
| 116 |
+
if inter_w * inter_h == 0:
|
| 117 |
+
continue
|
| 118 |
+
if ann['area'] <= 0 or w < 1 or h < 1:
|
| 119 |
+
continue
|
| 120 |
+
if ann['category_id'] not in self.cat_ids:
|
| 121 |
+
continue
|
| 122 |
+
bbox = [x1, y1, x1 + w, y1 + h]
|
| 123 |
+
|
| 124 |
+
if ann.get('iscrowd', False):
|
| 125 |
+
instance['ignore_flag'] = 1
|
| 126 |
+
else:
|
| 127 |
+
instance['ignore_flag'] = 0
|
| 128 |
+
instance['bbox'] = bbox
|
| 129 |
+
instance['bbox_label'] = self.cat2label[ann['category_id']]
|
| 130 |
+
|
| 131 |
+
if ann.get('segmentation', None):
|
| 132 |
+
instance['mask'] = ann['segmentation']
|
| 133 |
+
|
| 134 |
+
instances.append(instance)
|
| 135 |
+
data_info['instances'] = instances
|
| 136 |
+
return data_info
|
| 137 |
+
|
| 138 |
+
def filter_data(self) -> List[dict]:
|
| 139 |
+
"""Filter annotations according to filter_cfg.
|
| 140 |
+
|
| 141 |
+
Returns:
|
| 142 |
+
List[dict]: Filtered results.
|
| 143 |
+
"""
|
| 144 |
+
if self.test_mode:
|
| 145 |
+
return self.data_list
|
| 146 |
+
|
| 147 |
+
if self.filter_cfg is None:
|
| 148 |
+
return self.data_list
|
| 149 |
+
|
| 150 |
+
filter_empty_gt = self.filter_cfg.get('filter_empty_gt', False)
|
| 151 |
+
min_size = self.filter_cfg.get('min_size', 0)
|
| 152 |
+
|
| 153 |
+
# obtain images that contain annotation
|
| 154 |
+
ids_with_ann = set(data_info['img_id'] for data_info in self.data_list)
|
| 155 |
+
# obtain images that contain annotations of the required categories
|
| 156 |
+
ids_in_cat = set()
|
| 157 |
+
for i, class_id in enumerate(self.cat_ids):
|
| 158 |
+
ids_in_cat |= set(self.cat_img_map[class_id])
|
| 159 |
+
# merge the image id sets of the two conditions and use the merged set
|
| 160 |
+
# to filter out images if self.filter_empty_gt=True
|
| 161 |
+
ids_in_cat &= ids_with_ann
|
| 162 |
+
|
| 163 |
+
valid_data_infos = []
|
| 164 |
+
for i, data_info in enumerate(self.data_list):
|
| 165 |
+
img_id = data_info['img_id']
|
| 166 |
+
width = data_info['width']
|
| 167 |
+
height = data_info['height']
|
| 168 |
+
if filter_empty_gt and img_id not in ids_in_cat:
|
| 169 |
+
continue
|
| 170 |
+
if min(width, height) >= min_size:
|
| 171 |
+
valid_data_infos.append(data_info)
|
| 172 |
+
|
| 173 |
+
return valid_data_infos
|