File size: 15,748 Bytes
801f63d |
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# coding=utf-8
# Lint as: python3
"""The Caption Contest benchmark."""
import json
import os
import datasets
import base64
import pprint
_CAPTION_CONTEST_TASKS_CITATION = """\
@article{hessel2022androids,
title={Do Androids Laugh at Electric Sheep? Humor" Understanding" Benchmarks from The New Yorker Caption Contest},
author={Hessel, Jack and Marasovi{\'c}, Ana and Hwang, Jena D and Lee, Lillian and Da, Jeff and Zellers, Rowan and Mankoff, Robert and Choi, Yejin},
journal={arXiv preprint arXiv:2209.06293},
year={2022}
}
www.capcon.dev
Our data contributions are:
- The cartoon-level annotations;
- The joke explanations;
- and the framing of the tasks
We release these data we contribute under CC-BY (see DATASET_LICENSE).
If you find this data useful in your work, in addition to citing our contributions, please also cite the following, from which the cartoons/captions in our corpus are derived:
@misc{newyorkernextmldataset,
author={Jain, Lalit and Jamieson, Kevin and Mankoff, Robert and Nowak, Robert and Sievert, Scott},
title={The {N}ew {Y}orker Cartoon Caption Contest Dataset},
year={2020},
url={https://nextml.github.io/caption-contest-data/}
}
@inproceedings{radev-etal-2016-humor,
title = "Humor in Collective Discourse: Unsupervised Funniness Detection in The {New Yorker} Cartoon Caption Contest",
author = "Radev, Dragomir and
Stent, Amanda and
Tetreault, Joel and
Pappu, Aasish and
Iliakopoulou, Aikaterini and
Chanfreau, Agustin and
de Juan, Paloma and
Vallmitjana, Jordi and
Jaimes, Alejandro and
Jha, Rahul and
Mankoff, Robert",
booktitle = "LREC",
year = "2016",
}
@inproceedings{shahaf2015inside,
title={Inside jokes: Identifying humorous cartoon captions},
author={Shahaf, Dafna and Horvitz, Eric and Mankoff, Robert},
booktitle={KDD},
year={2015},
}
"""
_CAPTION_CONTEST_DESCRIPTION = """\
There are 3 caption contest tasks, described in the paper. In the Matching multiple choice task, models must recognize a caption written about a cartoon (vs. options that were not). In the Quality Ranking task, models must evaluate the quality
of that caption by scoring it more highly than a lower quality option from the same contest. In the Explanation Generation task, models must explain why the joke is funny.
"""
_MATCHING_DESCRIPTION = """\
You are given a cartoon and 5 captions. Only one of the captions was truly written about the cartoon. You must select it.
"""
_RANKING_DESCRIPTION = """\
You are given a cartoon and 2 captions. One of the captions was selected by crowd voting or New Yorker editors as high quality. You must select it.
"""
_EXPLANATION_DESCRIPTION = """\
You are given a cartoon and a caption that was written about it. You must autoregressively generate a joke explanation.
"""
_IMAGES_URL = "https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/all_contest_images.zip"
def _get_configs_crossvals():
cross_val_configs = []
for split_idx in [1,2,3,4]:
cur_split_configs = [
CaptionContestConfig(
name='matching_{}'.format(split_idx),
description=_MATCHING_DESCRIPTION,
features=[
'image',
'contest_number',
'image_location',
'image_description',
'image_uncanny_description',
'entities',
'questions',
'caption_choices',
'from_description',
],
label_classes=["A", "B", "C", "D", "E"],
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/matching_{}.zip'.format(split_idx),
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
CaptionContestConfig(
name='matching_from_pixels_{}'.format(split_idx),
description=_MATCHING_DESCRIPTION,
features=[
'image',
'contest_number',
'caption_choices',
],
label_classes=["A", "B", "C", "D", "E"],
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/matching_from_pixels_{}.zip'.format(split_idx),
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
CaptionContestConfig(
name='ranking_{}'.format(split_idx),
description=_RANKING_DESCRIPTION,
features=[
'image',
'contest_number',
'image_location',
'image_description',
'image_uncanny_description',
'entities',
'questions',
'caption_choices',
'from_description',
'winner_source',
],
label_classes=["A", "B"],
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/ranking_{}.zip'.format(split_idx),
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
CaptionContestConfig(
name='ranking_from_pixels_{}'.format(split_idx),
description=_RANKING_DESCRIPTION,
features=[
'image',
'contest_number',
'caption_choices',
'winner_source',
],
label_classes=["A", "B"],
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/ranking_from_pixels_{}.zip'.format(split_idx),
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
CaptionContestConfig(
name='explanation_{}'.format(split_idx),
description=_EXPLANATION_DESCRIPTION,
features=[
'image',
'contest_number',
'image_location',
'image_description',
'image_uncanny_description',
'entities',
'questions',
'caption_choices',
'from_description',
],
label_classes=None,
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/explanation_{}.zip'.format(split_idx),
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
CaptionContestConfig(
name='explanation_from_pixels_{}'.format(split_idx),
description=_EXPLANATION_DESCRIPTION,
features=[
'image',
'contest_number',
'caption_choices',
],
label_classes=None,
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/explanation_from_pixels_{}.zip'.format(split_idx),
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
]
cross_val_configs.extend(cur_split_configs)
return cross_val_configs
class CaptionContestConfig(datasets.BuilderConfig):
"""BuilderConfig for Caption Contest."""
def __init__(self, features, data_url, citation, url, label_classes=None, **kwargs):
"""BuilderConfig for Caption Contest.
Args:
features: `list[string]`, list of the features that will appear in the
feature dict. Should not include "label".
data_url: `string`, url to download the zip file from.
citation: `string`, citation for the data set.
url: `string`, url for information about the data set.
label_classes: `list[string]`, the list of classes for the label if the
label is present as a string. If not provided, there is no fixed label set.
**kwargs: keyword arguments forwarded to super.
"""
super(CaptionContestConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
self.features = features
self.data_url = data_url
self.citation = citation
self.url = url
self.label_classes = label_classes
class CaptionContest(datasets.GeneratorBasedBuilder):
"""The CaptionContest benchmark."""
BUILDER_CONFIGS = [
CaptionContestConfig(
name='matching',
description=_MATCHING_DESCRIPTION,
features=[
'image',
'contest_number',
'image_location',
'image_description',
'image_uncanny_description',
'entities',
'questions',
'caption_choices',
'from_description',
],
label_classes=["A", "B", "C", "D", "E"],
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/matching.zip',
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
CaptionContestConfig(
name='matching_from_pixels',
description=_MATCHING_DESCRIPTION,
features=[
'image',
'contest_number',
'caption_choices',
],
label_classes=["A", "B", "C", "D", "E"],
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/matching_from_pixels.zip',
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
CaptionContestConfig(
name='ranking',
description=_RANKING_DESCRIPTION,
features=[
'image',
'contest_number',
'image_location',
'image_description',
'image_uncanny_description',
'entities',
'questions',
'caption_choices',
'from_description',
'winner_source',
],
label_classes=["A", "B"],
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/ranking.zip',
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
CaptionContestConfig(
name='ranking_from_pixels',
description=_RANKING_DESCRIPTION,
features=[
'image',
'contest_number',
'caption_choices',
'winner_source',
],
label_classes=["A", "B"],
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/ranking_from_pixels.zip',
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
CaptionContestConfig(
name='explanation',
description=_EXPLANATION_DESCRIPTION,
features=[
'image',
'contest_number',
'image_location',
'image_description',
'image_uncanny_description',
'entities',
'questions',
'caption_choices',
'from_description',
],
label_classes=None,
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/explanation.zip',
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
CaptionContestConfig(
name='explanation_from_pixels',
description=_EXPLANATION_DESCRIPTION,
features=[
'image',
'contest_number',
'caption_choices',
],
label_classes=None,
data_url='https://storage.googleapis.com/ai2-jack-public/caption_contest_data_public/huggingface_hub/v1.0/explanation_from_pixels.zip',
url='www.capcon.dev',
citation=_CAPTION_CONTEST_TASKS_CITATION,
),
] + _get_configs_crossvals()
def _info(self):
features = {feature: datasets.Value("string") for feature in self.config.features}
# things are strings except for contest_number, entities, questions, and caption choices (if not explanation)
features['contest_number'] = datasets.Value("int32")
if 'explanation' not in self.config.name:
features['caption_choices'] = datasets.features.Sequence(datasets.Value("string"))
if 'entities' in features:
features['entities'] = datasets.features.Sequence(datasets.Value("string"))
if 'questions' in features:
features['questions'] = datasets.features.Sequence(datasets.Value("string"))
if 'image' in features:
features['image'] = datasets.Image()
features['label'] = datasets.Value("string")
features['n_tokens_label'] = datasets.Value("int32")
features['instance_id'] = datasets.Value("string")
return datasets.DatasetInfo(
description=_CAPTION_CONTEST_DESCRIPTION + self.config.description,
features=datasets.Features(features),
homepage=self.config.url,
citation=self.config.citation
)
def _split_generators(self, dl_manager):
dl_dir = dl_manager.download_and_extract(self.config.data_url) or ""
self.images_dir = dl_manager.download_and_extract(_IMAGES_URL)
task_name = _get_task_name_from_data_url(self.config.data_url)
dl_dir = os.path.join(dl_dir, task_name)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"data_file": os.path.join(dl_dir, "train.jsonl"),
"split": datasets.Split.TRAIN,
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"data_file": os.path.join(dl_dir, "val.jsonl"),
"split": datasets.Split.VALIDATION,
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"data_file": os.path.join(dl_dir, "test.jsonl"),
"split": datasets.Split.TEST,
},
),
]
def _generate_examples(self, data_file, split):
with open(data_file, encoding="utf-8") as f:
for line in f:
row = json.loads(line)
with open(self.images_dir + "/all_contest_images/{}.jpeg".format(row['contest_number']), "rb") as image:
row['image'] = {"path": self.images_dir + "/all_contest_images/{}.jpeg".format(row['contest_number']),
"bytes": image.read()}
yield row['instance_id'], row
def _get_task_name_from_data_url(data_url):
return data_url.split("/")[-1].split(".")[0]
|