-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_arcface.py
162 lines (135 loc) · 5.44 KB
/
train_arcface.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
import pickle
import json
import torch
import torch.nn as nn
from tqdm import tqdm
import utils.config as config
from torch.nn import functional as F
import numpy as np
from tensorboardX import SummaryWriter
writer_tsne = SummaryWriter('runs/tsne')
def compute_supcon_loss(feats, qtype):
tau = 1.0
if isinstance(qtype, tuple):
i = 0
dic = {}
for item in qtype:
if item not in dic:
dic[item] = i
i = i + 1
tau = 1.0
qtype = torch.tensor([dic[item] for item in qtype]).cuda()
feats_filt = F.normalize(feats, dim=1)
targets_r = qtype.reshape(-1, 1)
targets_c = qtype.reshape(1, -1)
mask = targets_r == targets_c
mask = mask.int().cuda()
feats_sim = torch.exp(torch.matmul(feats_filt, feats_filt.T) / tau)
negatives = feats_sim*(1.0 - mask)
negative_sum = torch.sum(negatives)
positives = torch.log(feats_sim/negative_sum)*mask
positive_sum = torch.sum(positives)
positive_sum = positive_sum/torch.sum(mask)
sup_con_loss = -1*torch.mean(positive_sum)
return sup_con_loss
def compute_acc(logits, labels):
pred = torch.argmax(logits, dim = 1)
pred = pred.detach().cpu().numpy()
score = (pred == np.array(labels))
tot_correct = score.sum()
return tot_correct
def compute_score_with_logits(logits, labels):
_, log_index = logits.max(dim=1, keepdim=True)
scores = labels.gather(dim=1, index=log_index)
return scores
def compute_loss(output, labels):
#Function for calculating loss
ce_loss = nn.CrossEntropyLoss(reduction='mean')(output, labels.squeeze(-1).long())
return ce_loss
def saved_for_eval(dataloader, results, question_ids, answer_preds):
""" Save as a format accepted by the evaluation server. """
_, answer_ids = answer_preds.max(dim=1)
answers = [dataloader.dataset.label2ans[i] for i in answer_ids]
for q, a in zip(question_ids, answers):
entry = {
'question_id': q.item(),
'answer': a,
}
results.append(entry)
return results
def train(model, m_model, optim, train_loader, loss_fn, tracker, writer, tb_count, epoch, args):
loader = tqdm(train_loader, ncols=0)
loss_trk = tracker.track('loss', tracker.MovingMeanMonitor(momentum=0.99))
acc_trk = tracker.track('acc', tracker.MovingMeanMonitor(momentum=0.99))
for v, q, a, mg, bias, q_id, f1, type in loader:
v = v.cuda()
q = q.cuda()
a = a.cuda()
mg = mg.cuda()
bias = bias.cuda()
hidden_, ce_logits = model(v, q)
hidden, pred = m_model(hidden_, ce_logits, mg, epoch, a)
f1 = f1.cuda()
dict_args = {'margin': mg, 'bias': bias, 'hidden': hidden, 'epoch': epoch, 'per': f1}
gt = torch.argmax(a, 1)
#If bias-injection or learnable margins is enabled.
if config.learnable_margins or config.bias_inject:
#Use cross entropy loss to train the bias-injecting module
ce_loss = - F.log_softmax(ce_logits, dim=-1) * a
ce_loss = ce_loss * f1
loss = ce_loss.sum(dim=-1).mean() + loss_fn(hidden, a, **dict_args)
else:
loss = loss_fn(hidden, a, **dict_args)
#Add the supcon loss, as mentioned in Section 3 of main paper.
if config.supcon:
loss = loss + compute_supcon_loss(hidden_, gt)
writer.add_scalars('data/losses', {
}, tb_count)
tb_count += 1
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), 0.25)
optim.step()
optim.zero_grad()
#Ensemble the logit heads, as mentioned in Section 3 of the main paper, if bias-injection is enabled
if config.bias_inject or config.learnable_margins:
ce_logits = F.normalize(ce_logits)
pred_l = F.normalize(pred)
pred = (ce_logits + pred_l) / 2
batch_score = compute_score_with_logits(pred, a.data)
fmt = '{:.4f}'.format
loss_trk.append(loss.item())
acc_trk.append(batch_score.mean())
loader.set_postfix(loss=fmt(loss_trk.mean.value),
acc=fmt(acc_trk.mean.value))
return tb_count
#Evaluation code
def evaluate(model, m_model, dataloader, epoch=0, write=False):
score = 0
results = [] # saving for evaluation
type_score = 0
for v, q, a, mg, _, q_id, _, qtype in tqdm(dataloader, ncols=0, leave=True):
v = v.cuda()
q = q.cuda()
mg = mg.cuda()
a = a.cuda()
hidden, ce_logits = model(v, q)
hidden, pred = m_model(hidden, ce_logits, mg, epoch, a)
#Ensemble the logit heads
if config.learnable_margins or config.bias_inject:
ce_logits = F.softmax(F.normalize(ce_logits) / config.temp, 1)
pred_l = F.softmax(F.normalize(pred), 1)
pred = config.alpha * pred_l + (1-config.alpha) * ce_logits
if write:
results = saved_for_eval(dataloader, results, q_id, pred)
batch_score = compute_score_with_logits(pred, a.cuda()).sum()
score += batch_score
print(score, len(dataloader.dataset))
score = score / len(dataloader.dataset)
if write:
print("saving prediction results to disk...")
result_file = 'vqa_{}_{}_{}_{}_results.json'.format(
config.task, config.test_split, config.version, epoch)
with open(result_file, 'w') as fd:
json.dump(results, fd)
print(score)
return score