Leyo HF staff commited on
Commit
636ed93
1 Parent(s): 8e0a8dc

create loading script

Browse files
Files changed (1) hide show
  1. ActivityNet_Captions.py +113 -0
ActivityNet_Captions.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import datasets
4
+
5
+ _CITATION = """
6
+ @inproceedings{krishna2017dense,
7
+ title={Dense-Captioning Events in Videos},
8
+ author={Krishna, Ranjay and Hata, Kenji and Ren, Frederic and Fei-Fei, Li and Niebles, Juan Carlos},
9
+ booktitle={International Conference on Computer Vision (ICCV)},
10
+ year={2017}
11
+ }
12
+ """
13
+
14
+ _DESCRIPTION = """\
15
+ The ActivityNet Captions dataset connects videos to a series of temporally annotated sentence descriptions.
16
+ Each sentence covers an unique segment of the video, describing multiple events that occur. These events
17
+ may occur over very long or short periods of time and are not limited in any capacity, allowing them to
18
+ co-occur. On average, each of the 20k videos contains 3.65 temporally localized sentences, resulting in
19
+ a total of 100k sentences. We find that the number of sentences per video follows a relatively normal
20
+ distribution. Furthermore, as the video duration increases, the number of sentences also increases.
21
+ Each sentence has an average length of 13.48 words, which is also normally distributed. You can find more
22
+ details of the dataset under the ActivityNet Captions Dataset section, and under supplementary materials
23
+ in the paper.
24
+ """
25
+
26
+ _URL_BASE = "https://cs.stanford.edu/people/ranjaykrishna/densevid/"
27
+
28
+
29
+ class ActivityNetConfig(datasets.BuilderConfig):
30
+ """BuilderConfig for ActivityNet Captions."""
31
+
32
+ def __init__(self, **kwargs):
33
+ super(ActivityNetConfig, self).__init__(
34
+ version=datasets.Version("2.1.0", ""), **kwargs)
35
+
36
+
37
+ class ActivityNet(datasets.GeneratorBasedBuilder):
38
+
39
+ DEFAULT_CONFIG_NAME = "all"
40
+ BUILDER_CONFIGS = [
41
+ ActivityNetConfig(
42
+ name="all", description="All the ActivityNet Captions dataset"),
43
+ ]
44
+
45
+ def _info(self):
46
+ return datasets.DatasetInfo(
47
+ description=_DESCRIPTION,
48
+ features=datasets.Features(
49
+ {
50
+ "video_id": datasets.Value("string"),
51
+ "video_path": datasets.Value("string"),
52
+ "duration": datasets.Value("float32"),
53
+ "captions_starts": datasets.features.Sequence(datasets.Value("float32")),
54
+ "captions_ends": datasets.features.Sequence(datasets.Value("float32")),
55
+ "en_captions": datasets.features.Sequence(datasets.Value("string"))
56
+ }
57
+ ),
58
+ supervised_keys=None,
59
+ homepage=_URL_BASE,
60
+ citation=_CITATION,
61
+ )
62
+
63
+ def _split_generators(self, dl_manager):
64
+ archive_path = dl_manager.download_and_extract(
65
+ _URL_BASE + "captions.zip")
66
+
67
+ train_splits = [
68
+ datasets.SplitGenerator(
69
+ name=datasets.Split.TRAIN,
70
+ gen_kwargs={
71
+ "infos_file": os.path.join(archive_path, "train.json")
72
+ },
73
+ )
74
+ ]
75
+ dev_splits = [
76
+ datasets.SplitGenerator(
77
+ name=datasets.Split.VALIDATION,
78
+ gen_kwargs={
79
+ "infos_file": os.path.join(archive_path, "val_1.json")
80
+ },
81
+ )
82
+ ]
83
+ test_splits = [
84
+ datasets.SplitGenerator(
85
+ name=datasets.Split.TEST,
86
+ gen_kwargs={
87
+ "infos_file": os.path.join(archive_path, "val_2.json")
88
+ },
89
+ )
90
+ ]
91
+ return train_splits + dev_splits + test_splits
92
+
93
+ def _generate_examples(self, infos_file):
94
+ """This function returns the examples."""
95
+
96
+ with open(infos_file, encoding="utf-8") as json_file:
97
+ infos = json.load(json_file)
98
+ for idx, id in enumerate(infos):
99
+ path = "https://www.youtube.com/watch?v=" + id[2:]
100
+ starts = [float(timestamp[0])
101
+ for timestamp in infos[id]["timestamps"]]
102
+ ends = [float(timestamp[1])
103
+ for timestamp in infos[id]["timestamps"]]
104
+ captions = [str(caption) for caption in infos[id]["sentences"]]
105
+ yield idx, {
106
+ "video_id": id,
107
+ "video_path": path,
108
+ "duration": float(infos[id]["duration"]),
109
+ "captions_starts": starts,
110
+ "captions_ends": ends,
111
+ "en_captions": captions,
112
+ }
113
+