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