File size: 1,952 Bytes
02a19c8 cd9a788 02a19c8 61d6165 02a19c8 61d6165 6c1272e 02a19c8 61d6165 02a19c8 |
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 |
"""Dataset class for stained-glass dataset."""
import datasets
import os
import pandas as pd
_DESCRIPTION = """Dataset consists of 21 high-resolution images of stained glass art, accompanied by corresponding captions"""
_HOMEPAGE = """https://huggingface.co/datasets/abinthomasonline/stained-glass"""
_ADJECTIVES = ["", "good", "cropped", "clean", "bright", "cool", "nice", "small", "large", "dark", "weird"]
class StainedGlass(datasets.GeneratorBasedBuilder):
"""Stained Glass Images dataset"""
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"image": datasets.Image(),
"caption": datasets.Value(dtype='string'),
}
),
supervised_keys=("image", "caption"),
homepage=_HOMEPAGE,
)
def _split_generators(self, dl_manager):
captions_path = dl_manager.download("captions.csv")
df = pd.read_csv(captions_path, header=None)
image_paths = {row[0]: os.path.join("images", row[0]) for _, row in df.iterrows()}
image_paths = dl_manager.download(image_paths)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"captions_path": captions_path,
"image_paths": image_paths,
},
)
]
def _generate_examples(self, captions_path, image_paths):
df = pd.read_csv(captions_path, header=None)
captions = {row[0]: row[1].replace('"', '') for _, row in df.iterrows()}
for adjective in _ADJECTIVES:
for key, image_path in image_paths.items():
yield key + adjective, {
"image": image_path,
"caption": captions[key].format(token='<token>', adjective=adjective),
}
|