Datasets:
Tasks:
Feature Extraction
Languages:
Korean
Multilinguality:
monolingual
Size Categories:
1M<n<10M
Language Creators:
crowdsourced
Annotations Creators:
crowdsourced
Source Datasets:
extended|laion/laion2B-multi
Tags:
License:
import json | |
from collections import defaultdict | |
from pathlib import Path | |
from datasets import ( | |
DatasetInfo, | |
DownloadManager, | |
Features, | |
GeneratorBasedBuilder, | |
Image, | |
Split, | |
SplitGenerator, | |
Value, | |
) | |
_URLS = [f"data/{i:0>5}.tar" for i in range(2123)] | |
class Laion2bMultiKoreanSubsetWithImage(GeneratorBasedBuilder): | |
VERSION = "0.1.3" | |
def _info(self): | |
return DatasetInfo( | |
description="laion2b_multi_korean_subset data with images", | |
features=Features( | |
{ | |
"image": Image(), | |
"text": Value("string"), | |
"width": Value("int32"), | |
"height": Value("int32"), | |
} | |
), | |
supervised_keys=None, | |
license="cc-by-4.0", | |
version=self.VERSION, | |
) | |
def _split_generators(self, dl_manager: DownloadManager): | |
downloaded = dl_manager.download(_URLS) | |
return [ | |
SplitGenerator( | |
name=Split.TRAIN, | |
gen_kwargs={ | |
"archives": [ | |
dl_manager.iter_archive(archive) for archive in downloaded | |
] | |
}, | |
) | |
] | |
def _generate_examples(self, archives): | |
idx = 0 | |
temp = defaultdict(dict) | |
for archive in archives: | |
iter_archive = iter(archive) | |
while True: | |
# for tarfile.ReadError | |
try: | |
path, file = next(iter_archive) | |
except Exception: | |
break | |
if path.endswith(".txt"): | |
continue | |
file_id = Path(path).stem | |
if path.endswith(".json"): | |
try: | |
meta = json.loads(file.read()) | |
except Exception: | |
continue | |
temp[file_id]["text"] = meta["caption"] | |
temp[file_id]["width"] = meta["width"] | |
temp[file_id]["height"] = meta["height"] | |
if "image" in temp[file_id]: | |
yield idx, temp.pop(file_id) | |
idx += 1 | |
elif path.endswith(".webp"): | |
try: | |
temp[file_id]["image"] = {"path": path, "bytes": file.read()} | |
except Exception: | |
continue | |
if "text" in temp[file_id]: | |
yield idx, temp.pop(file_id) | |
idx += 1 | |
temp.clear() | |