File size: 5,714 Bytes
dbaa341 |
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 |
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# 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.
"""Subset of YFCC100M used by OpenAI for CLIP"""
import zipfile
import io
import pandas as pd
import datasets
_CITATION = """\
@article{thomee2016yfcc100m,
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",
title = "{YFCC100M}: The New Data in Multimedia Research",
journal = "Communications of the {ACM}",
volume = "59",
number = "2",
pages = "64--73",
year = "2016",
url = "http://cacm.acm.org/magazines/2016/2/197425-yfcc100m/fulltext",
}
"""
_DESCRIPTION = """\
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.
This version is a subset defined in openai/CLIP.
"""
_HOMEPAGE = "https://multimediacommons.wordpress.com/yfcc100m-core-dataset/"
_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."
_SHARDs = [f"{idx:03x}" for idx in range(1, 4096) if idx != 0xAC8]
_URLs = [
{
"metadata": f"https://huggingface.co/datasets/dalle-mini/YFCC100M_OpenAI_subset/resolve/main/metadata/metadata_{idx}.jsonl.gz",
"images": f"https://huggingface.co/datasets/dalle-mini/YFCC100M_OpenAI_subset/resolve/main/data/{idx}.zip",
}
for idx in _SHARDs
]
# easier to treat every item as string (due to null, NA, etc…)
_FEATURES = datasets.Features(
{
"title_clean": datasets.Value("string"),
"description_clean": datasets.Value("string"),
"img": datasets.Value("binary"),
"photoid": datasets.Value("string"),
"uid": datasets.Value("string"),
"unickname": datasets.Value("string"),
"datetaken": datasets.Value("string"),
"dateuploaded": datasets.Value("string"),
"capturedevice": datasets.Value("string"),
"title": datasets.Value("string"),
"description": datasets.Value("string"),
"usertags": datasets.Value("string"),
"machinetags": datasets.Value("string"),
"longitude": datasets.Value("string"),
"latitude": datasets.Value("string"),
"accuracy": datasets.Value("string"),
"pageurl": datasets.Value("string"),
"downloadurl": datasets.Value("string"),
"licensename": datasets.Value("string"),
"licenseurl": datasets.Value("string"),
"serverid": datasets.Value("string"),
"farmid": datasets.Value("string"),
"secret": datasets.Value("string"),
"secretoriginal": datasets.Value("string"),
"ext": datasets.Value("string"),
"marker": datasets.Value("string"),
"key": datasets.Value("string"),
}
)
class YFCC100M(datasets.GeneratorBasedBuilder):
"""Subset of YFCC100M used by OpenAI for CLIP"""
VERSION = datasets.Version("0.0.1")
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=_FEATURES,
supervised_keys=None,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
URLs = dl_manager.download(_URLs)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"data": URLs[:-4],
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"data": URLs[-4:],
},
),
]
def _generate_examples(
self,
data,
):
"""Yields examples as (key, example) tuples."""
id_ = -1
for chunk in data:
metadata_json = chunk["metadata"]
images_zip = chunk["images"]
metadata = pd.read_json(
open(metadata_json, mode="rb"),
orient="records",
lines=True,
compression="gzip",
)
with open(images_zip, mode="rb") as f:
# required for fast reading
zip_content = io.BytesIO(f.read())
with zipfile.ZipFile(zip_content) as images:
for _, row in metadata.iterrows():
key = row["key"]
id_ += 1
with images.open(
f"data/images/{key[:3]}/{key[3:6]}/{key}.jpg"
) as f:
img = f.read()
yield id_, {
**{k: str(v) for k, v in row.items()},
"img": img,
}
|