File size: 4,422 Bytes
69fab37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcb7e9f
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
import csv
import datasets
import os
import urllib.request

_CITATION = """
@InProceedings{tgif-cvpr2016,
  author = {Li, Yuncheng and Song, Yale and Cao, Liangliang and Tetreault, Joel and Goldberg, Larry and Jaimes, Alejandro and Luo, Jiebo},
  title = "{TGIF: A New Dataset and Benchmark on Animated GIF Description}",
  booktitle = {The IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
  month = {June},
  year = {2016}
}
"""

_DESCRIPTION = """\
The Tumblr GIF (TGIF) dataset contains 100K animated GIFs and 120K sentences describing visual content of the animated GIFs. 
The animated GIFs have been collected from Tumblr, from randomly selected posts published between May and June of 2015. 
We provide the URLs of animated GIFs in this release. The sentences are collected via crowdsourcing, with a carefully designed
annotationinterface that ensures high quality dataset. We provide one sentence per animated GIF for the training and validation splits,
and three sentences per GIF for the test split. The dataset shall be used to evaluate animated GIF/video description techniques.
"""

_URL_BASE = "http://raingo.github.io/TGIF-Release/"

_DL_URL = "https://github.com/raingo/TGIF-Release/archive/master.zip"


class TGIFConfig(datasets.BuilderConfig):
    """BuilderConfig for TGIF."""

    def __init__(self, **kwargs):
        super(TGIFConfig, self).__init__(
            version=datasets.Version("2.1.0", ""), **kwargs)


class TGIF(datasets.GeneratorBasedBuilder):

    DEFAULT_CONFIG_NAME = "all"
    BUILDER_CONFIGS = [
        TGIFConfig(name="all", description="All the TGIF dataset"),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "video_path": datasets.Value("string"),
                    "video_bytes": datasets.Value("large_binary"),
                    "en_global_captions": datasets.features.Sequence(datasets.Value("string"))
                }
            ),
            supervised_keys=None,
            homepage=_URL_BASE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        archive_path = dl_manager.download_and_extract(_DL_URL)
        archive_data_path = os.path.join(
            archive_path, "TGIF-Release-master/data/splits/")
        infos_file = os.path.join(
            archive_path, "TGIF-Release-master/data/tgif-v1.0.tsv")

        train_splits = [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "split_links_file": os.path.join(archive_data_path, "train.txt"),
                    "infos_file": infos_file
                },
            )
        ]
        dev_splits = [
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "split_links_file": os.path.join(archive_data_path, "val.txt"),
                    "infos_file": infos_file
                },
            )
        ]
        test_splits = [
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "split_links_file": os.path.join(archive_data_path, "test.txt"),
                    "infos_file": infos_file
                },
            )
        ]
        return train_splits + dev_splits + test_splits

    def _generate_examples(self, split_links_file, infos_file):
        """This function returns the examples."""

        dict = {}
        with open(split_links_file, encoding="utf-8") as txt_file:
            for line in txt_file:
                line = line[0:-1]
                dict[line] = []
        with open(infos_file, encoding="utf-8") as tsv_file:
            tsv_reader = csv.reader(tsv_file, delimiter="\t", quotechar='"')
            for idx, (video_link, text) in enumerate(tsv_reader):
                try:
                    dict[video_link].append(text)
                except Exception:
                    pass
        for idx, video_link in enumerate(dict):
            video_data = urllib.request.urlopen(video_link).read()
            video_bytes = bytearray(video_data)
            yield idx, {
                "video_path": video_link,
                "video_bytes": video_bytes,
                "en_global_captions": dict[video_link],
            }