| import json | |
| import datasets | |
| from pathlib import Path | |
| _HOMEPAGE = 'https://cocodataset.org/' | |
| _LICENSE = 'Creative Commons Attribution 4.0 License' | |
| _DESCRIPTION = 'COCO is a large-scale object detection, segmentation, and captioning dataset.' | |
| _CITATION = '''\ | |
| @article{cocodataset, | |
| author = {Tsung{-}Yi Lin and Michael Maire and Serge J. Belongie and Lubomir D. Bourdev and Ross B. Girshick and James Hays and Pietro Perona and Deva Ramanan and Piotr Doll{'{a} }r and C. Lawrence Zitnick}, | |
| title = {Microsoft {COCO:} Common Objects in Context}, | |
| journal = {CoRR}, | |
| volume = {abs/1405.0312}, | |
| year = {2014}, | |
| url = {http://arxiv.org/abs/1405.0312}, | |
| archivePrefix = {arXiv}, | |
| eprint = {1405.0312}, | |
| timestamp = {Mon, 13 Aug 2018 16:48:13 +0200}, | |
| biburl = {https://dblp.org/rec/bib/journals/corr/LinMBHPRDZ14}, | |
| bibsource = {dblp computer science bibliography, https://dblp.org} | |
| } | |
| ''' | |
| _NAMES = [ | |
| 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', | |
| 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', | |
| 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', | |
| 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', | |
| 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', | |
| 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', | |
| 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', | |
| 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', | |
| 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', | |
| 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', | |
| 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', | |
| 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', | |
| 'scissors', 'teddy bear', 'hair drier', 'toothbrush' | |
| ] | |
| class COCODetectionConfig(datasets.BuilderConfig): | |
| '''Builder Config for coco2017''' | |
| def __init__( | |
| self, description, homepage, | |
| annotation_urls, **kwargs | |
| ): | |
| super(COCODetectionConfig, self).__init__( | |
| version=datasets.Version('1.0.0', ''), | |
| **kwargs | |
| ) | |
| self.description = description | |
| self.homepage = homepage | |
| url = 'http://images.cocodataset.org/zips/' | |
| self.train_image_url = url + 'train2017.zip' | |
| self.val_image_url = url + 'val2017.zip' | |
| self.train_annotation_urls = annotation_urls['train'] | |
| self.val_annotation_urls = annotation_urls['validation'] | |
| class COCODetection(datasets.GeneratorBasedBuilder): | |
| BUILDER_CONFIGS = [ | |
| COCODetectionConfig( | |
| description=_DESCRIPTION, | |
| homepage=_HOMEPAGE, | |
| annotation_urls={ | |
| 'train': 'data/instance_train.zip', | |
| 'validation': 'data/instance_validation.zip' | |
| }, | |
| ) | |
| ] | |
| def _info(self): | |
| features = datasets.Features({ | |
| 'image': datasets.Image(mode='RGB', decode=True, id=None), | |
| 'bboxes': datasets.Sequence( | |
| feature=datasets.Sequence( | |
| feature=datasets.Value(dtype='float32', id=None), | |
| length=4, id=None | |
| ), length=-1, id=None | |
| ), | |
| 'categories': datasets.Sequence( | |
| feature=datasets.ClassLabel(names=_NAMES), | |
| length=-1, id=None | |
| ), | |
| 'inst.rles': datasets.Sequence( | |
| feature={ | |
| 'size': datasets.Sequence( | |
| feature=datasets.Value(dtype='int32', id=None), | |
| length=2, id=None | |
| ), | |
| 'counts': datasets.Value(dtype='string', id=None) | |
| }, | |
| length=-1, id=None | |
| ), | |
| }) | |
| return datasets.DatasetInfo( | |
| description=_DESCRIPTION, | |
| features=features, | |
| homepage=_HOMEPAGE, | |
| license=_LICENSE, | |
| citation=_CITATION | |
| ) | |
| def _split_generators(self, dl_manager): | |
| train_image_path = dl_manager.download_and_extract( | |
| self.config.train_image_url | |
| ) | |
| val_image_path = dl_manager.download_and_extract( | |
| self.config.val_image_url | |
| ) | |
| train_annotation_paths = dl_manager.download_and_extract( | |
| self.config.train_annotation_urls | |
| ) | |
| val_annotation_paths = dl_manager.download_and_extract( | |
| self.config.val_annotation_urls | |
| ) | |
| return [ | |
| datasets.SplitGenerator( | |
| name=datasets.Split.TRAIN, | |
| gen_kwargs={ | |
| 'image_path': f'{train_image_path}/train2017', | |
| 'annotation_path': f'{train_annotation_paths}/instance_train.jsonl' | |
| } | |
| ), | |
| datasets.SplitGenerator( | |
| name=datasets.Split.VALIDATION, | |
| gen_kwargs={ | |
| 'image_path': f'{val_image_path}/val2017', | |
| 'annotation_path': f'{val_annotation_paths}/instance_validation.jsonl' | |
| } | |
| ) | |
| ] | |
| def _generate_examples(self, image_path, annotation_path): | |
| idx = 0 | |
| image_path = Path(image_path) | |
| with open(annotation_path, 'r', encoding='utf-8') as f: | |
| for line in f: | |
| obj = json.loads(line.strip()) | |
| example = { | |
| 'image': str(image_path / obj['image']), | |
| 'bboxes': obj['bboxes'], | |
| 'categories': obj['categories'], | |
| 'inst.rles': obj['inst.rles'] | |
| } | |
| yield idx, example | |
| idx += 1 | |