SNLI-VE / SNLI-VE.py
Leyo's picture
Leyo HF staff
small fixes
54e07e3
"""SNLI-VE loading script."""
import json
import os
import datasets
_CITATION = """\
@article{xie2019visual,
title={Visual Entailment: A Novel Task for Fine-grained Image Understanding},
author={Xie, Ning and Lai, Farley and Doran, Derek and Kadav, Asim},
journal={arXiv preprint arXiv:1901.06706},
year={2019}
}
@article{xie2018visual,
title={Visual Entailment Task for Visually-Grounded Language Learning},
author={Xie, Ning and Lai, Farley and Doran, Derek and Kadav, Asim},
journal={arXiv preprint arXiv:1811.10582},
year={2018}
}
@article{young-etal-2014-image,
title = "From image descriptions to visual denotations: New similarity metrics for semantic inference over event descriptions",
author = "Young, Peter and
Lai, Alice and
Hodosh, Micah and
Hockenmaier, Julia",
journal = "Transactions of the Association for Computational Linguistics",
volume = "2",
year = "2014",
address = "Cambridge, MA",
publisher = "MIT Press",
url = "https://aclanthology.org/Q14-1006",
doi = "10.1162/tacl_a_00166",
pages = "67--78",
abstract = "We propose to use the visual denotations of linguistic expressions (i.e. the set of images they describe) to define novel denotational similarity metrics, which we show to be at least as beneficial as distributional similarities for two tasks that require semantic inference. To compute these denotational similarities, we construct a denotation graph, i.e. a subsumption hierarchy over constituents and their denotations, based on a large corpus of 30K images and 150K descriptive captions.",
}
"""
_DESCRIPTION = """\
SNLI-VE is the dataset proposed for the Visual Entailment (VE) task investigated in Visual Entailment Task for Visually-Grounded Language Learning accpeted to NeurIPS 2018 ViGIL workshop).
SNLI-VE is built on top of SNLI and Flickr30K. The problem that VE is trying to solve is to reason about the relationship between an image premise Pimage and a text hypothesis Htext.
Specifically, given an image as premise, and a natural language sentence as hypothesis, three labels (entailment, neutral and contradiction) are assigned based on the relationship conveyed by the (Pimage, Htext)
entailment holds if there is enough evidence in Pimage to conclude that Htext is true.
contradiction holds if there is enough evidence in Pimage to conclude that Htext is false.
Otherwise, the relationship is neutral, implying the evidence in Pimage is insufficient to draw a conclusion about Htext.
"""
_HOMEPAGE = "https://github.com/necla-ml/SNLI-VE"
_LICENSE = "BSD-3-clause"
_SNLI_VE_URL_BASE = "https://huggingface.co/datasets/HuggingFaceM4/SNLI-VE/resolve/main/"
_SNLI_VE_SPLITS = {
"train": "snli_ve_train.jsonl",
"validation": "snli_ve_dev.jsonl",
"test": "snli_ve_test.jsonl",
}
_FEATURES = datasets.Features(
{
"image": datasets.Image(),
"filename": datasets.Value("string"),
"premise": datasets.Value("string"),
"hypothesis": datasets.Value("string"),
"label": datasets.features.ClassLabel(names=["entailment", "neutral", "contradiction"]),
}
)
class SNLIVE(datasets.GeneratorBasedBuilder):
"""SNLIVE."""
@property
def manual_download_instructions(self):
return """\
You need to go to http://shannon.cs.illinois.edu/DenotationGraph/data/index.html,
and manually download the dataset ("Flickr 30k images."). Once it is completed,
a file named `flickr30k-images.tar.gz` will appear in your Downloads folder
or whichever folder your browser chooses to save files to.
Then, the dataset can be loaded using the following command `datasets.load_dataset("HuggingFaceM4/SNLI-VE", data_dir="<path/to/folder>")`.
"""
_LOCAL_IMAGE_FOLDER_NAME = "flickr30k-images"
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=_FEATURES,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
images_path = os.path.join(
dl_manager.extract(os.path.join(dl_manager.manual_dir, "flickr30k-images.tar.gz")),
self._LOCAL_IMAGE_FOLDER_NAME
)
urls = {
"train": os.path.join(_SNLI_VE_URL_BASE, _SNLI_VE_SPLITS["train"]),
"validation": os.path.join(_SNLI_VE_URL_BASE, _SNLI_VE_SPLITS["validation"]),
"test": os.path.join(_SNLI_VE_URL_BASE, _SNLI_VE_SPLITS["test"]),
}
snli_ve_annotation_path = dl_manager.download_and_extract(urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"snli_ve_annotation_path": snli_ve_annotation_path["train"],
"images_path": images_path
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"snli_ve_annotation_path": snli_ve_annotation_path["validation"],
"images_path": images_path
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"snli_ve_annotation_path": snli_ve_annotation_path["test"],
"images_path": images_path
},
),
]
def _generate_examples(self, snli_ve_annotation_path, images_path):
counter = 0
print(snli_ve_annotation_path)
with open(snli_ve_annotation_path, 'r') as json_file:
for elem in json_file:
elem = json.loads(elem)
img_filename = str(elem["Flickr30K_ID"]) + ".jpg"
assert os.path.exists(os.path.join(images_path, img_filename))
record = {
"image": os.path.join(images_path, img_filename),
"filename": img_filename,
"premise": elem["sentence1"],
"hypothesis": elem["sentence2"],
"label": elem["gold_label"],
}
yield counter, record
counter += 1