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
|
|
|
|
|
| 1 |
+
import gravdataset
|
| 2 |
+
import os
|
| 3 |
+
from gravdataset.features import Features, Sequence, Value
|
| 4 |
+
from pycocotools.coco import COCO
|
| 5 |
+
|
| 6 |
+
_DESCRIPTION = 'COCO dataset for detection and instance segmentation task.'
|
| 7 |
+
|
| 8 |
+
_URLS = {
|
| 9 |
+
'COCO2014': {
|
| 10 |
+
'train_prefix': 'train2014',
|
| 11 |
+
'train_meta': 'annotations/instances_train2014.json',
|
| 12 |
+
'val_prefix': 'val2014',
|
| 13 |
+
'val_meta': 'annotations/instances_val2014.json'
|
| 14 |
+
},
|
| 15 |
+
'COCO2017': {
|
| 16 |
+
'train_prefix': 'train2017',
|
| 17 |
+
'train_meta': 'annotations/instances_train2017.json',
|
| 18 |
+
'val_prefix': 'val2017',
|
| 19 |
+
'val_meta': 'annotations/instances_val2017.json'
|
| 20 |
+
},
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
_CLASSES = ('person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
|
| 24 |
+
'train', 'truck', 'boat', 'traffic light', 'fire hydrant',
|
| 25 |
+
'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog',
|
| 26 |
+
'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe',
|
| 27 |
+
'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
|
| 28 |
+
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat',
|
| 29 |
+
'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
|
| 30 |
+
'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
|
| 31 |
+
'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot',
|
| 32 |
+
'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
|
| 33 |
+
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop',
|
| 34 |
+
'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven',
|
| 35 |
+
'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
|
| 36 |
+
'scissors', 'teddy bear', 'hair drier', 'toothbrush')
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class Coco(gravdataset.GeneratorBasedBuilder):
|
| 40 |
+
"""COCO dataset for detection and instance segmentation task."""
|
| 41 |
+
|
| 42 |
+
VERSION = gravdataset.Version('0.1.0')
|
| 43 |
+
|
| 44 |
+
BUILDER_CONFIGS = [
|
| 45 |
+
gravdataset.BuilderConfig(
|
| 46 |
+
name='COCO2014',
|
| 47 |
+
version=VERSION,
|
| 48 |
+
description='COCO2014 dataset for det and segm'),
|
| 49 |
+
gravdataset.BuilderConfig(
|
| 50 |
+
name='COCO2017',
|
| 51 |
+
version=VERSION,
|
| 52 |
+
description='COCO2017 dataset for det and segm'),
|
| 53 |
+
]
|
| 54 |
+
# It's not mandatory to have a default configuration.
|
| 55 |
+
# Just use one if it make sense.
|
| 56 |
+
DEFAULT_CONFIG_NAME = 'train'
|
| 57 |
+
|
| 58 |
+
def _info(self):
|
| 59 |
+
return gravdataset.DatasetInfo(
|
| 60 |
+
# This is the description that will appear on the datasets page.
|
| 61 |
+
description=_DESCRIPTION,
|
| 62 |
+
meta_info=dict(classes=_CLASSES),
|
| 63 |
+
features=Features({
|
| 64 |
+
'img_info': {
|
| 65 |
+
'filename': Value('string'),
|
| 66 |
+
'height': Value('int32'),
|
| 67 |
+
'width': Value('int32'),
|
| 68 |
+
},
|
| 69 |
+
'ann_info': {
|
| 70 |
+
'bboxes': Sequence(Sequence(Value('float64'))),
|
| 71 |
+
'labels': Sequence(Value('int64')),
|
| 72 |
+
'masks': Sequence(Sequence(Sequence(Value('float64')))),
|
| 73 |
+
'bboxes_ignore': Sequence(Sequence(Value('float64'))),
|
| 74 |
+
'label_ignore': Sequence(Value('int64')),
|
| 75 |
+
'masks_ignore': Sequence(
|
| 76 |
+
{
|
| 77 |
+
'counts': Sequence(Value('int64')),
|
| 78 |
+
'size': Sequence(Value('int64'))
|
| 79 |
+
}
|
| 80 |
+
),
|
| 81 |
+
'seg_map': Value('string')
|
| 82 |
+
}
|
| 83 |
+
}))
|
| 84 |
+
|
| 85 |
+
def _split_generators(self, dl_manager):
|
| 86 |
+
train_prefix = _URLS[self.config.name]['train_prefix']
|
| 87 |
+
train_meta = _URLS[self.config.name]['train_meta']
|
| 88 |
+
val_prefix = _URLS[self.config.name]['val_prefix']
|
| 89 |
+
val_meta = _URLS[self.config.name]['val_meta']
|
| 90 |
+
train_meta = dl_manager.download(train_meta)
|
| 91 |
+
val_meta = dl_manager.download(val_meta)
|
| 92 |
+
return [
|
| 93 |
+
gravdataset.SplitGenerator(
|
| 94 |
+
name='train',
|
| 95 |
+
# These kwargs will be passed to _generate_examples
|
| 96 |
+
gen_kwargs={
|
| 97 |
+
'img_prefix': train_prefix,
|
| 98 |
+
'ann_file': train_meta
|
| 99 |
+
}),
|
| 100 |
+
gravdataset.SplitGenerator(
|
| 101 |
+
name='val',
|
| 102 |
+
# These kwargs will be passed to _generate_examples
|
| 103 |
+
gen_kwargs={
|
| 104 |
+
'img_prefix': val_prefix,
|
| 105 |
+
'ann_file': val_meta
|
| 106 |
+
}),
|
| 107 |
+
]
|
| 108 |
+
|
| 109 |
+
def _generate_examples(self, img_prefix, ann_file):
|
| 110 |
+
"""Parser coco format annotation file."""
|
| 111 |
+
coco = COCO(ann_file)
|
| 112 |
+
cat_ids = coco.getCatIds(_CLASSES)
|
| 113 |
+
cat2label = {cat_id: i for i, cat_id in enumerate(cat_ids)}
|
| 114 |
+
img_ids = coco.getImgIds()
|
| 115 |
+
index = 0
|
| 116 |
+
for i in img_ids:
|
| 117 |
+
sample = dict(img_info=dict())
|
| 118 |
+
info = coco.loadImgs([i])[0]
|
| 119 |
+
sample['img_info']['filename'] = os.path.join(
|
| 120 |
+
img_prefix, info['file_name'])
|
| 121 |
+
sample['img_info']['height'] = info['height']
|
| 122 |
+
sample['img_info']['width'] = info['width']
|
| 123 |
+
ann_ids = coco.getAnnIds([i])
|
| 124 |
+
ann_info = coco.loadAnns(ann_ids)
|
| 125 |
+
gt_bboxes = []
|
| 126 |
+
gt_labels = []
|
| 127 |
+
gt_bboxes_ignore = []
|
| 128 |
+
gt_label_ignore = []
|
| 129 |
+
gt_masks_ann = []
|
| 130 |
+
gt_masks_ignore = []
|
| 131 |
+
for i, ann in enumerate(ann_info):
|
| 132 |
+
if ann.get('ignore', False):
|
| 133 |
+
continue
|
| 134 |
+
x1, y1, w, h = ann['bbox']
|
| 135 |
+
inter_w = max(0, min(x1 + w, info['width']) - max(x1, 0))
|
| 136 |
+
inter_h = max(0, min(y1 + h, info['height']) - max(y1, 0))
|
| 137 |
+
if inter_w * inter_h == 0:
|
| 138 |
+
continue
|
| 139 |
+
if ann['area'] <= 0 or w < 1 or h < 1:
|
| 140 |
+
continue
|
| 141 |
+
if ann['category_id'] not in cat_ids:
|
| 142 |
+
continue
|
| 143 |
+
bbox = [x1, y1, x1 + w, y1 + h]
|
| 144 |
+
if ann.get('iscrowd', False):
|
| 145 |
+
gt_bboxes_ignore.append(bbox)
|
| 146 |
+
gt_label_ignore.append(cat2label[ann['category_id']])
|
| 147 |
+
gt_masks_ignore.append(ann.get('segmentation', None))
|
| 148 |
+
else:
|
| 149 |
+
gt_bboxes.append(bbox)
|
| 150 |
+
gt_labels.append(cat2label[ann['category_id']])
|
| 151 |
+
gt_masks_ann.append(ann.get('segmentation', None))
|
| 152 |
+
|
| 153 |
+
seg_map = sample['img_info']['filename'].rsplit('.', 1)[0] + '.png'
|
| 154 |
+
|
| 155 |
+
sample['ann_info'] = dict(
|
| 156 |
+
bboxes=gt_bboxes,
|
| 157 |
+
labels=gt_labels,
|
| 158 |
+
bboxes_ignore=gt_bboxes_ignore,
|
| 159 |
+
label_ignore=gt_label_ignore,
|
| 160 |
+
masks=gt_masks_ann,
|
| 161 |
+
masks_ignore=gt_masks_ignore,
|
| 162 |
+
seg_map=seg_map)
|
| 163 |
+
yield index, sample
|
| 164 |
+
index += 1
|