-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathutils_pytorch.py
144 lines (121 loc) · 5.19 KB
/
utils_pytorch.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
# imdbfolder_coco.py
# created by Sylvestre-Alvise Rebuffi [[email protected]]
# Copyright © The University of Oxford, 2017-2020
# This code is made available under the Apache v2.0 licence, see LICENSE.txt for details
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import os
import time
import numpy as np
from torch.autograd import Variable
import config_task
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def adjust_learning_rate_and_learning_taks(optimizer, epoch, args):
"""Sets the learning rate to the initial LR decayed by 10 every X epochs"""
if epoch >= args.step2:
lr = args.lr * 0.01
elif epoch >= args.step1:
lr = args.lr * 0.1
else:
lr = args.lr
for param_group in optimizer.param_groups:
param_group['lr'] = lr
# Return training classes
return range(len(args.dataset))
# Training
def train(epoch, tloaders, tasks, net, args, optimizer,list_criterion=None):
print('\nEpoch: %d' % epoch)
net.train()
batch_time = AverageMeter()
data_time = AverageMeter()
losses = [AverageMeter() for i in tasks]
top1 = [AverageMeter() for i in tasks]
end = time.time()
loaders = [tloaders[i] for i in tasks]
min_len_loader = np.min([len(i) for i in loaders])
train_iter = [iter(i) for i in loaders]
for batch_idx in range(min_len_loader*len(tasks)):
config_task.first_batch = (batch_idx == 0)
# Round robin process of the tasks
current_task_index = batch_idx % len(tasks)
inputs, targets = (train_iter[current_task_index]).next()
config_task.task = tasks[current_task_index]
# measure data loading time
data_time.update(time.time() - end)
if args.use_cuda:
inputs, targets = inputs.cuda(async=True), targets.cuda(async=True)
optimizer.zero_grad()
inputs, targets = Variable(inputs), Variable(targets)
outputs = net(inputs)
loss = args.criterion(outputs, targets)
# measure accuracy and record loss
(losses[current_task_index]).update(loss.data[0], targets.size(0))
_, predicted = torch.max(outputs.data, 1)
correct = predicted.eq(targets.data).cpu().sum()
(top1[current_task_index]).update(correct*100./targets.size(0), targets.size(0))
# apply gradients
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if batch_idx % 50 == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'.format(
epoch, batch_idx, min_len_loader*len(tasks), batch_time=batch_time,
data_time=data_time))
for i in range(len(tasks)):
print('Task {0} : Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Acc {top1.val:.3f} ({top1.avg:.3f})'.format(tasks[i], loss=losses[i], top1=top1[i]))
return [top1[i].avg for i in range(len(tasks))], [losses[i].avg for i in range(len(tasks))]
def test(epoch, loaders, all_tasks, net, best_acc, args, optimizer):
net.eval()
losses = [AverageMeter() for i in all_tasks]
top1 = [AverageMeter() for i in all_tasks]
print('Epoch: [{0}]'.format(epoch))
for itera in range(len(all_tasks)):
i = all_tasks[itera]
config_task.task = i
for batch_idx, (inputs, targets) in enumerate(loaders[i]):
if args.use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = Variable(inputs, volatile=True), Variable(targets)
outputs = net(inputs)
if isinstance(outputs, tuple):
outputs = outputs[0]
loss = args.criterion(outputs, targets)
losses[itera].update(loss.data[0], targets.size(0))
_, predicted = torch.max(outputs.data, 1)
correct = predicted.eq(targets.data).cpu().sum()
top1[itera].update(correct*100./targets.size(0), targets.size(0))
print('Task {0} : Test Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Test Acc {top1.val:.3f} ({top1.avg:.3f})'.format(i, loss=losses[itera], top1=top1[itera]))
# Save checkpoint.
acc = np.sum([top1[i].avg for i in range(len(all_tasks))])
if acc > best_acc:
print('Saving..')
state = {
'net': net,
'acc': acc,
'epoch': epoch,
}
torch.save(state, args.ckpdir+'/ckpt'+config_task.mode+args.archi+args.proj+''.join(args.dataset)+'.t7')
best_acc = acc
return [top1[i].avg for i in range(len(all_tasks))], [losses[i].avg for i in range(len(all_tasks))], best_acc