-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha5_helper.py
278 lines (236 loc) · 8.01 KB
/
a5_helper.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
import json
import math
import time
import matplotlib.pyplot as plt
import seaborn
import torch
def load_coco_captions(path: str = "./datasets/coco.pt", print_it=False):
"""
Download and load serialized COCO data from coco.pt
It contains a dictionary of
"train_images" - resized training images (112x112)
"val_images" - resized validation images (112x112)
"train_captions" - tokenized and numericalized training captions
"val_captions" - tokenized and numericalized validation captions
"vocab" - caption vocabulary, including "idx_to_token" and "token_to_idx"
Returns: a data dictionary
"""
data_dict = torch.load(path)
# print out all the keys and values from the data dictionary
if print_it:
for k, v in data_dict.items():
if type(v) == torch.Tensor:
print(k, type(v), v.shape, v.dtype)
else:
print(k, type(v), v.keys())
assert data_dict["train_images"].size(0) == data_dict["train_captions"].size(
0
) and data_dict["val_images"].size(0) == data_dict["val_captions"].size(
0
), "shapes of data mismatch!"
if print_it:
print("\nTrain images shape: ", data_dict["train_images"].shape)
print("Train caption tokens shape: ", data_dict["train_captions"].shape)
print("Validation images shape: ", data_dict["val_images"].shape)
print("Validation caption tokens shape: ", data_dict["val_captions"].shape)
print(
"total number of caption tokens: ", len(data_dict["vocab"]["idx_to_token"])
)
print(
"mappings (list) from index to caption token: ",
data_dict["vocab"]["idx_to_token"],
)
print(
"mappings (dict) from caption token to index: ",
data_dict["vocab"]["token_to_idx"],
)
return data_dict
def get_toy_data(path: str = "final_data.json"):
return json.load(open(path))
def train_captioner(
model,
image_data,
caption_data,
num_epochs,
batch_size,
learning_rate,
lr_decay=1,
device: torch.device = torch.device("cpu"),
):
"""
Run optimization to train the model.
"""
model = model.to(device)
model.train()
optimizer = torch.optim.AdamW(
filter(lambda p: p.requires_grad, model.parameters()), learning_rate
)
lr_scheduler = torch.optim.lr_scheduler.LambdaLR(
optimizer, lambda epoch: lr_decay ** epoch
)
# sample minibatch data
iter_per_epoch = math.ceil(image_data.shape[0] // batch_size)
loss_history = []
for i in range(num_epochs):
start_t = time.time()
for j in range(iter_per_epoch):
images, captions = (
image_data[j * batch_size : (j + 1) * batch_size],
caption_data[j * batch_size : (j + 1) * batch_size],
)
images = images.to(device)
captions = captions.to(device)
loss = model(images, captions)
optimizer.zero_grad()
loss.backward()
loss_history.append(loss.item())
optimizer.step()
end_t = time.time()
print(
"(Epoch {} / {}) loss: {:.4f} time per epoch: {:.1f}s".format(
i, num_epochs, loss.item(), end_t - start_t
)
)
lr_scheduler.step()
# plot the training losses
plt.plot(loss_history)
plt.xlabel("Iteration")
plt.ylabel("Loss")
plt.title("Training loss history")
plt.show()
return model, loss_history
def decode_captions(captions, idx_to_word):
"""
Decoding caption indexes into words.
Args:
captions: Caption indexes in a tensor of shape (N, T).
idx_to_word: Mapping from the vocab index to word.
Returns:
decoded: A sentence (or a list of N sentences).
"""
singleton = captions.ndim == 1
captions = captions[None] if singleton else captions
decoded = []
N, T = captions.shape
for i in range(N):
words = []
for t in range(T):
word = idx_to_word[captions[i, t]]
if word != "<NULL>":
words.append(word)
if word == "<END>":
break
decoded.append(" ".join(words))
if singleton:
decoded = decoded[0]
return decoded
def train(
model,
train_dataloader,
val_dataloader,
loss_func,
num_epochs,
batch_size=32,
warmup_lr=6e-6,
warmup_interval=1000,
lr=6e-4,
device=torch.device("cpu"),
):
print("Training started...")
if warmup_interval is None:
optimizer = torch.optim.Adam(
model.parameters(), lr=lr, betas=(0.9, 0.995), eps=1e-9
)
else:
optimizer = torch.optim.Adam(
model.parameters(), lr=warmup_lr, betas=(0.9, 0.995), eps=1e-9
)
iteration = 0
for epoch_num in range(num_epochs):
epoch_loss = []
model.train()
for it in train_dataloader:
inp, inp_pos, out, out_pos = it
model = model.to(device)
inp_pos = inp_pos.to(device)
out_pos = out_pos.to(device)
out = out.to(device)
inp = inp.to(device)
gnd = out[:, 1:].contiguous().view(-1).long()
optimizer.zero_grad()
pred = model(inp.long(), inp_pos, out.long(), out_pos)
loss = loss_func(pred, gnd)
epoch_loss.append(loss.item())
if warmup_interval is not None and iteration == warmup_interval:
print(
f"End of warmup. Swapping learning rates from {warmup_lr} to {lr}"
)
for param_group in optimizer.param_groups:
warmup_lr = lr
param_group["lr"] = lr
loss.backward()
optimizer.step()
iteration = iteration + 1
avg_epoch_loss = sum(epoch_loss) / len(epoch_loss)
val_loss, val_acc = val(model, val_dataloader, loss_func, batch_size)
loss_hist = avg_epoch_loss / (batch_size * 4)
print(
f"[epoch: {epoch_num+1}]",
"[loss: ",
f"{loss_hist:.4f}",
"]",
"val_loss: [val_loss ",
f"{val_loss:.4f}",
"]",
)
return model
def val(model, dataloader, loss_func, batch_size, device=torch.device("cpu")):
model.eval()
epoch_loss = []
num_correct = 0
total = 0
for it in dataloader:
inp, inp_pos, out, out_pos = it
model = model.to(device)
inp_pos = inp_pos.to(device)
out_pos = out_pos.to(device)
out = out.to(device)
inp = inp.to(device)
gnd = out[:, 1:].contiguous().view(-1).long()
pred = model(inp.long(), inp_pos, out.long(), out_pos)
loss = loss_func(pred, gnd)
pred_max = pred.max(1)[1]
gnd = gnd.contiguous().view(-1)
n_correct = pred_max.eq(gnd)
n_correct = n_correct.sum().item()
num_correct = num_correct + n_correct
total = total + len(pred_max)
epoch_loss.append(loss.item())
avg_epoch_loss = sum(epoch_loss) / len(epoch_loss)
return avg_epoch_loss / (batch_size * 4), n_correct / total
def inference(model, inp_exp, inp_exp_pos, out_pos_exp, out_seq_len):
model.eval()
y_init = torch.LongTensor([14]).unsqueeze(0).cuda().view(1, 1)
ques_emb = model.emb_layer(inp_exp)
q_emb_inp = ques_emb + inp_exp_pos
enc_out = model.encoder(q_emb_inp)
for i in range(out_seq_len - 1):
ans_emb = model.emb_layer(y_init)
a_emb_inp = ans_emb + out_pos_exp[:, : y_init.shape[1], :]
dec_out = model.decoder(a_emb_inp, enc_out, None)
_, next_word = torch.max(
dec_out[0, y_init.shape[1] - 1 : y_init.shape[1]], dim=1
)
y_init = torch.cat([y_init, next_word.view(1, 1)], dim=1)
return y_init, model
def draw(data, x, y, ax):
seaborn.heatmap(
data,
xticklabels=x,
square=True,
yticklabels=y,
vmin=0.0,
vmax=1.0,
cbar=False,
ax=ax,
)