-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
206 lines (162 loc) · 6.96 KB
/
dataloader.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
import os, argparse
import numpy as np
import torch
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from utils import sen2seq
class ImageFolder(datasets.ImageFolder):
def __getitem__(self, index):
original_tuple = super(ImageFolder, self).__getitem__(index)
return (original_tuple + (self.imgs[index][0],))
class Flickr8k_Dataset(torch.utils.data.Dataset):
def __init__(self, data, tokens, voca):
self.data = data
self.trg = tokens
self.voca = voca
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
idx_type = str(type(idx))
if idx_type == "<class 'int'>":
image, _, path = self.data[idx] # image : [3, 224, 224]
x = image # [3, 244, 244]
y = [sen2seq(self.voca, x[1:]) for x in self.trg if path.split('/')[3] in x[0]]
elif idx_type == "<class 'slice'>":
x, y = None, None
for i in range(*idx.indices(len(self.data))):
image, _, path = self.data[i] # image : [3, 224, 224]
if x is None:
x = image.unsqueeze(0) # [1, 3, 244, 244]
y = [sen2seq(self.voca, x[1:]) for x in self.trg if path.split('/')[3] in x[0]] # [1, 5, caption length]
else:
next_x = image.unsqueeze(0) # [1, 3, 244, 244]
next_y = [sen2seq(self.voca, x[1:]) for x in self.trg if path.split('/')[3] in x[0]] # [5, caption length]
x = torch.cat([x, next_x], dim = 0) # [(i+1), 3, 244, 244]
y += next_y # [(i+1)*5, caption_lengths]
else:
raise TypeError('Not compatible type')
return x, y
class Flickr8k_Dataset_with_Random_Sampling(torch.utils.data.Dataset):
def __init__(self, data, tokens, voca):
self.data = data
self.trg = tokens
self.voca = voca
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
idx_type = str(type(idx))
if idx_type == "<class 'int'>":
image, _, path = self.data[idx] # image : [3, 224, 224]
x = image # [3, 244, 244]
y = [sen2seq(self.voca, x[1:]) for x in self.trg if path.split('/')[3] in x[0]]
y = y[np.random.randint(5)]
elif idx_type == "<class 'slice'>":
x, y = [], []
for i in range(*idx.indices(len(self.data))):
image, _, path = self.data[i] # image : [3, 224, 224]
captions = [sen2seq(self.voca, x[1:]) for x in self.trg if path.split('/')[3] in x[0]]
caption = captions[np.random.randint(5)]
x.append(image.unsqueeze(0))
y.append(caption)
x = torch.cat(x, dim = 0)
else:
raise TypeError('Not compatible type')
return x, y
class DataLoader:
def __init__(self, dataset, batch_size, pad_idx):
self.dataset = dataset
self.size = len(dataset)
self.batch_size = batch_size
self.pad_idx = pad_idx
def __iter__(self):
self.index = 0
return self
def pad(self, batch):
max_len = 0
for seq in batch:
if max_len < len(seq):
max_len = len(seq)
for i in range(len(batch)):
batch[i] += [self.pad_idx] * (max_len - len(batch[i]))
return batch
def __next__(self):
if self.batch_size * self.index >= self.size:
raise StopIteration
src_batch, trg_batch = self.dataset[self.batch_size * self.index : self.batch_size * (self.index+1)]
trg_batch = self.pad(trg_batch)
self.index += 1
return src_batch, trg_batch
def get_train_data_loader(data_root, token_path, voca_path, batch_size, pad_idx):
# load tokens
if not os.path.exists(token_path):
tokens = []
with open(data_root + 'Flickr8k.token.txt', 'r') as reader:
for line in reader:
token = line.split()
if '.' in token :
token.remove('.')
tokens.append(token)
torch.save(tokens, 'preprocessed_data/tokens')
else:
print('tokens are loaded from ' + token_path)
tokens = torch.load(token_path)
# load vocabulary
if not os.path.exists(voca_path):
vocabulary = {'<sos>' : 0, '<eos>' : 1, '<pad>' : 2}
idx = 3
for token in tokens:
for word in token[1:]:
word = word.lower()
if word not in vocabulary:
vocabulary[word] = idx
idx += 1
torch.save(vocabulary, 'preprocessed_data/vocabulary')
else:
vocabulary = torch.load(voca_path)
print('vocabulary is loaded from ' + voca_path)
data = ImageFolder(root=data_root + 'train',
transform=transforms.Compose([
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))]))
dataset = Flickr8k_Dataset_with_Random_Sampling(data, tokens, vocabulary)
return DataLoader(dataset, batch_size, pad_idx)
def get_test_data_loader(data_root, token_path, voca_path, batch_size, pad_idx, dataset_type = 'test'):
# load tokens
if not os.path.exists(token_path):
tokens = []
with open(data_root + 'Flickr8k.token.txt', 'r') as reader:
for line in reader:
token = line.split()
if '.' in token :
token.remove('.')
tokens.append(token)
torch.save(tokens, 'preprocessed_data/tokens')
else:
print('tokens are loaded from ' + token_path)
tokens = torch.load(token_path)
# load vocabulary
if not os.path.exists(voca_path):
vocabulary = {'<sos>' : 0, '<eos>' : 1, '<pad>' : 2}
idx = 3
for token in tokens:
for word in token[1:]:
word = word.lower()
if word not in vocabulary:
vocabulary[word] = idx
idx += 1
torch.save(vocabulary, 'preprocessed_data/vocabulary')
else:
vocabulary = torch.load(voca_path)
print('vocabulary is loaded from ' + voca_path)
data = ImageFolder(root=data_root + dataset_type,
transform=transforms.Compose([
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))]))
dataset = Flickr8k_Dataset(data, tokens, vocabulary)
return DataLoader(dataset, batch_size, pad_idx)