-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathcrossmodal3600.py
152 lines (132 loc) · 4 KB
/
crossmodal3600.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import codecs
import json
import os
from subprocess import call
from PIL import Image
from torchvision.datasets import VisionDataset
SUPPORTED_LANGUAGES = [
"ar",
"bn",
"cs",
"da",
"de",
"el",
"en",
"es",
"fa",
"fi",
"fil",
"fr",
"he",
"hi",
"hr",
"hu",
"id",
"it",
"ja",
"ko",
"mi",
"nl",
"no",
"pl",
"pt",
"quz",
"ro",
"ru",
"sv",
"sw",
"te",
"th",
"tr",
"uk",
"vi",
"zh",
]
CAPTIONS_DOWNLOAD_URL = "https://google.github.io/crossmodal-3600/web-data/captions.zip"
IMAGES_DOWNLOAD_URL = (
"https://open-images-dataset.s3.amazonaws.com/crossmodal-3600/images.tgz"
)
OUTPUT_FILENAME_TEMPLATE = "crossmodal3600_captions-{}.json"
class Crossmodal3600(VisionDataset):
def __init__(self, root, ann_file, transform=None, target_transform=None):
super().__init__(root, transform=transform, target_transform=target_transform)
self.ann_file = os.path.expanduser(ann_file)
with codecs.open(ann_file, "r", encoding="utf-8") as fp:
data = json.load(fp)
self.data = [
(img_path, txt)
for img_path, txt in zip(data["image_paths"], data["annotations"])
]
def __getitem__(self, index):
img, captions = self.data[index]
# Image
img = Image.open(img).convert("RGB")
if self.transform is not None:
img = self.transform(img)
# Captions
target = [
captions,
]
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self) -> int:
return len(self.data)
def _download_captions(out_path):
os.makedirs(out_path, exist_ok=True)
print("Downloading captions")
call(f"wget {CAPTIONS_DOWNLOAD_URL} -O captions.zip", shell=True)
call(f"unzip captions.zip -d {out_path}", shell=True)
call("rm captions.zip", shell=True)
def _download_images(out_path):
os.makedirs(out_path, exist_ok=True)
print("Downloading images")
call(f"wget {IMAGES_DOWNLOAD_URL} -O images.tgz", shell=True)
call(f"tar -xzf images.tgz -C {out_path}", shell=True)
call("rm images.tgz", shell=True)
def create_annotation_file(root, lang_code):
if lang_code not in SUPPORTED_LANGUAGES:
raise ValueError(
f"Language code {lang_code} not supported. Supported languages are {SUPPORTED_LANGUAGES}"
)
data_dir = os.path.join(root, "xm3600")
images_dir = os.path.join(data_dir, "images")
if not os.path.exists(images_dir):
_download_images(images_dir)
captions_path = os.path.join(data_dir, "captions.jsonl")
if not os.path.exists(captions_path):
_download_captions(data_dir)
with open(captions_path, "r", encoding="utf-8") as f:
data = f.readlines()
data = [json.loads(line) for line in data]
number_of_missing_images = 0
valid_images, valid_annotations, valid_indicies = [], [], []
for i, data_item in enumerate(data):
image_id = data_item["image/key"]
image_name = f"{image_id}.jpg"
image_path = os.path.join(images_dir, image_name)
if not os.path.exists(image_path):
print("Missing image file", image_name)
number_of_missing_images += 1
continue
captions = data_item[lang_code]["caption"]
txt = captions[0]
valid_images.append(image_path)
valid_annotations.append(txt)
valid_indicies.append(i)
if number_of_missing_images > 0:
print(f"*** WARNING *** missing {number_of_missing_images} files.")
with codecs.open(
os.path.join(root, OUTPUT_FILENAME_TEMPLATE.format(lang_code)),
"w",
encoding="utf-8",
) as fp:
json.dump(
{
"image_paths": valid_images,
"annotations": valid_annotations,
"indicies": valid_indicies,
},
fp,
ensure_ascii=False,
)