Datasets:

ArXiv:
License:
SaulLu commited on
Commit
4d0dfd4
1 Parent(s): 0afc661

Add a new configuration in which all the captions associated with an image are listed in a single example (#1)

Browse files

- Added a new configuration in which all the captions associated with an image are listed in a single example (69b2bc7c69c8c030098cd22f66d6abfee7384d94)

Files changed (1) hide show
  1. COCO.py +67 -2
COCO.py CHANGED
@@ -82,6 +82,21 @@ _FEATURES = datasets.Features(
82
  }
83
  )
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  class COCO(datasets.GeneratorBasedBuilder):
87
  """COCO"""
@@ -89,7 +104,14 @@ class COCO(datasets.GeneratorBasedBuilder):
89
  VERSION = datasets.Version("1.0.0")
90
 
91
  BUILDER_CONFIGS = [
92
- datasets.BuilderConfig(name="2014", version=VERSION, description="2014 version of COCO with Karpathy annotations and splits"),
 
 
 
 
 
 
 
93
  ]
94
 
95
  DEFAULT_CONFIG_NAME = "2014"
@@ -97,7 +119,7 @@ class COCO(datasets.GeneratorBasedBuilder):
97
  def _info(self):
98
  return datasets.DatasetInfo(
99
  description=_DESCRIPTION,
100
- features=_FEATURES,
101
  homepage=_HOMEPAGE,
102
  license=_LICENSE,
103
  citation=_CITATION,
@@ -135,6 +157,49 @@ class COCO(datasets.GeneratorBasedBuilder):
135
  ]
136
 
137
  def _generate_examples(self, annotation_file, image_folders, split_key):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  counter = 0
139
  with open(annotation_file, "r", encoding="utf-8") as fi:
140
  annotations = json.load(fi)
 
82
  }
83
  )
84
 
85
+ _FEATURES_CAPTIONS = datasets.Features(
86
+ {
87
+ "image": datasets.Image(),
88
+ "filepath": datasets.Value("string"),
89
+ "sentids": [datasets.Value("int32")],
90
+ "filename": datasets.Value("string"),
91
+ "imgid": datasets.Value("int32"),
92
+ "split": datasets.Value("string"),
93
+ "sentences_tokens": [[datasets.Value("string")]],
94
+ "sentences_raw": [datasets.Value("string")],
95
+ "sentences_sentid": [datasets.Value("int32")],
96
+ "cocoid": datasets.Value("int32"),
97
+ }
98
+ )
99
+
100
 
101
  class COCO(datasets.GeneratorBasedBuilder):
102
  """COCO"""
 
104
  VERSION = datasets.Version("1.0.0")
105
 
106
  BUILDER_CONFIGS = [
107
+ datasets.BuilderConfig(
108
+ name="2014", version=VERSION, description="2014 version of COCO with Karpathy annotations and splits"
109
+ ),
110
+ datasets.BuilderConfig(
111
+ name="2014_captions",
112
+ version=VERSION,
113
+ description="Same as 2014 but with all captions of one image gathered in a single example",
114
+ ),
115
  ]
116
 
117
  DEFAULT_CONFIG_NAME = "2014"
 
119
  def _info(self):
120
  return datasets.DatasetInfo(
121
  description=_DESCRIPTION,
122
+ features=_FEATURES if self.config.name == "2014" else _FEATURES_CAPTIONS,
123
  homepage=_HOMEPAGE,
124
  license=_LICENSE,
125
  citation=_CITATION,
 
157
  ]
158
 
159
  def _generate_examples(self, annotation_file, image_folders, split_key):
160
+ if self.config.name == "2014_captions":
161
+ return self._generate_examples_2014_captions(annotation_file, image_folders, split_key)
162
+ elif self.config.name == "2014":
163
+ return self._generate_examples_2014(annotation_file, image_folders, split_key)
164
+
165
+ def _generate_examples_2014_captions(self, annotation_file, image_folders, split_key):
166
+ with open(annotation_file, "r", encoding="utf-8") as fi:
167
+ annotations = json.load(fi)
168
+
169
+ for image_metadata in annotations["images"]:
170
+ if split_key == "train":
171
+ if image_metadata["split"] != "train" and image_metadata["split"] != "restval":
172
+ continue
173
+ elif split_key == "validation":
174
+ if image_metadata["split"] != "val":
175
+ continue
176
+ elif split_key == "test":
177
+ if image_metadata["split"] != "test":
178
+ continue
179
+
180
+ if "val2014" in image_metadata["filename"]:
181
+ image_path = image_folders["validation"] / _SPLIT_MAP["validation"]
182
+ else:
183
+ image_path = image_folders["train"] / _SPLIT_MAP["train"]
184
+
185
+ image_path = image_path / image_metadata["filename"]
186
+
187
+ record = {
188
+ "image": str(image_path.absolute()),
189
+ "filepath": image_metadata["filename"],
190
+ "sentids": image_metadata["sentids"],
191
+ "filename": image_metadata["filename"],
192
+ "imgid": image_metadata["imgid"],
193
+ "split": image_metadata["split"],
194
+ "cocoid": image_metadata["cocoid"],
195
+ "sentences_tokens": [caption["tokens"] for caption in image_metadata["sentences"]],
196
+ "sentences_raw": [caption["raw"] for caption in image_metadata["sentences"]],
197
+ "sentences_sentid": [caption["sentid"] for caption in image_metadata["sentences"]],
198
+ }
199
+
200
+ yield record["imgid"], record
201
+
202
+ def _generate_examples_2014(self, annotation_file, image_folders, split_key):
203
  counter = 0
204
  with open(annotation_file, "r", encoding="utf-8") as fi:
205
  annotations = json.load(fi)