-
Notifications
You must be signed in to change notification settings - Fork 115
/
basics.py
302 lines (248 loc) · 11.4 KB
/
basics.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import logging
import math
import os
import numpy as np
import torch
import torch.nn.functional as F
from tqdm import tqdm
import networks
from utils import pm
from utils.distributed import all_gather_coalesced
# train models
def load_train_models(state):
if state.train_nets_type == 'unknown_init':
model, = networks.get_networks(state, N=1)
return [model for _ in range(state.local_n_nets)]
elif state.train_nets_type == 'known_init':
return networks.get_networks(state, N=state.local_n_nets)
elif state.train_nets_type == 'loaded':
models = networks.get_networks(state, N=state.local_n_nets)
with state.pretend(phase='train'): # in case test_nets_type == same_as_train
model_dir = state.get_model_dir()
start_idx = state.world_rank * state.local_n_nets
for n, model in enumerate(models, start_idx):
model_path = os.path.join(model_dir, 'net_{:04d}'.format(n))
model.load_state_dict(torch.load(model_path, map_location=state.device))
logging.info('Loaded checkpoints [{} ... {}) from {}'.format(
start_idx, start_idx + state.local_n_nets, model_dir))
return models
else:
raise ValueError("train_nets_type: {}".format(state.train_nets_type))
def task_loss(state, output, label, **kwargs):
if state.num_classes == 2:
label = label.to(output, non_blocking=True).view_as(output)
return F.binary_cross_entropy_with_logits(output, label, **kwargs)
else:
return F.cross_entropy(output, label, **kwargs)
def final_objective_loss(state, output, label):
if state.mode in {'distill_basic', 'distill_adapt'}:
return task_loss(state, output, label)
elif state.mode == 'distill_attack':
label = label.clone()
label[label == state.attack_class] = state.target_class
return task_loss(state, output, label)
else:
raise NotImplementedError('mode ({}) is not implemented'.format(state.mode))
# NB: This trains params or model inplace!!!
def train_steps_inplace(state, models, steps, params=None, callback=None):
if isinstance(models, torch.nn.Module):
models = [models]
if params is None:
params = [m.get_param() for m in models]
for i, (data, label, lr) in enumerate(steps):
if callback is not None:
callback(i, params)
data = data.detach()
label = label.detach()
lr = lr.detach()
for model, w in zip(models, params):
model.train() # callback may change model.training so we set here
output = model.forward_with_param(data, w)
loss = task_loss(state, output, label)
loss.backward(lr.squeeze())
with torch.no_grad():
w.sub_(w.grad)
w.grad = None
if callback is not None:
callback(len(steps), params)
return params
# NOTE [ Evaluation Result Format ]
#
# Result is always a 3-tuple, containing (test_step_indices, accuracies, losses):
#
# - `test_step_indices`: an int64 vector of shape [NUM_STEPS].
# - `accuracies`:
# + for mode != 'distill_attack', a matrix of shape [NUM_STEPS, NUM_MODELS].
# + for mode == 'distill_attack', a tensor of shape
# [NUM_STEPS, NUM_MODELS x NUM_CLASSES + 3], where the last dimensions
# contains
# [overall acc, acc w.r.t. modified labels,
# class 0 acc, class 1 acc, ...,
# ratio of attack_class predicted as target_class]
# - `losses`: a matrix of shape [NUM_STEPS, NUM_MODELS]
# See NOTE [ Evaluation Result Format ] for output format
def evaluate_models(state, models, param_list=None, test_all=False, test_loader_iter=None):
n_models = len(models)
device = state.device
num_classes = state.num_classes
corrects = np.zeros(n_models, dtype=np.int64)
losses = np.zeros(n_models)
attack_mode = state.mode == 'distill_attack'
total = np.array(0, dtype=np.int64)
if attack_mode:
class_total = np.zeros(num_classes + 1, dtype=np.int64)
# per-class acc & attacked acc. wrt target
class_corrects = np.zeros((n_models, num_classes + 1), dtype=np.int64)
if test_all or test_loader_iter is None: # use raw full iter for test_all
test_loader_iter = iter(state.test_loader)
for model in models:
model.eval()
with torch.no_grad():
for i, (data, target) in enumerate(test_loader_iter):
data, target = data.to(device, non_blocking=True), target.to(device, non_blocking=True)
if attack_mode:
for n in range(num_classes):
class_total[n] += (target == n).sum().item()
for k, model in enumerate(models):
if param_list is None or param_list[k] is None:
output = model(data)
else:
output = model.forward_with_param(data, param_list[k])
if num_classes == 2:
pred = (output > 0.5).to(target.dtype).view(-1)
else:
pred = output.argmax(1) # get the index of the max log-probability
correct_list = pred == target
losses[k] += task_loss(state, output, target, reduction='sum').item() # sum up batch loss
if attack_mode:
for c in range(num_classes):
class_mask = target == c
class_corrects[k, c] += correct_list[class_mask].sum().item()
if c == state.attack_class:
class_corrects[k, -1] += (pred[class_mask] == state.target_class).sum().item()
corrects[k] += correct_list.sum().item()
total += output.size(0)
if not test_all and i + 1 >= state.test_niter:
break
losses /= total
if attack_mode:
class_total[-1] = class_total[state.attack_class]
accs = corrects / total
if attack_mode:
class_accs = class_corrects / class_total[None, :]
for n in range(num_classes):
per_class_accs = class_accs[:, n]
accs_wrt_wrong = corrects - class_corrects[:, state.attack_class] + class_corrects[:, -1]
accs_wrt_wrong = accs_wrt_wrong / total
return np.hstack((accs[:, None], accs_wrt_wrong[:, None], class_accs)), losses
else:
return accs, losses
def fixed_width_fmt(num, width=4, align='>'):
if math.isnan(num):
return '{{:{}{}}}'.format(align, width).format(str(num))
return '{{:{}0.{}f}}'.format(align, width).format(num)[:width]
def _desc_step(state, steps, i):
if i == 0:
return 'before steps'
else:
lr = steps[i - 1][-1]
return 'step {:2d} (lr={})'.format(i, fixed_width_fmt(lr.sum().item(), 6))
# See NOTE [ Evaluation Result Format ] for output format
def format_stepwise_results(state, steps, info, res):
accs = res[1] * 100
losses = res[2]
acc_mus = accs.mean(1)
acc_stds = accs.std(1, unbiased=True)
loss_mus = losses.mean(1)
loss_stds = losses.std(1, unbiased=True)
def format_into_line(*fields, align='>'):
single_fmt = '{{:{}24}}'.format(align)
return ' '.join(single_fmt.format(f) for f in fields)
msgs = [format_into_line('STEP', 'ACCURACY', 'LOSS', align='^')]
acc_fmt = '{{: >8.4f}} {}{{: >5.2f}}%'.format(pm)
loss_fmt = '{{: >8.4f}} {}{{: >5.2f}}'.format(pm)
tested_steps = set(res[0].tolist())
for at_step, acc_mu, acc_std, loss_mu, loss_std in zip(res[0], acc_mus, acc_stds, loss_mus, loss_stds):
if state.mode == 'distill_attack':
msgs.append('-' * 74)
desc = _desc_step(state, steps, at_step)
loss_str = loss_fmt.format(loss_mu, loss_std)
acc_mu = acc_mu.view(-1) # into vector
acc_std = acc_std.view(-1) # into vector
acc_str = acc_fmt.format(acc_mu[0], acc_std[0])
msgs.append(format_into_line(desc, acc_str, loss_str))
if state.mode == 'distill_attack':
msgs.append(format_into_line('acc wrt modified labels', acc_fmt.format(acc_mu[1], acc_std[1])))
for cls_idx, (mu, std) in enumerate(zip(acc_mu[2:-1], acc_std[2:-1])):
msgs.append(format_into_line('class {:>2} acc'.format(cls_idx), acc_fmt.format(mu, std)))
msgs.append(format_into_line('{:>2} predicted as {:>2}'.format(state.attack_class, state.target_class),
acc_fmt.format(acc_mu[-1], acc_std[-1])))
return '{} test results:\n{}'.format(info, '\n'.join(('\t' + m) for m in msgs))
def infinite_iterator(iterable):
while True:
yield from iter(iterable)
# See NOTE [ Evaluation Result Format ] for output format
def evaluate_steps(state, steps, prefix, details='', test_all=False, test_at_steps=None, log_results=True):
models = state.test_models
n_steps = len(steps)
if test_at_steps is None:
test_at_steps = [0, n_steps]
else:
test_at_steps = [(x if x >= 0 else n_steps + 1 + x) for x in test_at_steps]
test_at_steps = set(test_at_steps)
N = len(test_at_steps)
# cache test dataloader iter
if test_all:
test_loader_iter = None
else:
test_loader_iter = infinite_iterator(state.test_loader)
test_nets_desc = '{} {} nets'.format(len(models), state.test_nets_type)
def _evaluate_steps(comment, reset): # returns Tensor [STEP x MODEL]
if len(comment) > 0:
comment = '({})'.format(comment)
pbar_desc = prefix + ' ' + comment
else:
pbar_desc = prefix
if log_results:
pbar = tqdm(total=N, desc=pbar_desc)
at_steps = []
accs = [] # STEP x MODEL (x CLASSES)
totals = [] # STEP x MODEL (x CLASSES)
losses = [] # STEP x MODEL
if reset:
params = [m.reset(state, inplace=False) for m in models]
else:
params = [m.get_param(clone=True) for m in models]
def test_callback(at_step, params):
if at_step not in test_at_steps: # not test_all and
return
acc, loss = evaluate_models(state, models, params, test_all=test_all,
test_loader_iter=test_loader_iter)
at_steps.append(at_step)
accs.append(acc)
losses.append(loss)
if log_results:
pbar.update()
params = train_steps_inplace(state, models, steps, params, callback=test_callback)
if log_results:
pbar.close()
at_steps = torch.as_tensor(at_steps, device=state.device) # STEP
accs = torch.as_tensor(accs, device=state.device) # STEP x MODEL (x CLASS)
losses = torch.as_tensor(losses, device=state.device) # STEP x MODEL
return at_steps, accs, losses
if log_results:
logging.info('')
logging.info('{} {}{}:'.format(prefix, details, ' (test ALL)' if test_all else ''))
res = _evaluate_steps(test_nets_desc, reset=(state.test_nets_type == 'unknown_init'))
if state.distributed:
rcv_lsts = all_gather_coalesced(res[1:])
res = (
res[0], # at_steps
torch.cat([lst[0] for lst in rcv_lsts], 1), # accs
torch.cat([lst[1] for lst in rcv_lsts], 1), # losses
)
if log_results:
result_title = '{} {} ({})'.format(prefix, details, test_nets_desc)
logging.info(format_stepwise_results(state, steps, result_title, res))
logging.info('')
return res