-
Notifications
You must be signed in to change notification settings - Fork 15
/
trainers.py
176 lines (134 loc) · 5.72 KB
/
trainers.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
import torch, copy, random, numpy as np
from torch.nn import functional as F
from torch import nn
from ignite.engine.engine import Engine, State, Events
from ignite.utils import convert_tensor
from ignite import metrics
import utils
device = torch.device('cuda:0')
def create_baseline_trainer(model,
optimizer=None,
name='train',
device=None):
if device is not None:
model.to(device)
is_train = optimizer is not None
def _update(engine, batch):
model.train(is_train)
with torch.set_grad_enabled(is_train):
images, labels = convert_tensor(batch, device=device)
preds = model(images)
loss = F.cross_entropy(preds, labels)
if is_train:
optimizer.zero_grad()
loss.backward()
optimizer.step()
return {
'loss': loss.item(),
'y_pred': preds,
'y': labels
}
engine = Engine(_update)
engine.name = name
metrics.Average(lambda o: o['loss']).attach(engine, 'single_loss')
metrics.Accuracy(lambda o: (o['y_pred'], o['y'])).attach(engine, 'single_acc')
return engine
def create_sla_trainer(model,
transform,
optimizer=None,
with_large_loss=False,
name='train',
device=None):
if device is not None:
model.to(device)
is_train = optimizer is not None
def _update(engine, batch):
model.train(is_train)
with torch.set_grad_enabled(is_train):
images, labels = convert_tensor(batch, device=device)
batch_size = images.shape[0]
images = transform(model, images, labels)
n = images.shape[0] // batch_size
preds = model(images)
labels = torch.stack([labels*n+i for i in range(n)], 1).view(-1)
loss = F.cross_entropy(preds, labels)
if with_large_loss:
loss = loss * n
single_preds = preds[::n, ::n]
single_labels = labels[::n] // n
agg_preds = 0
for i in range(n):
agg_preds = agg_preds + preds[i::n, i::n] / n
if is_train:
optimizer.zero_grad()
loss.backward()
optimizer.step()
return {
'loss': loss.item(),
'preds': preds,
'labels': labels,
'single_preds': single_preds,
'single_labels': single_labels,
'agg_preds': agg_preds,
}
engine = Engine(_update)
engine.name = name
metrics.Average(lambda o: o['loss']).attach(engine, 'total_loss')
metrics.Accuracy(lambda o: (o['preds'], o['labels'])).attach(engine, 'total_acc')
metrics.Average(lambda o: F.cross_entropy(o['single_preds'], o['single_labels'])).attach(engine, 'single_loss')
metrics.Accuracy(lambda o: (o['single_preds'], o['single_labels'])).attach(engine, 'single_acc')
metrics.Average(lambda o: F.cross_entropy(o['agg_preds'], o['single_labels'])).attach(engine, 'agg_loss')
metrics.Accuracy(lambda o: (o['agg_preds'], o['single_labels'])).attach(engine, 'agg_acc')
return engine
def create_sla_sd_trainer(model,
transform,
optimizer=None,
T=1.0,
with_large_loss=False,
name='train',
device=None):
if device is not None:
model.to(device)
is_train = optimizer is not None
def _update(engine, batch):
model.train(is_train)
with torch.set_grad_enabled(is_train):
images, single_labels = convert_tensor(batch, device=device)
batch_size = images.shape[0]
images = transform(model, images, single_labels)
n = images.shape[0] // batch_size
joint_preds, single_preds = model(images, None)
single_preds = single_preds[::n]
joint_labels = torch.stack([single_labels*n+i for i in range(n)], 1).view(-1)
joint_loss = F.cross_entropy(joint_preds, joint_labels)
single_loss = F.cross_entropy(single_preds, single_labels)
if with_large_loss:
joint_loss = joint_loss * n
agg_preds = 0
for i in range(n):
agg_preds = agg_preds + joint_preds[i::n, i::n] / n
distillation_loss = F.kl_div(F.log_softmax(single_preds / T, 1),
F.softmax(agg_preds.detach() / T, 1),
reduction='batchmean')
loss = joint_loss + single_loss + distillation_loss.mul(T**2)
if is_train:
optimizer.zero_grad()
loss.backward()
optimizer.step()
return {
'loss': loss.item(),
'preds': joint_preds,
'labels': joint_labels,
'single_preds': single_preds,
'single_labels': single_labels,
'agg_preds': agg_preds,
}
engine = Engine(_update)
engine.name = name
metrics.Average(lambda o: o['loss']).attach(engine, 'total_loss')
metrics.Accuracy(lambda o: (o['preds'], o['labels'])).attach(engine, 'total_acc')
metrics.Average(lambda o: F.cross_entropy(o['single_preds'], o['single_labels'])).attach(engine, 'single_loss')
metrics.Accuracy(lambda o: (o['single_preds'], o['single_labels'])).attach(engine, 'single_acc')
metrics.Average(lambda o: F.cross_entropy(o['agg_preds'], o['single_labels'])).attach(engine, 'agg_loss')
metrics.Accuracy(lambda o: (o['agg_preds'], o['single_labels'])).attach(engine, 'agg_acc')
return engine