-
Notifications
You must be signed in to change notification settings - Fork 148
/
trainer.py
64 lines (46 loc) · 1.79 KB
/
trainer.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
# coding=utf-8
from __future__ import print_function, absolute_import
import time
from utils import AverageMeter, orth_reg
import torch
from torch.autograd import Variable
from torch.backends import cudnn
cudnn.benchmark = True
def train(epoch, model, criterion, optimizer, train_loader, args):
losses = AverageMeter()
batch_time = AverageMeter()
accuracy = AverageMeter()
pos_sims = AverageMeter()
neg_sims = AverageMeter()
end = time.time()
freq = min(args.print_freq, len(train_loader))
for i, data_ in enumerate(train_loader, 0):
inputs, labels = data_
# wrap them in Variable
inputs = Variable(inputs).cuda()
labels = Variable(labels).cuda()
optimizer.zero_grad()
embed_feat = model(inputs)
loss, inter_, dist_ap, dist_an = criterion(embed_feat, labels)
if args.orth_reg != 0:
loss = orth_reg(net=model, loss=loss, cof=args.orth_reg)
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
losses.update(loss.item())
accuracy.update(inter_)
pos_sims.update(dist_ap)
neg_sims.update(dist_an)
if (i + 1) % freq == 0 or (i+1) == len(train_loader):
print('Epoch: [{0:03d}][{1}/{2}]\t'
'Time {batch_time.avg:.3f}\t'
'Loss {loss.avg:.4f} \t'
'Accuracy {accuracy.avg:.4f} \t'
'Pos {pos.avg:.4f}\t'
'Neg {neg.avg:.4f} \t'.format
(epoch + 1, i + 1, len(train_loader), batch_time=batch_time,
loss=losses, accuracy=accuracy, pos=pos_sims, neg=neg_sims))
if epoch == 0 and i == 0:
print('-- HA-HA-HA-HA-AH-AH-AH-AH --')