VictorSanh commited on
Commit
6f2d732
·
1 Parent(s): 6bdb36f
Files changed (1) hide show
  1. flickr30k.py +126 -0
flickr30k.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """Flickr 30k"""
16
+ import json
17
+ import os
18
+ import datasets
19
+
20
+
21
+ _CITATION = """
22
+ @article{young-etal-2014-image,
23
+ title = "From image descriptions to visual denotations: New similarity metrics for semantic inference over event descriptions",
24
+ author = "Young, Peter and
25
+ Lai, Alice and
26
+ Hodosh, Micah and
27
+ Hockenmaier, Julia",
28
+ journal = "Transactions of the Association for Computational Linguistics",
29
+ volume = "2",
30
+ year = "2014",
31
+ address = "Cambridge, MA",
32
+ publisher = "MIT Press",
33
+ url = "https://aclanthology.org/Q14-1006",
34
+ doi = "10.1162/tacl_a_00166",
35
+ pages = "67--78",
36
+ 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.",
37
+ }
38
+ """
39
+
40
+ # TODO: Victor
41
+ _DESCRIPTION = """"""
42
+
43
+ _HOMEPAGE = "https://shannon.cs.illinois.edu/DenotationGraph/"
44
+
45
+ # TODO: Victor
46
+ _LICENSE = ""
47
+
48
+ _ANNOTATION_URL = "http://cs.stanford.edu/people/karpathy/deepimagesent/caption_datasets.zip"
49
+
50
+ _FEATURES = datasets.Features(
51
+ {
52
+ "image": datasets.Image(),
53
+ "filename": datasets.Value("string"),
54
+ "imgid": datasets.Value("int32"),
55
+ "sentids": [datasets.Value("int32")],
56
+ "sentences": [
57
+ {
58
+ "tokens": [datasets.Value("string")],
59
+ "raw": datasets.Value("string"),
60
+ "imgid": datasets.Value("int32"),
61
+ "sentid": datasets.Value("int32")
62
+ }
63
+ ]
64
+ }
65
+ )
66
+
67
+
68
+ class Flickr30k(datasets.GeneratorBasedBuilder):
69
+ """Flick30k."""
70
+
71
+ @property
72
+ def manual_download_instructions(self):
73
+ return """\
74
+ You need to go to http://shannon.cs.illinois.edu/DenotationGraph/data/index.html,
75
+ and manually download the dataset ("Flickr 30k images."). Once it is completed,
76
+ a file named `flickr30k-images.tar.gz` will appear in your Downloads folder
77
+ or whichever folder your browser chooses to save files to.
78
+ Then, the dataset can be loaded using the following command `datasets.load_dataset("flickr30k", data_dir="<path/to/folder>")`.
79
+ """
80
+
81
+ def _info(self):
82
+ return datasets.DatasetInfo(
83
+ description=_DESCRIPTION,
84
+ features=_FEATURES,
85
+ homepage=_HOMEPAGE,
86
+ license=_LICENSE,
87
+ citation=_CITATION,
88
+ )
89
+
90
+ def _split_generators(self, dl_manager):
91
+ annotations_zip = dl_manager.download_and_extract(_ANNOTATION_URL)
92
+ annotation_path = os.path.join(annotations_zip, "dataset_flickr30k.json")
93
+ images_path = os.path.join(
94
+ dl_manager.extract(os.path.join(dl_manager.manual_dir, "flickr30k-images.tar.gz")),
95
+ "flickr30k-images"
96
+ )
97
+
98
+ return [
99
+ datasets.SplitGenerator(
100
+ name=split,
101
+ gen_kwargs={"annotation_path": annotation_path, "images_path": images_path, "split_name": name},
102
+ )
103
+ for (split, name) in [
104
+ (datasets.Split.TRAIN, "train"),
105
+ (datasets.Split.VALIDATION, "val"),
106
+ (datasets.Split.TEST, "test")
107
+ ]
108
+ ]
109
+
110
+ def _generate_examples(self, annotation_path, images_path, split_name):
111
+ counter = 0
112
+ print(annotation_path)
113
+ with open(annotation_path, "r", encoding="utf-8") as f:
114
+ data = json.load(f)
115
+ for elem in data["images"]:
116
+ if elem["split"] != split_name:
117
+ continue
118
+ assert os.path.exists(os.path.join(images_path, elem["filename"]))
119
+ yield counter, {
120
+ "image": os.path.join(images_path, elem["filename"]),
121
+ "filename": elem["filename"],
122
+ "imgid": elem["imgid"],
123
+ "sentids": elem["sentids"],
124
+ "sentences": elem["sentences"],
125
+ }
126
+ counter += 1