stained-glass / stained-glass.py
abinthomasonline's picture
Update stained-glass.py
6c1272e
raw
history blame
1.95 kB
"""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),
}