ydshieh
commited on
Commit
•
3fbabfa
1
Parent(s):
e1e7da9
add files
Browse files- coco_dataset.py +206 -0
- download_coco.txt +12 -0
- dummy_data/annotations_trainval2017.zip +3 -0
- dummy_data/image_info_test2017.zip +3 -0
- dummy_data/test2017.zip +3 -0
- dummy_data/train2017.zip +3 -0
- dummy_data/val2017.zip +3 -0
coco_dataset.py
ADDED
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import datasets
|
4 |
+
|
5 |
+
|
6 |
+
class COCOBuilderConfig(datasets.BuilderConfig):
|
7 |
+
|
8 |
+
def __init__(self, name, splits, **kwargs):
|
9 |
+
super().__init__(name, **kwargs)
|
10 |
+
self.splits = splits
|
11 |
+
|
12 |
+
|
13 |
+
# Add BibTeX citation
|
14 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
15 |
+
_CITATION = """\
|
16 |
+
@article{DBLP:journals/corr/LinMBHPRDZ14,
|
17 |
+
author = {Tsung{-}Yi Lin and
|
18 |
+
Michael Maire and
|
19 |
+
Serge J. Belongie and
|
20 |
+
Lubomir D. Bourdev and
|
21 |
+
Ross B. Girshick and
|
22 |
+
James Hays and
|
23 |
+
Pietro Perona and
|
24 |
+
Deva Ramanan and
|
25 |
+
Piotr Doll{'{a} }r and
|
26 |
+
C. Lawrence Zitnick},
|
27 |
+
title = {Microsoft {COCO:} Common Objects in Context},
|
28 |
+
journal = {CoRR},
|
29 |
+
volume = {abs/1405.0312},
|
30 |
+
year = {2014},
|
31 |
+
url = {http://arxiv.org/abs/1405.0312},
|
32 |
+
archivePrefix = {arXiv},
|
33 |
+
eprint = {1405.0312},
|
34 |
+
timestamp = {Mon, 13 Aug 2018 16:48:13 +0200},
|
35 |
+
biburl = {https://dblp.org/rec/bib/journals/corr/LinMBHPRDZ14},
|
36 |
+
bibsource = {dblp computer science bibliography, https://dblp.org}
|
37 |
+
}
|
38 |
+
"""
|
39 |
+
|
40 |
+
# Add description of the dataset here
|
41 |
+
# You can copy an official description
|
42 |
+
_DESCRIPTION = """\
|
43 |
+
COCO is a large-scale object detection, segmentation, and captioning dataset.
|
44 |
+
"""
|
45 |
+
|
46 |
+
# Add a link to an official homepage for the dataset here
|
47 |
+
_HOMEPAGE = "http://cocodataset.org/#home"
|
48 |
+
|
49 |
+
# Add the licence for the dataset here if you can find it
|
50 |
+
_LICENSE = ""
|
51 |
+
|
52 |
+
# Add link to the official dataset URLs here
|
53 |
+
# The HuggingFace dataset library don't host the datasets but only point to the original files
|
54 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
55 |
+
|
56 |
+
# This script is supposed to work with local (downloaded) COCO dataset.
|
57 |
+
_URLs = {}
|
58 |
+
|
59 |
+
|
60 |
+
# Name of the dataset usually match the script name with CamelCase instead of snake_case
|
61 |
+
class COCODataset(datasets.GeneratorBasedBuilder):
|
62 |
+
"""An example dataset script to work with the local (downloaded) COCO dataset"""
|
63 |
+
|
64 |
+
VERSION = datasets.Version("0.0.0")
|
65 |
+
|
66 |
+
BUILDER_CONFIG_CLASS = COCOBuilderConfig
|
67 |
+
BUILDER_CONFIGS = [
|
68 |
+
COCOBuilderConfig(name='2017', splits=['train', 'valid', 'test']),
|
69 |
+
]
|
70 |
+
DEFAULT_CONFIG_NAME = "2017"
|
71 |
+
|
72 |
+
def _info(self):
|
73 |
+
# This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
|
74 |
+
|
75 |
+
feature_dict = {
|
76 |
+
"image_id": datasets.Value("int64"),
|
77 |
+
"caption_id": datasets.Value("int64"),
|
78 |
+
"caption": datasets.Value("string"),
|
79 |
+
"height": datasets.Value("int64"),
|
80 |
+
"width": datasets.Value("int64"),
|
81 |
+
"file_name": datasets.Value("string"),
|
82 |
+
"coco_url": datasets.Value("string"),
|
83 |
+
"image_path": datasets.Value("string"),
|
84 |
+
}
|
85 |
+
|
86 |
+
features = datasets.Features(feature_dict)
|
87 |
+
|
88 |
+
return datasets.DatasetInfo(
|
89 |
+
# This is the description that will appear on the datasets page.
|
90 |
+
description=_DESCRIPTION,
|
91 |
+
# This defines the different columns of the dataset and their types
|
92 |
+
features=features, # Here we define them above because they are different between the two configurations
|
93 |
+
# If there's a common (input, target) tuple from the features,
|
94 |
+
# specify them here. They'll be used if as_supervised=True in
|
95 |
+
# builder.as_dataset.
|
96 |
+
supervised_keys=None,
|
97 |
+
# Homepage of the dataset for documentation
|
98 |
+
homepage=_HOMEPAGE,
|
99 |
+
# License for the dataset if available
|
100 |
+
license=_LICENSE,
|
101 |
+
# Citation for the dataset
|
102 |
+
citation=_CITATION,
|
103 |
+
)
|
104 |
+
|
105 |
+
def _split_generators(self, dl_manager):
|
106 |
+
"""Returns SplitGenerators."""
|
107 |
+
# This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
108 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
109 |
+
|
110 |
+
data_dir = self.config.data_dir
|
111 |
+
if not data_dir:
|
112 |
+
raise ValueError(
|
113 |
+
"This script is supposed to work with local (downloaded) COCO dataset. The argument `data_dir` in `load_dataset()` is required."
|
114 |
+
)
|
115 |
+
|
116 |
+
splits = []
|
117 |
+
for split in self.config.splits:
|
118 |
+
if split == 'train':
|
119 |
+
dataset = datasets.SplitGenerator(
|
120 |
+
name=datasets.Split.TRAIN,
|
121 |
+
# These kwargs will be passed to _generate_examples
|
122 |
+
gen_kwargs={
|
123 |
+
"json_path": os.path.join(data_dir, f"captions_train{self.config.name}.json"),
|
124 |
+
"image_dir": os.path.join(data_dir, f'train{self.config.name}'),
|
125 |
+
"split": "train",
|
126 |
+
}
|
127 |
+
)
|
128 |
+
elif split in ['val', 'valid', 'validation', 'dev']:
|
129 |
+
dataset = datasets.SplitGenerator(
|
130 |
+
name=datasets.Split.VALIDATION,
|
131 |
+
# These kwargs will be passed to _generate_examples
|
132 |
+
gen_kwargs={
|
133 |
+
"json_path": os.path.join(data_dir, f"captions_val{self.config.name}.json"),
|
134 |
+
"image_dir": os.path.join(data_dir, f'val{self.config.name}'),
|
135 |
+
"split": "valid",
|
136 |
+
},
|
137 |
+
)
|
138 |
+
elif split == 'test':
|
139 |
+
dataset = datasets.SplitGenerator(
|
140 |
+
name=datasets.Split.TEST,
|
141 |
+
# These kwargs will be passed to _generate_examples
|
142 |
+
gen_kwargs={
|
143 |
+
"json_path": os.path.join(data_dir, f'image_info_test{self.config.name}.json'),
|
144 |
+
"image_dir": os.path.join(data_dir, f'test{self.config.name}'),
|
145 |
+
"split": "test",
|
146 |
+
},
|
147 |
+
)
|
148 |
+
else:
|
149 |
+
continue
|
150 |
+
|
151 |
+
splits.append(dataset)
|
152 |
+
|
153 |
+
return splits
|
154 |
+
|
155 |
+
def _generate_examples(
|
156 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
157 |
+
self, json_path, image_dir, split
|
158 |
+
):
|
159 |
+
""" Yields examples as (key, example) tuples. """
|
160 |
+
# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
161 |
+
# The `key` is here for legacy reason (tfds) and is not important in itself.
|
162 |
+
|
163 |
+
_features = ["image_id", "caption_id", "caption", "height", "width", "file_name", "coco_url", "image_path", "id"]
|
164 |
+
features = list(_features)
|
165 |
+
|
166 |
+
if split in "valid":
|
167 |
+
split = "val"
|
168 |
+
|
169 |
+
with open(json_path, 'r', encoding='UTF-8') as fp:
|
170 |
+
data = json.load(fp)
|
171 |
+
|
172 |
+
# list of dict
|
173 |
+
images = data["images"]
|
174 |
+
entries = images
|
175 |
+
|
176 |
+
# build a dict of image_id -> image info dict
|
177 |
+
d = {image["id"]: image for image in images}
|
178 |
+
|
179 |
+
# list of dict
|
180 |
+
if split in ["train", "val"]:
|
181 |
+
annotations = data["annotations"]
|
182 |
+
|
183 |
+
# build a dict of image_id ->
|
184 |
+
for annotation in annotations:
|
185 |
+
_id = annotation["id"]
|
186 |
+
image_info = d[annotation["image_id"]]
|
187 |
+
annotation.update(image_info)
|
188 |
+
annotation["id"] = _id
|
189 |
+
|
190 |
+
entries = annotations
|
191 |
+
|
192 |
+
for id_, entry in enumerate(entries):
|
193 |
+
|
194 |
+
entry = {k: v for k, v in entry.items() if k in features}
|
195 |
+
|
196 |
+
if split == "test":
|
197 |
+
entry["image_id"] = entry["id"]
|
198 |
+
entry["id"] = -1
|
199 |
+
entry["caption"] = -1
|
200 |
+
|
201 |
+
entry["caption_id"] = entry.pop("id")
|
202 |
+
entry["image_path"] = os.path.join(image_dir, entry["file_name"])
|
203 |
+
|
204 |
+
entry = {k: entry[k] for k in _features if k in entry}
|
205 |
+
|
206 |
+
yield str((entry["image_id"], entry["caption_id"])), entry
|
download_coco.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
mkdir data
|
2 |
+
cd data
|
3 |
+
wget http://images.cocodataset.org/zips/train2017.zip
|
4 |
+
wget http://images.cocodataset.org/zips/val2017.zip
|
5 |
+
wget http://images.cocodataset.org/zips/test2017.zip
|
6 |
+
wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip
|
7 |
+
wget http://images.cocodataset.org/annotations/image_info_test2017.zip
|
8 |
+
unzip train2017.zip
|
9 |
+
unzip val2017.zip
|
10 |
+
unzip test2017.zip
|
11 |
+
unzip annotations_trainval2017.zip
|
12 |
+
unzip image_info_test2017.zip
|
dummy_data/annotations_trainval2017.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:973b53d5b4748ff251816b6d741dcf19c9a94bfa5b38edcf6164592ebd610476
|
3 |
+
size 7615
|
dummy_data/image_info_test2017.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a794a2b342055f4c22d6d7f0a3e29a8b8ec9ffa8b8611b24bd62b49cd84d3ac3
|
3 |
+
size 3682
|
dummy_data/test2017.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:abdc12551acb154ed65c38f493ce930ad22fffd74c1b1e1f811a46212eb28e91
|
3 |
+
size 3023535
|
dummy_data/train2017.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:370a953a7907709727f53c989d8282a1c0e47fb6fc0900cebf1c67ba362c6e73
|
3 |
+
size 3590323
|
dummy_data/val2017.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c3625319b0adc788f918dea4a32a3c551323fd883888e8a03668cb47dd823b94
|
3 |
+
size 2556555
|