boris commited on
Commit
dbaa341
·
1 Parent(s): 9cfa72f

feat: add loading script

Browse files
Files changed (1) hide show
  1. YFCC100M_OpenAI_subset.py +158 -0
YFCC100M_OpenAI_subset.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """Subset of YFCC100M used by OpenAI for CLIP"""
16
+
17
+
18
+ import zipfile
19
+ import io
20
+ import pandas as pd
21
+ import datasets
22
+
23
+
24
+ _CITATION = """\
25
+ @article{thomee2016yfcc100m,
26
+ author = "Bart Thomee and David A. Shamma and Gerald Friedland and Benjamin Elizalde and Karl Ni and Douglas Poland and Damian Borth and Li-Jia Li",
27
+ title = "{YFCC100M}: The New Data in Multimedia Research",
28
+ journal = "Communications of the {ACM}",
29
+ volume = "59",
30
+ number = "2",
31
+ pages = "64--73",
32
+ year = "2016",
33
+ url = "http://cacm.acm.org/magazines/2016/2/197425-yfcc100m/fulltext",
34
+ }
35
+ """
36
+
37
+ _DESCRIPTION = """\
38
+ The YFCC100M is one of the largest publicly and freely useable multimedia collection, containing the metadata of around 99.2 million photos and 0.8 million videos from Flickr, all of which were shared under one of the various Creative Commons licenses.
39
+
40
+ This version is a subset defined in openai/CLIP.
41
+ """
42
+
43
+ _HOMEPAGE = "https://multimediacommons.wordpress.com/yfcc100m-core-dataset/"
44
+
45
+ _LICENSE = "Use of the original media files is subject to the Creative Commons licenses chosen by their creators/uploaders. License information for each media file can be found within the metadata."
46
+
47
+ _SHARDs = [f"{idx:03x}" for idx in range(1, 4096) if idx != 0xAC8]
48
+
49
+ _URLs = [
50
+ {
51
+ "metadata": f"https://huggingface.co/datasets/dalle-mini/YFCC100M_OpenAI_subset/resolve/main/metadata/metadata_{idx}.jsonl.gz",
52
+ "images": f"https://huggingface.co/datasets/dalle-mini/YFCC100M_OpenAI_subset/resolve/main/data/{idx}.zip",
53
+ }
54
+ for idx in _SHARDs
55
+ ]
56
+
57
+ # easier to treat every item as string (due to null, NA, etc…)
58
+ _FEATURES = datasets.Features(
59
+ {
60
+ "title_clean": datasets.Value("string"),
61
+ "description_clean": datasets.Value("string"),
62
+ "img": datasets.Value("binary"),
63
+ "photoid": datasets.Value("string"),
64
+ "uid": datasets.Value("string"),
65
+ "unickname": datasets.Value("string"),
66
+ "datetaken": datasets.Value("string"),
67
+ "dateuploaded": datasets.Value("string"),
68
+ "capturedevice": datasets.Value("string"),
69
+ "title": datasets.Value("string"),
70
+ "description": datasets.Value("string"),
71
+ "usertags": datasets.Value("string"),
72
+ "machinetags": datasets.Value("string"),
73
+ "longitude": datasets.Value("string"),
74
+ "latitude": datasets.Value("string"),
75
+ "accuracy": datasets.Value("string"),
76
+ "pageurl": datasets.Value("string"),
77
+ "downloadurl": datasets.Value("string"),
78
+ "licensename": datasets.Value("string"),
79
+ "licenseurl": datasets.Value("string"),
80
+ "serverid": datasets.Value("string"),
81
+ "farmid": datasets.Value("string"),
82
+ "secret": datasets.Value("string"),
83
+ "secretoriginal": datasets.Value("string"),
84
+ "ext": datasets.Value("string"),
85
+ "marker": datasets.Value("string"),
86
+ "key": datasets.Value("string"),
87
+ }
88
+ )
89
+
90
+
91
+ class YFCC100M(datasets.GeneratorBasedBuilder):
92
+ """Subset of YFCC100M used by OpenAI for CLIP"""
93
+
94
+ VERSION = datasets.Version("0.0.1")
95
+
96
+ def _info(self):
97
+
98
+ return datasets.DatasetInfo(
99
+ description=_DESCRIPTION,
100
+ features=_FEATURES,
101
+ supervised_keys=None,
102
+ homepage=_HOMEPAGE,
103
+ license=_LICENSE,
104
+ citation=_CITATION,
105
+ )
106
+
107
+ def _split_generators(self, dl_manager):
108
+ """Returns SplitGenerators."""
109
+ URLs = dl_manager.download(_URLs)
110
+ return [
111
+ datasets.SplitGenerator(
112
+ name=datasets.Split.TRAIN,
113
+ # These kwargs will be passed to _generate_examples
114
+ gen_kwargs={
115
+ "data": URLs[:-4],
116
+ },
117
+ ),
118
+ datasets.SplitGenerator(
119
+ name=datasets.Split.VALIDATION,
120
+ # These kwargs will be passed to _generate_examples
121
+ gen_kwargs={
122
+ "data": URLs[-4:],
123
+ },
124
+ ),
125
+ ]
126
+
127
+ def _generate_examples(
128
+ self,
129
+ data,
130
+ ):
131
+ """Yields examples as (key, example) tuples."""
132
+
133
+ id_ = -1
134
+ for chunk in data:
135
+ metadata_json = chunk["metadata"]
136
+ images_zip = chunk["images"]
137
+
138
+ metadata = pd.read_json(
139
+ open(metadata_json, mode="rb"),
140
+ orient="records",
141
+ lines=True,
142
+ compression="gzip",
143
+ )
144
+ with open(images_zip, mode="rb") as f:
145
+ # required for fast reading
146
+ zip_content = io.BytesIO(f.read())
147
+ with zipfile.ZipFile(zip_content) as images:
148
+ for _, row in metadata.iterrows():
149
+ key = row["key"]
150
+ id_ += 1
151
+ with images.open(
152
+ f"data/images/{key[:3]}/{key[3:6]}/{key}.jpg"
153
+ ) as f:
154
+ img = f.read()
155
+ yield id_, {
156
+ **{k: str(v) for k, v in row.items()},
157
+ "img": img,
158
+ }