Datasets:
File size: 5,197 Bytes
3f1212d |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# Copyright 2022 Lance Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""COCO: Microsoft COCO Dataset.
https://cocodataset.org/#home
"""
import os
from typing import List
import datasets
import lance
import pyarrow as pa
import pyarrow.compute as pc
_CLASS_MAP = {
1: "person",
2: "bicycle",
3: "car",
4: "motorcycle",
5: "airplane",
6: "bus",
7: "train",
8: "truck",
9: "boat",
10: "traffic light",
11: "fire hydrant",
13: "stop sign",
14: "parking meter",
15: "bench",
16: "bird",
17: "cat",
18: "dog",
19: "horse",
20: "sheep",
21: "cow",
22: "elephant",
23: "bear",
24: "zebra",
25: "giraffe",
27: "backpack",
28: "umbrella",
31: "handbag",
32: "tie",
33: "suitcase",
34: "frisbee",
35: "skis",
36: "snowboard",
37: "sports ball",
38: "kite",
39: "baseball bat",
40: "baseball glove",
41: "skateboard",
42: "surfboard",
43: "tennis racket",
44: "bottle",
46: "wine glass",
47: "cup",
48: "fork",
49: "knife",
50: "spoon",
51: "bowl",
52: "banana",
53: "apple",
54: "sandwich",
55: "orange",
56: "broccoli",
57: "carrot",
58: "hot dog",
59: "pizza",
60: "donut",
61: "cake",
62: "chair",
63: "couch",
64: "potted plant",
65: "bed",
67: "dining table",
70: "toilet",
72: "tv",
73: "laptop",
74: "mouse",
75: "remote",
76: "keyboard",
77: "cell phone",
78: "microwave",
79: "oven",
80: "toaster",
81: "sink",
82: "refrigerator",
84: "book",
85: "clock",
86: "vase",
87: "scissors",
88: "teddy bear",
89: "hair drier",
90: "toothbrush",
}
_DATASET_URI = (
"https://eto-public.s3.us-west-2.amazonaws.com/datasets/coco/coco.lance.tar.gz"
)
class Coco(datasets.ArrowBasedBuilder):
"""COCO: Microsoft common object in context dataset"""
def _info(self):
class_names = []
for i in range(0, max(_CLASS_MAP.keys()) + 1):
class_names.append(_CLASS_MAP.get(i, f"N/A-{i}"))
return datasets.DatasetInfo(
description="COCO: Microsoft object detection dataset",
features=datasets.Features(
{
"image": datasets.Image(),
"split": datasets.Value("string"),
"annotations": datasets.Sequence(
{
"bbox": datasets.Sequence(
datasets.Value("float32"), length=4
),
"category_id": datasets.ClassLabel(names=class_names),
}
),
}
),
supervised_keys=None,
homepage="https://github.com/eto-ai/lance/tree/main/python/benchmarks/coco",
)
def _split_generators(
self, dl_manager: datasets.DownloadManager
) -> List[datasets.SplitGenerator]:
extracted_dir = dl_manager.download_and_extract(_DATASET_URI)
base_uri = os.path.join(extracted_dir, "coco.lance")
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"split": "train", "base_uri": base_uri},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={"split": "val", "base_uri": base_uri},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"split": "test", "base_uri": base_uri},
),
]
def _generate_tables(self, split, base_uri):
idx = 0
dataset = lance.dataset(base_uri)
scanner = dataset.scanner(
filter=pc.field("split") == split,
)
for batch in scanner.to_batches(): # type: pa.RecordBatch
cols = []
names = []
annotations = batch.column("annotations")
if len(annotations) == 0:
continue
cols.append(annotations)
names.append("annotations")
# Decode split because Huggingface does not support dictionary yet.
split_arr = batch.column("split").dictionary_decode()
cols.append(split_arr)
names.append("split")
bytes_arr = batch.column("image").storage
arr = pa.StructArray.from_arrays([bytes_arr], ["bytes"])
cols.append(arr)
names.append("image")
yield idx, pa.Table.from_arrays(cols, names)
idx += 1
|