YFCC100M_OpenAI_subset / YFCC100M_OpenAI_subset.py
boris's picture
feat: add loading script
dbaa341
# 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,
}