-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader.py
212 lines (181 loc) · 9.33 KB
/
data_loader.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
from vizwiz import VizWiz
import nltk
import os
import torch
import torch.utils.data as data
from vocabulary import Vocabulary
from PIL import Image
import numpy as np
from tqdm import tqdm
import json
def get_loader(transform=None,
mode='train',
batch_size=1,
vocab_threshold=None,
vocab_file='./vocab.pkl',
start_word="<start>",
end_word="<end>",
unk_word="<unk>",
vocab_from_file=True,
num_workers=0,
vizwiz_loc='./',
full_batch=False):
"""Returns the data loader.
Args:
transform: Image transform.
mode: One of 'train' or 'test'.
batch_size: Batch size (if in testing mode, must have batch_size=1).
vocab_threshold: Minimum word count threshold.
vocab_file: File containing the vocabulary.
start_word: Special word denoting sentence start.
end_word: Special word denoting sentence end.
unk_word: Special word denoting unknown words.
vocab_from_file: If False, create vocab from scratch & override any existing vocab_file.
If True, load vocab from from existing vocab_file, if it exists.
num_workers: Number of subprocesses to use for data loading
vizwiz_loc: The location of the folder containing the VizWiz API
"""
assert mode in ['train', 'test', 'val'], "mode must be one of 'train', 'val' or 'test'."
if vocab_from_file == False: assert mode == 'train', "To generate vocab from captions file, must be in training mode (mode='train')."
# Based on mode (train, val, test), obtain img_folder and annotations_file.
if mode == 'train':
if vocab_from_file == True: assert os.path.exists(
vocab_file), "vocab_file does not exist. Change vocab_from_file to False to create vocab_file."
img_folder = os.path.join(vizwiz_loc,
'images/train/')
annotations_file = os.path.join(vizwiz_loc,
'annotations/train.json')
if mode == 'val':
assert os.path.exists(vocab_file), "Must first generate vocab.pkl from training data."
assert vocab_from_file == True, "Change vocab_from_file to True."
img_folder = os.path.join(vizwiz_loc,
'images/val/')
annotations_file = os.path.join(vizwiz_loc,
'annotations/val.json')
if mode == 'test':
assert batch_size == 1, "Please change batch_size to 1 if testing your model."
assert os.path.exists(vocab_file), "Must first generate vocab.pkl from training data."
assert vocab_from_file == True, "Change vocab_from_file to True."
img_folder = os.path.join(vizwiz_loc, 'images/test/')
annotations_file = os.path.join(vizwiz_loc, 'annotations/test.json')
# VizWiz caption dataset.
dataset = VizWizDataset(transform=transform,
mode=mode,
batch_size=batch_size,
vocab_threshold=vocab_threshold,
vocab_file=vocab_file,
start_word=start_word,
end_word=end_word,
unk_word=unk_word,
annotations_file=annotations_file,
vocab_from_file=vocab_from_file,
img_folder=img_folder,
full_batch=full_batch)
if mode == 'train':
# Randomly sample a caption length, and sample indices with that length.
indices = dataset.get_train_indices()
# Create and assign a batch sampler to retrieve a batch with the sampled indices.
initial_sampler = data.sampler.SubsetRandomSampler(indices=indices)
# data loader for VizWiz dataset.
data_loader = data.DataLoader(dataset=dataset,
num_workers=num_workers,
batch_sampler=data.sampler.BatchSampler(sampler=initial_sampler,
batch_size=dataset.batch_size,
drop_last=False))
elif mode == 'val':
data_loader = data.DataLoader(dataset=dataset,
batch_size=dataset.batch_size,
num_workers=num_workers)
else:
data_loader = data.DataLoader(dataset=dataset,
batch_size=dataset.batch_size,
shuffle=True,
num_workers=num_workers)
return data_loader
class VizWizDataset(data.Dataset):
def __init__(self, transform, mode, batch_size, vocab_threshold, vocab_file, start_word,
end_word, unk_word, annotations_file, vocab_from_file, img_folder, full_batch):
self.transform = transform
self.mode = mode
self.batch_size = batch_size
self.vocab = Vocabulary(vocab_threshold, vocab_file, start_word,
end_word, unk_word, annotations_file, vocab_from_file)
self.img_folder = img_folder
if self.mode == 'train':
self.wizviz = VizWiz(annotations_file)
self.ids = list(self.wizviz.anns.keys())
print('Obtaining caption lengths...')
all_tokens = [nltk.tokenize.word_tokenize(str(self.wizviz.anns[self.ids[index]]['caption']).lower()) for index
in tqdm(np.arange(len(self.ids)))]
self.caption_lengths = [len(token) for token in all_tokens]
elif self.mode == 'val':
self.wizviz = VizWiz(annotations_file)
self.ids = list(self.wizviz.anns.keys())
print('Obtaining caption lengths...')
all_tokens = [nltk.tokenize.word_tokenize(str(self.wizviz.anns[self.ids[index]]['caption']).lower()) for
index
in tqdm(np.arange(len(self.ids)))]
self.caption_lengths = [len(token) for token in all_tokens]
test_info = json.loads(open(annotations_file).read())
self.paths = [item for item in test_info['images'] if len(self.wizviz.imgToAnns[item['id']]) > 0]
else:
test_info = json.loads(open(annotations_file).read())
self.paths = [item['file_name'] for item in test_info['images']]
def __getitem__(self, index):
# obtain image and caption if in training mode
if self.mode == 'train':
ann_id = self.ids[index]
caption = self.wizviz.anns[ann_id]['caption']
img_id = self.wizviz.anns[ann_id]['image_id']
path = self.wizviz.loadImgs(img_id)[0]['file_name']
# Convert image to tensor and pre-process using transform
image = Image.open(os.path.join(self.img_folder, path)).convert('RGB')
if self.transform:
image = self.transform(image)
# Convert caption to tensor of word ids.
tokens = nltk.tokenize.word_tokenize(str(caption).lower())
caption = []
caption.append(self.vocab(self.vocab.start_word))
caption.extend([self.vocab(token) for token in tokens])
caption.append(self.vocab(self.vocab.end_word))
caption = torch.Tensor(caption).long()
# return pre-processed image and caption tensors
return image, caption
elif self.mode == 'val':
image = self.paths[index]
img_id = image['id']
# pick first matching cation
caption = np.random.choice(self.wizviz.imgToAnns[img_id])
path = self.wizviz.loadImgs(img_id)[0]['file_name']
# Convert image to tensor and pre-process using transform
image = Image.open(os.path.join(self.img_folder, path)).convert('RGB')
if self.transform:
image = self.transform(image)
# Convert caption to tensor of word ids.
tokens = nltk.tokenize.word_tokenize(str(caption).lower())
caption = []
caption.append(self.vocab(self.vocab.start_word))
caption.extend([self.vocab(token) for token in tokens])
caption.append(self.vocab(self.vocab.end_word))
caption = torch.Tensor(caption).long()
# return pre-processed image and caption tensors
return image, caption, img_id
# obtain image if in test mode
else:
path = self.paths[index]
# Convert image to tensor and pre-process using transform
PIL_image = Image.open(os.path.join(self.img_folder, path)).convert('RGB')
orig_image = np.array(PIL_image)
image = self.transform(PIL_image)
# return original image and pre-processed image tensor
return orig_image, image
def get_train_indices(self):
sel_length = np.random.choice(self.caption_lengths)
all_indices = np.where([self.caption_lengths[i] == sel_length for i in np.arange(len(self.caption_lengths))])[0]
indices = list(np.random.choice(all_indices, size=self.batch_size))
return indices
def __len__(self):
if self.mode == 'train':
return len(self.ids)
else:
return len(self.paths)