-
Notifications
You must be signed in to change notification settings - Fork 3
/
data.py
341 lines (293 loc) · 11 KB
/
data.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""Dataloader"""
import os
import copy
import csv
import nltk
import numpy as np
import torch
import torch.utils.data as data
class PrecompDataset(data.Dataset):
"""
Load precomputed captions and image features
Possible options: f30k_precomp, coco_precomp
"""
def __init__(
self,
captions,
images,
data_split,
noise_ratio=0,
noise_file="",
mode="",
pred=[],
probability=[],
):
assert 0 <= noise_ratio < 1
self.captions = captions
self.images = images
self.noise_ratio = noise_ratio
self.data_split = data_split
self.mode = mode
self.length = len(self.captions)
# rkiros data has redundancy in images, we divide by 5, 10crop doesn't.
if self.images.shape[0] != self.length:
self.im_div = 5
else:
self.im_div = 1
# the development set for coco is large and so validation would be slow
if data_split == "dev":
self.length = 1000 * self.im_div
# one image has five captions
self.t2i_index = np.arange(0, self.length) // self.im_div
# Noisy label
if data_split == "train":
split_idx = None
self._t2i_index = copy.deepcopy(self.t2i_index)
if noise_ratio:
if os.path.exists(noise_file):
print("=> load noisy index from {}".format(noise_file))
self.t2i_index = np.load(noise_file)
else:
idx = np.arange(self.length)
np.random.shuffle(idx)
noise_length = int(noise_ratio * self.length)
shuffle_index = self.t2i_index[idx[:noise_length]]
np.random.shuffle(shuffle_index)
self.t2i_index[idx[:noise_length]] = shuffle_index
np.save(noise_file, self.t2i_index)
print("=> save noisy index to {}".format(noise_file))
# save clean labels
self._labels = np.ones((self.length), dtype="int")
self._labels[self._t2i_index != self.t2i_index] = 0
noise_label = np.ones_like(self._labels)
if self.mode == "labeled":
split_idx = pred.nonzero()[0]
self.probability = [probability[i] for i in split_idx]
elif self.mode == "unlabeled":
split_idx = (1 - pred).nonzero()[0]
if split_idx is not None:
# self.images = self.images[split_idx]
self.captions = [self.captions[i] for i in split_idx]
self.t2i_index = [self.t2i_index[i] for i in split_idx]
self._t2i_index = [self._t2i_index[i] for i in split_idx]
self._labels = [self._labels[i] for i in split_idx]
self.length = len(self.captions)
print("{} {} data has a size of {}".format(data_split, self.mode, self.length))
def __getitem__(self, index):
image = torch.Tensor(self.images[self.t2i_index[index]])
text = torch.Tensor(self.captions[index])
if self.data_split == "train":
if self.mode == "labeled":
return (
image,
text,
index,
torch.Tensor([1]),
torch.Tensor([self.probability[index]]),
self._labels[index],
)
elif self.mode == "unlabeled":
return image, text, index, self._labels[index], 0
else:
return image, text, index, self.t2i_index[index]
else:
return image, text, index, self.t2i_index[index]
def __len__(self):
return self.length
def collate_fn(data):
"""Build mini-batch tensors from a list of (image, caption) tuples.
Args:
data: list of (image, caption) tuple.
- image: torch tensor of shape (3, 256, 256).
- caption: torch tensor of shape (?); variable length.
Returns:
images: torch tensor of shape (batch_size, 3, 256, 256).
text: torch tensor of shape (batch_size, padded_length).
lengths: list; valid length for each padded caption.
"""
labels = None
if len(data[0]) == 6:
images, captions, ids, labels, prob, _labels = zip(*data)
# Merge
labels = torch.stack(labels, 0).long()
# Merge
prob = torch.stack(prob, 0)
elif len(data[0]) == 5:
images, captions, ids, _labels, _ = zip(*data)
else:
images, captions, ids, img_ids = zip(*data)
# Merge images (convert tuple of 3D tensor to 4D tensor)
images = torch.stack(images, 0)
# Merge captions (convert tuple of 1D tensor to 2D tensor)
lengths = [len(cap) for cap in captions]
text = torch.zeros(len(captions), max(lengths)).long()
for i, cap in enumerate(captions):
end = lengths[i]
text[i, :end] = cap[:end]
if len(data[0]) == 6:
return images, text, lengths, ids, labels, prob, _labels
elif len(data[0]) == 5:
return images, text, lengths, ids, _labels
else:
return images, text, lengths, ids
def get_dataset(data_path, data_name, data_split, vocab, return_id_caps=False):
data_path = os.path.join(data_path, data_name)
# Captions
captions = []
if data_name == "cc152k_precomp":
img_ids = []
with open(os.path.join(data_path, "%s_caps.tsv" % data_split)) as f:
tsvreader = csv.reader(f, delimiter="\t")
for line in tsvreader:
captions.append(line[1].strip())
img_ids.append(line[0])
elif data_name in ["coco_precomp", "f30k_precomp"]:
with open(os.path.join(data_path, "%s_caps.txt" % data_split), "r") as f:
for line in f:
captions.append(line.strip())
else:
raise NotImplementedError("Unsupported dataset!")
# caption tokens
captions_token = []
for index in range(len(captions)):
caption = captions[index]
# Convert caption (string) to word ids.
tokens = nltk.tokenize.word_tokenize(caption.lower())
caption = []
caption.append(vocab("<start>"))
caption.extend([vocab(token) for token in tokens])
caption.append(vocab("<end>"))
captions_token.append(caption)
# images
images = np.load(os.path.join(data_path, "%s_ims.npy" % data_split))
print(
"load {} / {} data: {} images, {} captions".format(
data_path, data_split, images.shape[0], len(captions)
)
)
if return_id_caps:
return captions_token, images, img_ids, captions
else:
return captions_token, images
def get_dataset_mask(data_path, data_name, data_split, vocab, return_id_caps=False):
data_path = os.path.join(data_path, data_name)
# Captions
captions = []
if data_name == "cc152k_precomp":
img_ids = []
with open(os.path.join(data_path, "%s_caps.tsv" % data_split)) as f:
tsvreader = csv.reader(f, delimiter="\t")
for line in tsvreader:
captions.append(line[1].strip())
img_ids.append(line[0])
elif data_name in ["coco_precomp", "f30k_precomp"]:
with open(os.path.join(data_path, "%s_caps.txt" % data_split), "r") as f:
for line in f:
captions.append(line.strip())
else:
raise NotImplementedError("Unsupported dataset!")
# caption tokens
captions_token = []
for index in range(len(captions)):
caption = captions[index]
# Convert caption (string) to word ids.
tokens = nltk.tokenize.word_tokenize(caption.lower())
caption = []
caption.append(vocab("<start>"))
caption.extend([vocab(token) for token in tokens])
caption.append(vocab("<end>"))
captions_token.append(caption)
# images
images = np.load(os.path.join(data_path, "%s_ims.npy" % data_split))
print(
"load {} / {} data: {} images, {} captions".format(
data_path, data_split, images.shape[0], len(captions)
)
)
if return_id_caps:
return captions_token, images, img_ids, captions
else:
return captions_token, images
def get_loader(
captions,
images,
data_split,
batch_size,
workers,
noise_ratio=0,
noise_file="",
pred=[],
prob=[],
):
if data_split == "warmup":
dset = PrecompDataset(captions, images, "train", noise_ratio, noise_file)
data_loader = torch.utils.data.DataLoader(
dataset=dset,
batch_size=batch_size,
shuffle=True,
pin_memory=True,
collate_fn=collate_fn,
num_workers=workers,
)
return data_loader, dset.length, dset._labels
elif data_split == "train":
labeled_dataset = PrecompDataset(
captions,
images,
"train",
noise_ratio,
noise_file,
mode="labeled",
pred=pred,
probability=prob,
)
labeled_trainloader = torch.utils.data.DataLoader(
dataset=labeled_dataset,
batch_size=batch_size,
shuffle=True,
pin_memory=True,
collate_fn=collate_fn,
num_workers=workers,
)
unlabeled_dataset = PrecompDataset(
captions,
images,
"train",
noise_ratio,
noise_file,
mode="unlabeled",
pred=pred,
probability=prob,
)
unlabeled_trainloader = torch.utils.data.DataLoader(
dataset=unlabeled_dataset,
batch_size=batch_size,
shuffle=True,
pin_memory=True,
collate_fn=collate_fn,
num_workers=workers,
)
return labeled_trainloader, unlabeled_trainloader
elif data_split == "dev":
dset = PrecompDataset(captions, images, data_split)
data_loader = torch.utils.data.DataLoader(
dataset=dset,
batch_size=batch_size,
shuffle=False,
pin_memory=True,
collate_fn=collate_fn,
num_workers=workers,
)
elif data_split in ["test", "testall", "test5k"]:
dset = PrecompDataset(captions, images, data_split)
data_loader = torch.utils.data.DataLoader(
dataset=dset,
batch_size=batch_size,
shuffle=False,
pin_memory=True,
collate_fn=collate_fn,
num_workers=workers,
)
else:
raise NotImplementedError("Not support data split!")
return data_loader