Upload flickr8k-dataset.py
Browse files- flickr8k-dataset.py +112 -0
flickr8k-dataset.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
import re
|
3 |
+
from pathlib import Path
|
4 |
+
|
5 |
+
|
6 |
+
class Flickr8kDataset(datasets.GeneratorBasedBuilder):
|
7 |
+
VERSION = datasets.Version("0.0.0")
|
8 |
+
BUILDER_CONFIGS = [
|
9 |
+
datasets.BuilderConfig(
|
10 |
+
name="flickr8k_dataset",
|
11 |
+
version=VERSION,
|
12 |
+
),
|
13 |
+
]
|
14 |
+
|
15 |
+
def _info(self):
|
16 |
+
feature_dict = {
|
17 |
+
"image_id": datasets.Value("string"),
|
18 |
+
"image_path": datasets.Value("string"),
|
19 |
+
"captions": datasets.Sequence(datasets.Value("string")),
|
20 |
+
}
|
21 |
+
return datasets.DatasetInfo(
|
22 |
+
description="Flickr8k dataset",
|
23 |
+
features=datasets.Features(feature_dict),
|
24 |
+
)
|
25 |
+
|
26 |
+
def _split_generators(self, dl_manager):
|
27 |
+
data_dir = self.config.data_dir
|
28 |
+
if not data_dir:
|
29 |
+
raise ValueError("'data_dir' argument is required.")
|
30 |
+
|
31 |
+
archive_path = dl_manager.extract(
|
32 |
+
{
|
33 |
+
"images": Path(data_dir).joinpath("Flickr8k_Dataset.zip"),
|
34 |
+
"texts": Path(data_dir).joinpath("Flickr8k_text.zip"),
|
35 |
+
}
|
36 |
+
)
|
37 |
+
|
38 |
+
splits = [
|
39 |
+
datasets.SplitGenerator(
|
40 |
+
name=datasets.Split.TRAIN,
|
41 |
+
gen_kwargs={
|
42 |
+
"image_ids": Path(archive_path["texts"]).joinpath(
|
43 |
+
"Flickr_8k.trainImages.txt"
|
44 |
+
),
|
45 |
+
},
|
46 |
+
),
|
47 |
+
datasets.SplitGenerator(
|
48 |
+
name=datasets.Split.TEST,
|
49 |
+
gen_kwargs={
|
50 |
+
"image_ids": Path(archive_path["texts"]).joinpath(
|
51 |
+
"Flickr_8k.testImages.txt"
|
52 |
+
),
|
53 |
+
},
|
54 |
+
),
|
55 |
+
datasets.SplitGenerator(
|
56 |
+
name=datasets.Split.VALIDATION,
|
57 |
+
gen_kwargs={
|
58 |
+
"image_ids": Path(archive_path["texts"]).joinpath(
|
59 |
+
"Flickr_8k.devImages.txt"
|
60 |
+
),
|
61 |
+
},
|
62 |
+
),
|
63 |
+
]
|
64 |
+
for split in splits:
|
65 |
+
split.gen_kwargs.update(
|
66 |
+
{
|
67 |
+
"image_dir": Path(archive_path["images"]).joinpath(
|
68 |
+
"Flicker8k_Dataset"
|
69 |
+
),
|
70 |
+
"captions_txt": Path(archive_path["texts"]).joinpath(
|
71 |
+
"Flickr8k.token.txt"
|
72 |
+
),
|
73 |
+
}
|
74 |
+
)
|
75 |
+
|
76 |
+
return splits
|
77 |
+
|
78 |
+
def _generate_examples(
|
79 |
+
self,
|
80 |
+
image_ids,
|
81 |
+
image_dir,
|
82 |
+
captions_txt,
|
83 |
+
):
|
84 |
+
def get_image_ids_as_list(fpath):
|
85 |
+
ids = []
|
86 |
+
with open(fpath, "r") as f:
|
87 |
+
for image in f.read().split("\n"):
|
88 |
+
if image != "":
|
89 |
+
ids.append(image.split(".")[0])
|
90 |
+
return ids
|
91 |
+
|
92 |
+
def get_captions_by_id(captions_txt, id):
|
93 |
+
captions = []
|
94 |
+
with open(captions_txt, "r") as f:
|
95 |
+
for line in f.read().split("\n"):
|
96 |
+
if line != "":
|
97 |
+
m = re.match(id + r".jpg#\d+(.*)", line)
|
98 |
+
if m != None:
|
99 |
+
captions.append(m.group(1)[1:])
|
100 |
+
return captions
|
101 |
+
|
102 |
+
ids = get_image_ids_as_list(image_ids)
|
103 |
+
|
104 |
+
for id in ids:
|
105 |
+
img_path = image_dir.joinpath(id + ".jpg")
|
106 |
+
captions = get_captions_by_id(captions_txt, id)
|
107 |
+
example = {
|
108 |
+
"image_id": id,
|
109 |
+
"image_path": str(img_path),
|
110 |
+
"captions": captions,
|
111 |
+
}
|
112 |
+
yield id, example
|