-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
391 lines (332 loc) · 14.1 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import torch
import torch.utils.data as data
import torchvision.transforms as transforms
import os
import nltk
from PIL import Image
from pycocotools.coco import COCO
import numpy as np
import json as jsonmod
import pdb
def get_paths(path, name='coco', use_restval=False):
"""
Returns paths to images and annotations for the given datasets. For MSCOCO
indices are also returned to control the data split being used.
The indices are extracted from the Karpathy et al. splits using this
snippet:
>>> import json
>>> dataset=json.load(open('dataset_coco.json','r'))
>>> A=[]
>>> for i in range(len(D['images'])):
... if D['images'][i]['split'] == 'val':
... A+=D['images'][i]['sentids'][:5]
...
:param name: Dataset names
:param use_restval: If True, the the `restval` data is included in train.
"""
roots = {}
ids = {}
if 'coco' == name:
imgdir = os.path.join(path, 'images')
capdir = os.path.join(path, 'annotations')
roots['train'] = {
'img': os.path.join(imgdir, 'train2014'),
'cap': os.path.join(capdir, 'captions_train2014.json')
}
roots['val'] = {
'img': os.path.join(imgdir, 'val2014'),
'cap': os.path.join(capdir, 'captions_val2014.json')
}
roots['test'] = {
'img': os.path.join(imgdir, 'val2014'),
'cap': os.path.join(capdir, 'captions_val2014.json')
}
roots['trainrestval'] = {
'img': (roots['train']['img'], roots['val']['img']),
'cap': (roots['train']['cap'], roots['val']['cap'])
}
ids['train'] = np.load(os.path.join(capdir, 'coco_train_ids.npy'))
ids['val'] = np.load(os.path.join(capdir, 'coco_dev_ids.npy'))[:5000]
ids['test'] = np.load(os.path.join(capdir, 'coco_test_ids.npy'))
ids['trainrestval'] = (
ids['train'],
np.load(os.path.join(capdir, 'coco_restval_ids.npy')))
if use_restval:
roots['train'] = roots['trainrestval']
ids['train'] = ids['trainrestval']
elif 'f8k' == name:
imgdir = os.path.join(path, 'Flicker8k_Dataset')
cap = os.path.join(path, 'dataset_flickr8k.json')
roots['train'] = {'img': imgdir, 'cap': cap}
roots['val'] = {'img': imgdir, 'cap': cap}
roots['test'] = {'img': imgdir, 'cap': cap}
ids = {'train': None, 'val': None, 'test': None}
elif 'f30k' == name:
imgdir = os.path.join(path, 'images')
cap = os.path.join(path, 'dataset_flickr30k.json')
roots['train'] = {'img': imgdir, 'cap': cap}
roots['val'] = {'img': imgdir, 'cap': cap}
roots['test'] = {'img': imgdir, 'cap': cap}
ids = {'train': None, 'val': None, 'test': None}
return roots, ids
class CocoDataset(data.Dataset):
"""COCO Custom Dataset compatible with torch.utils.data.DataLoader."""
def __init__(self, root, json, vocab, transform=None, ids=None):
"""
Args:
root: image directory.
json: coco annotation file path.
vocab: vocabulary wrapper.
transform: transformer for image.
"""
self.root = root
# when using `restval`, two json files are needed
if isinstance(json, tuple):
self.coco = (COCO(json[0]), COCO(json[1]))
else:
self.coco = (COCO(json),)
self.root = (root,)
# if ids provided by get_paths, use split-specific ids
if ids is None:
self.ids = list(self.coco.anns.keys())
else:
self.ids = ids
# if `restval` data is to be used, record the break point for ids
if isinstance(self.ids, tuple):
self.bp = len(self.ids[0])
self.ids = list(self.ids[0]) + list(self.ids[1])
else:
self.bp = len(self.ids)
self.vocab = vocab
self.transform = transform
def __getitem__(self, index):
"""This function returns a tuple that is further passed to collate_fn
"""
vocab = self.vocab
root, caption, img_id, path, image = self.get_raw_item(index)
if self.transform is not None:
image = self.transform(image)
# Convert caption (string) to word ids.
tokens = nltk.tokenize.word_tokenize(
str(caption).lower().decode('utf-8'))
caption = []
caption.append(vocab('<start>'))
caption.extend([vocab(token) for token in tokens])
caption.append(vocab('<end>'))
target = torch.Tensor(caption)
return image, target, index, img_id
def get_raw_item(self, index):
if index < self.bp:
coco = self.coco[0]
root = self.root[0]
else:
coco = self.coco[1]
root = self.root[1]
ann_id = self.ids[index]
caption = coco.anns[ann_id]['caption']
img_id = coco.anns[ann_id]['image_id']
path = coco.loadImgs(img_id)[0]['file_name']
image = Image.open(os.path.join(root, path)).convert('RGB')
return root, caption, img_id, path, image
def __len__(self):
return len(self.ids)
class FlickrDataset(data.Dataset):
"""
Dataset loader for Flickr30k and Flickr8k full datasets.
"""
def __init__(self, root, json, split, vocab, transform=None):
self.root = root
self.vocab = vocab
self.split = split
self.transform = transform
self.dataset = jsonmod.load(open(json, 'r'))['images']
self.ids = []
for i, d in enumerate(self.dataset):
if d['split'] == split:
self.ids += [(i, x) for x in range(len(d['sentences']))]
def __getitem__(self, index):
"""This function returns a tuple that is further passed to collate_fn
"""
vocab = self.vocab
root = self.root
ann_id = self.ids[index]
img_id = ann_id[0]
caption = self.dataset[img_id]['sentences'][ann_id[1]]['raw']
path = self.dataset[img_id]['filename']
image = Image.open(os.path.join(root, path)).convert('RGB')
if self.transform is not None:
image = self.transform(image)
# Convert caption (string) to word ids.
tokens = nltk.tokenize.word_tokenize(
str(caption).lower().decode('utf-8'))
caption = []
caption.append(vocab('<start>'))
caption.extend([vocab(token) for token in tokens])
caption.append(vocab('<end>'))
target = torch.Tensor(caption)
return image, target, index, img_id
def __len__(self):
return len(self.ids)
class PrecompDataset(data.Dataset):
"""
Load precomputed captions and image features
Possible options: f8k, f30k, coco, 10crop
"""
def __init__(self, data_path, data_split, vocab):
self.vocab = vocab
loc = data_path + '/'
# Captions
self.captions = []
with open(loc+'%s_caps.txt' % data_split, 'rb') as f:
for line in f:
self.captions.append(line.strip())
# Image features
self.images = np.load(loc+'%s_ims.npy' % data_split)
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 = 5000
# audio loader
self.audios = np.load(loc+'%s_aud.npy' % data_split)
def __getitem__(self, index):
# handle the image redundancy
img_id = index/self.im_div
image = torch.Tensor(self.images[img_id])
caption = self.captions[index]
vocab = self.vocab
# Convert caption (string) to word ids.
tokens = nltk.tokenize.word_tokenize(
str(caption).lower().decode('utf-8'))
caption = []
caption.append(vocab('<start>'))
caption.extend([vocab(token) for token in tokens])
caption.append(vocab('<end>'))
target = torch.Tensor(caption)
# extract audio
audio = torch.Tensor(self.audios[index])
return image, target, audio, index, img_id
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).
targets: torch tensor of shape (batch_size, padded_length).
lengths: list; valid length for each padded caption.
"""
# Sort a data list by caption length
data.sort(key=lambda x: len(x[1]), reverse=True)
# pdb.set_trace()
images, captions, audios, ids, img_ids = zip(*data)
# Merge images (convert tuple of 3D tensor to 4D tensor)
images = torch.stack(images, 0)
# Merget captions (convert tuple of 1D tensor to 2D tensor)
lengths = [len(cap) for cap in captions]
targets = torch.zeros(len(captions), max(lengths)).long()
for i, cap in enumerate(captions):
end = lengths[i]
targets[i, :end] = cap[:end]
audios = torch.stack(audios, 0)
return images, targets, audios, lengths, ids
def get_loader_single(data_name, split, root, json, vocab, transform,
batch_size=100, shuffle=True,
num_workers=2, ids=None, collate_fn=collate_fn):
"""Returns torch.utils.data.DataLoader for custom coco dataset."""
if 'coco' in data_name:
# COCO custom dataset
dataset = CocoDataset(root=root,
json=json,
vocab=vocab,
transform=transform, ids=ids)
elif 'f8k' in data_name or 'f30k' in data_name:
dataset = FlickrDataset(root=root,
split=split,
json=json,
vocab=vocab,
transform=transform)
# Data loader
data_loader = torch.utils.data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
pin_memory=True,
num_workers=num_workers,
collate_fn=collate_fn)
return data_loader
def get_precomp_loader(data_path, data_split, vocab, opt, batch_size=100,
shuffle=True, num_workers=2):
"""Returns torch.utils.data.DataLoader for custom coco dataset."""
dset = PrecompDataset(data_path, data_split, vocab)
data_loader = torch.utils.data.DataLoader(dataset=dset,
batch_size=batch_size,
shuffle=shuffle,
pin_memory=True,
collate_fn=collate_fn)
return data_loader
def get_transform(data_name, split_name, opt):
normalizer = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
t_list = []
if split_name == 'train':
t_list = [transforms.RandomResizedCrop(opt.crop_size),
transforms.RandomHorizontalFlip()]
elif split_name == 'val':
t_list = [transforms.Resize(256), transforms.CenterCrop(224)]
elif split_name == 'test':
t_list = [transforms.Resize(256), transforms.CenterCrop(224)]
t_end = [transforms.ToTensor(), normalizer]
transform = transforms.Compose(t_list + t_end)
return transform
def get_loaders(data_name, vocab, crop_size, batch_size, workers, opt):
dpath = os.path.join(opt.data_path, data_name)
if opt.data_name.endswith('_precomp'):
train_loader = get_precomp_loader(dpath, 'train', vocab, opt,
batch_size, True, workers)
val_loader = get_precomp_loader(dpath, 'dev', vocab, opt,
batch_size, False, workers)
else:
# Build Dataset Loader
roots, ids = get_paths(dpath, data_name, opt.use_restval)
transform = get_transform(data_name, 'train', opt)
train_loader = get_loader_single(opt.data_name, 'train',
roots['train']['img'],
roots['train']['cap'],
vocab, transform, ids=ids['train'],
batch_size=batch_size, shuffle=True,
num_workers=workers,
collate_fn=collate_fn)
transform = get_transform(data_name, 'val', opt)
val_loader = get_loader_single(opt.data_name, 'val',
roots['val']['img'],
roots['val']['cap'],
vocab, transform, ids=ids['val'],
batch_size=batch_size, shuffle=False,
num_workers=workers,
collate_fn=collate_fn)
return train_loader, val_loader
def get_test_loader(split_name, data_name, vocab, crop_size, batch_size,
workers, opt):
dpath = os.path.join(opt.data_path, data_name)
if opt.data_name.endswith('_precomp'):
test_loader = get_precomp_loader(dpath, split_name, vocab, opt,
batch_size, False, workers)
else:
# Build Dataset Loader
roots, ids = get_paths(dpath, data_name, opt.use_restval)
transform = get_transform(data_name, split_name, opt)
test_loader = get_loader_single(opt.data_name, split_name,
roots[split_name]['img'],
roots[split_name]['cap'],
vocab, transform, ids=ids[split_name],
batch_size=batch_size, shuffle=False,
num_workers=workers,
collate_fn=collate_fn)
return test_loader