|
|
|
import os |
|
import datasets |
|
import joblib |
|
from pathlib import Path |
|
from tqdm import tqdm |
|
|
|
|
|
_BASE_HF_URL = Path("./data") |
|
_CITATION = "" |
|
_HOMEPAGE = "" |
|
_DESCRIPTION = "" |
|
_DATA_URL = { |
|
"train": [_BASE_HF_URL/"images.tar.gz"] |
|
} |
|
|
|
|
|
class AVA(datasets.GeneratorBasedBuilder): |
|
VERSION = datasets.Version("1.0.0") |
|
|
|
DEFAULT_WRITER_BATCH_SIZE = 1000 |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"image": datasets.Image(), |
|
"rating_counts": datasets.features.Sequence(datasets.Value("int32")), |
|
"text_tag_0": datasets.Value("string"), |
|
"text_tag_1": datasets.Value("string") |
|
} |
|
), |
|
homepage=_HOMEPAGE, |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
archives = dl_manager.download(_DATA_URL) |
|
self.dict_metadata = joblib.load(Path(dl_manager.download_and_extract(_BASE_HF_URL/ "metadata.pkl"))) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"archives": [dl_manager.iter_archive(archive) for archive in archives["train"]], |
|
"split": "train", |
|
}, |
|
) |
|
] |
|
|
|
def _generate_examples(self, archives, split): |
|
"""Yields examples.""" |
|
idx = 0 |
|
for archive in archives: |
|
for path, file in tqdm(archive): |
|
if path.endswith(".jpg"): |
|
|
|
_id = int(os.path.splitext(path)[0].split('/')[-1]) |
|
_metadata = self.dict_metadata[_id] |
|
ex = {"image": {"path": path, "bytes": file.read()}, |
|
"rating_counts": _metadata[0], |
|
"text_tag0":_metadata[1], |
|
"text_tag1": _metadata[2]} |
|
yield idx, ex |
|
idx += 1 |
|
|
|
|
|
|