Datasets:
File size: 9,803 Bytes
05a6dc1 923af49 05a6dc1 6846fac 05a6dc1 6846fac 05a6dc1 |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
import json
import os
import datasets
import pandas as pd
from PIL import Image
class ArtelingoBuilderConfig(datasets.BuilderConfig):
def __init__(self, name, splits, **kwargs):
super().__init__(name, **kwargs)
self.splits = splits
# Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@inproceedings{mohamed2022artelingo,
title={ArtELingo: A Million Emotion Annotations of WikiArt with Emphasis on Diversity over Language and Culture},
author={Mohamed, Youssef and Abdelfattah, Mohamed and Alhuwaider, Shyma and Li, Feifan and Zhang, Xiangliang and Church, Kenneth and Elhoseiny, Mohamed},
booktitle={Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing},
pages={8770--8785},
year={2022}
}
"""
# Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
ArtELingo is a benchmark and dataset having a collection of 80,000 artworks from WikiArt with 1.2 Million annotations in English, Arabic, and Chinese.
"""
# Add a link to an official homepage for the dataset here
_HOMEPAGE = "https://www.artelingo.org/"
# Add the licence for the dataset here if you can find it
_LICENSE = "Terms of Use: Before we are able to offer you access to the database, \
please agree to the following terms of use. After approval, you (the 'Researcher') \
receive permission to use the ArtELingo database (the 'Database') at King Abdullah \
University of Science and Technology (KAUST). In exchange for being able to join the \
ArtELingo community and receive such permission, Researcher hereby agrees to the \
following terms and conditions: [1.] The Researcher shall use the Database only for \
non-commercial research and educational purposes. [2.] The Universities make no \
representations or warranties regarding the Database, including but not limited to \
warranties of non-infringement or fitness for a particular purpose. [3.] Researcher \
accepts full responsibility for his or her use of the Database and shall defend and \
indemnify the Universities, including their employees, Trustees, officers and agents, \
against any and all claims arising from Researcher's use of the Database, and \
Researcher's use of any copies of copyrighted 2D artworks originally uploaded to \
http://www.wikiart.org that the Researcher may use in connection with the Database. \
[4.] Researcher may provide research associates and colleagues with access to the \
Database provided that they first agree to be bound by these terms and conditions. \
[5.] The Universities reserve the right to terminate Researcher's access to the Database \
at any time. [6.] If Researcher is employed by a for-profit, commercial entity, \
Researcher's employer shall also be bound by these terms and conditions, and Researcher \
hereby represents that he or she is fully authorized to enter into this agreement on \
behalf of such employer. [7.] The international copyright laws shall apply to all \
disputes under this agreement."
# Add link to the official dataset URLs here
# The HuggingFace dataset library don't host the datasets but only point to the original files
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
# This script can work with local (downloaded) files.
_URLs = {
'val': 'https://artelingo.s3.amazonaws.com/val.zip',
'test': 'https://artelingo.s3.amazonaws.com/test.zip',
'train': 'https://artelingo.s3.amazonaws.com/train.zip',
'wecia-emo_dev': 'https://artelingo.s3.amazonaws.com/wecia_emo_dev.zip',
'wecia-cap_dev': 'https://artelingo.s3.amazonaws.com/wecia_cap_dev.zip',
'wecia-emo_hidden': 'https://artelingo.s3.amazonaws.com/wecia_emo_hidden.zip',
'wecia-cap_hidden': 'https://artelingo.s3.amazonaws.com/wecia_cap_hidden.zip',
}
# _URL_ANN = "https://artelingo.s3.amazonaws.com/artelingo_release_lite.csv"
_EMOTIONS = ['contentment', 'awe', 'amusement', 'excitement', 'sadness', 'fear', 'anger', 'disgust', 'something else']
# Name of the dataset usually match the script name with CamelCase instead of snake_case
class Artelingo(datasets.GeneratorBasedBuilder):
"""An example dataset script to work with ArtELingo dataset"""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
ArtelingoBuilderConfig(name='artelingo', splits=['train', 'val', 'test'],
version=VERSION, description="The full ArtELingo dataset"),
ArtelingoBuilderConfig(name='dev', splits=['val', 'test'],
version=VERSION, description="The Test and Val subsets of ArtELingo"),
ArtelingoBuilderConfig(name='wecia-emo', splits=['dev', 'hidden'],
version=VERSION, description="The Dev set of the WECIA Emotion Prediction challenge"),
ArtelingoBuilderConfig(name='wecia-cap', splits=['dev', 'hidden'],
version=VERSION, description="The Dev set of the WECIA Affective Caption Generation challenge"),
]
DEFAULT_CONFIG_NAME = "artelingo"
def _info(self):
# This method specifies the datasets. DatasetInfo object which contains informations and typings for the dataset
feature_dict = {
"uid": datasets.Value("int32"),
'image': datasets.Image(),
"art_style": datasets.Value("string"),
"painting": datasets.Value("string"),
# "emotion": datasets.ClassLabel(names=_EMOTIONS),
"emotion": datasets.Value("string"),
"language": datasets.Value("string"),
"text": datasets.Value("string"),
}
features = datasets.Features(feature_dict)
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
# This defines the different columns of the dataset and their types
features=features, # Here we define them above because they are different between the two configurations
# If there's a common (input, target) tuple from the features,
# specify them here. They'll be used if as_supervised=True in
# builder.as_dataset.
supervised_keys=None,
# Homepage of the dataset for documentation
homepage=_HOMEPAGE,
# License for the dataset if available
license=_LICENSE,
# Citation for the dataset
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
# This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
data_dir = self.config.data_dir
if data_dir is None:
data_dir = {}
prefix = self.config.name + '_' if 'wecia' in self.config.name else ''
for split in self.config.splits:
data_dir[split] = dl_manager.download_and_extract(_URLs[prefix + split])
# data_dir = dl_manager.download_and_extract(_URLs)
splits = []
for split in self.config.splits:
dataset = datasets.SplitGenerator(
name=split,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"metadata": os.path.join(data_dir[split], split, "metadata.csv"),
"image_dir": os.path.join(data_dir[split], split),
}
)
splits.append(dataset)
return splits
def _generate_examples(
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
self, metadata, image_dir
):
""" Yields examples as (key, example) tuples. """
# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
# The `key` is here for legacy reason (tfds) and is not important in itself.
name = self.config.name
df = pd.read_csv(metadata)
uids = range(len(df))
if name == 'wecia-emo':
for uid, entry in zip(uids, df.itertuples()):
result = {
"uid": entry.uid,
"image": Image.open(os.path.join(image_dir, entry.file_name)),
"art_style": entry.art_style,
"painting": entry.painting,
"text": entry.text,
"emotion": None,
'language': None,
}
yield (uid, result)
elif name == 'wecia-cap':
for uid, entry in zip(uids, df.itertuples()):
result = {
"uid": entry.uid,
"image": Image.open(os.path.join(image_dir, entry.file_name)),
"art_style": entry.art_style,
"painting": entry.painting,
"emotion": entry.emotion,
"language": entry.language,
"text": None,
}
yield (uid, result)
else:
for uid, entry in zip(uids, df.itertuples()):
result = {
"uid": uid,
"image": Image.open(os.path.join(image_dir, entry.file_name)),
"art_style": entry.art_style,
"painting": entry.painting,
"emotion": entry.emotion,
"language": entry.language,
"text": entry.text,
}
yield (uid, result) |