Datasets:
create loading script
Browse files
TGIF.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import datasets
|
| 3 |
+
import os
|
| 4 |
+
import urllib.request
|
| 5 |
+
|
| 6 |
+
_CITATION = """
|
| 7 |
+
@InProceedings{tgif-cvpr2016,
|
| 8 |
+
author = {Li, Yuncheng and Song, Yale and Cao, Liangliang and Tetreault, Joel and Goldberg, Larry and Jaimes, Alejandro and Luo, Jiebo},
|
| 9 |
+
title = "{TGIF: A New Dataset and Benchmark on Animated GIF Description}",
|
| 10 |
+
booktitle = {The IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
|
| 11 |
+
month = {June},
|
| 12 |
+
year = {2016}
|
| 13 |
+
}
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
_DESCRIPTION = """\
|
| 17 |
+
The Tumblr GIF (TGIF) dataset contains 100K animated GIFs and 120K sentences describing visual content of the animated GIFs.
|
| 18 |
+
The animated GIFs have been collected from Tumblr, from randomly selected posts published between May and June of 2015.
|
| 19 |
+
We provide the URLs of animated GIFs in this release. The sentences are collected via crowdsourcing, with a carefully designed
|
| 20 |
+
annotationinterface that ensures high quality dataset. We provide one sentence per animated GIF for the training and validation splits,
|
| 21 |
+
and three sentences per GIF for the test split. The dataset shall be used to evaluate animated GIF/video description techniques.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
_URL_BASE = "http://raingo.github.io/TGIF-Release/"
|
| 25 |
+
|
| 26 |
+
_DL_URL = "https://github.com/raingo/TGIF-Release/archive/master.zip"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class TGIFConfig(datasets.BuilderConfig):
|
| 30 |
+
"""BuilderConfig for TGIF."""
|
| 31 |
+
|
| 32 |
+
def __init__(self, **kwargs):
|
| 33 |
+
super(TGIFConfig, self).__init__(
|
| 34 |
+
version=datasets.Version("2.1.0", ""), **kwargs)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class TGIF(datasets.GeneratorBasedBuilder):
|
| 38 |
+
|
| 39 |
+
DEFAULT_CONFIG_NAME = "all"
|
| 40 |
+
BUILDER_CONFIGS = [
|
| 41 |
+
TGIFConfig(name="all", description="All the TGIF dataset"),
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
def _info(self):
|
| 45 |
+
return datasets.DatasetInfo(
|
| 46 |
+
description=_DESCRIPTION,
|
| 47 |
+
features=datasets.Features(
|
| 48 |
+
{
|
| 49 |
+
"video_path": datasets.Value("string"),
|
| 50 |
+
"video_bytes": datasets.Value("large_binary"),
|
| 51 |
+
"en_global_captions": datasets.features.Sequence(datasets.Value("string"))
|
| 52 |
+
}
|
| 53 |
+
),
|
| 54 |
+
supervised_keys=None,
|
| 55 |
+
homepage=_URL_BASE,
|
| 56 |
+
citation=_CITATION,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
def _split_generators(self, dl_manager):
|
| 60 |
+
archive_path = dl_manager.download_and_extract(_DL_URL)
|
| 61 |
+
archive_data_path = os.path.join(
|
| 62 |
+
archive_path, "TGIF-Release-master/data/splits/")
|
| 63 |
+
infos_file = os.path.join(
|
| 64 |
+
archive_path, "TGIF-Release-master/data/tgif-v1.0.tsv")
|
| 65 |
+
|
| 66 |
+
train_splits = [
|
| 67 |
+
datasets.SplitGenerator(
|
| 68 |
+
name=datasets.Split.TRAIN,
|
| 69 |
+
gen_kwargs={
|
| 70 |
+
"split_links_file": os.path.join(archive_data_path, "train.txt"),
|
| 71 |
+
"infos_file": infos_file
|
| 72 |
+
},
|
| 73 |
+
)
|
| 74 |
+
]
|
| 75 |
+
dev_splits = [
|
| 76 |
+
datasets.SplitGenerator(
|
| 77 |
+
name=datasets.Split.VALIDATION,
|
| 78 |
+
gen_kwargs={
|
| 79 |
+
"split_links_file": os.path.join(archive_data_path, "val.txt"),
|
| 80 |
+
"infos_file": infos_file
|
| 81 |
+
},
|
| 82 |
+
)
|
| 83 |
+
]
|
| 84 |
+
test_splits = [
|
| 85 |
+
datasets.SplitGenerator(
|
| 86 |
+
name=datasets.Split.TEST,
|
| 87 |
+
gen_kwargs={
|
| 88 |
+
"split_links_file": os.path.join(archive_data_path, "test.txt"),
|
| 89 |
+
"infos_file": infos_file
|
| 90 |
+
},
|
| 91 |
+
)
|
| 92 |
+
]
|
| 93 |
+
return train_splits + dev_splits + test_splits
|
| 94 |
+
|
| 95 |
+
def _generate_examples(self, split_links_file, infos_file):
|
| 96 |
+
"""This function returns the examples."""
|
| 97 |
+
|
| 98 |
+
dict = {}
|
| 99 |
+
with open(split_links_file, encoding="utf-8") as txt_file:
|
| 100 |
+
for line in txt_file:
|
| 101 |
+
line = line[0:-1]
|
| 102 |
+
dict[line] = []
|
| 103 |
+
with open(infos_file, encoding="utf-8") as tsv_file:
|
| 104 |
+
tsv_reader = csv.reader(tsv_file, delimiter="\t", quotechar='"')
|
| 105 |
+
for idx, (video_link, text) in enumerate(tsv_reader):
|
| 106 |
+
try:
|
| 107 |
+
dict[video_link].append(text)
|
| 108 |
+
except Exception:
|
| 109 |
+
pass
|
| 110 |
+
for idx, video_link in enumerate(dict):
|
| 111 |
+
video_data = urllib.request.urlopen(video_link).read()
|
| 112 |
+
video_bytes = bytearray(video_data)
|
| 113 |
+
yield idx, {
|
| 114 |
+
"video_path": video_link,
|
| 115 |
+
"video_bytes": video_bytes,
|
| 116 |
+
"en_global_captions": dict[video_link],
|
| 117 |
+
}
|