forked from timgaripov/dnn-mode-connectivity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_curve.py
187 lines (160 loc) · 6.05 KB
/
eval_curve.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
import argparse
import numpy as np
import os
import tabulate
import torch
import torch.nn.functional as F
import data
import models
import curves
import utils
parser = argparse.ArgumentParser(description='DNN curve evaluation')
parser.add_argument('--dir', type=str, default='/tmp/eval', metavar='DIR',
help='training directory (default: /tmp/eval)')
parser.add_argument('--num_points', type=int, default=61, metavar='N',
help='number of points on the curve (default: 61)')
parser.add_argument('--dataset', type=str, default='CIFAR10', metavar='DATASET',
help='dataset name (default: CIFAR10)')
parser.add_argument('--use_test', action='store_true',
help='switches between validation and test set (default: validation)')
parser.add_argument('--transform', type=str, default='VGG', metavar='TRANSFORM',
help='transform name (default: VGG)')
parser.add_argument('--data_path', type=str, default=None, metavar='PATH',
help='path to datasets location (default: None)')
parser.add_argument('--batch_size', type=int, default=128, metavar='N',
help='input batch size (default: 128)')
parser.add_argument('--num_workers', type=int, default=4, metavar='N',
help='number of workers (default: 4)')
parser.add_argument('--model', type=str, default=None, metavar='MODEL',
help='model name (default: None)')
parser.add_argument('--curve', type=str, default=None, metavar='CURVE',
help='curve type to use (default: None)')
parser.add_argument('--num_bends', type=int, default=3, metavar='N',
help='number of curve bends (default: 3)')
parser.add_argument('--ckpt', type=str, default=None, metavar='CKPT',
help='checkpoint to eval (default: None)')
parser.add_argument('--wd', type=float, default=1e-4, metavar='WD',
help='weight decay (default: 1e-4)')
args = parser.parse_args()
os.makedirs(args.dir, exist_ok=True)
torch.backends.cudnn.benchmark = True
loaders, num_classes = data.loaders(
args.dataset,
args.data_path,
args.batch_size,
args.num_workers,
args.transform,
args.use_test,
shuffle_train=False
)
architecture = getattr(models, args.model)
curve = getattr(curves, args.curve)
model = curves.CurveNet(
num_classes,
curve,
architecture.curve,
args.num_bends,
architecture_kwargs=architecture.kwargs,
)
model.cuda()
checkpoint = torch.load(args.ckpt)
model.load_state_dict(checkpoint['model_state'])
criterion = F.cross_entropy
regularizer = curves.l2_regularizer(args.wd)
T = args.num_points
ts = np.linspace(0.0, 1.0, T)
tr_loss = np.zeros(T)
tr_nll = np.zeros(T)
tr_acc = np.zeros(T)
te_loss = np.zeros(T)
te_nll = np.zeros(T)
te_acc = np.zeros(T)
tr_err = np.zeros(T)
te_err = np.zeros(T)
dl = np.zeros(T)
previous_weights = None
columns = ['t', 'Train loss', 'Train nll', 'Train error (%)', 'Test nll', 'Test error (%)']
t = torch.FloatTensor([0.0]).cuda()
for i, t_value in enumerate(ts):
t.data.fill_(t_value)
weights = model.weights(t)
if previous_weights is not None:
dl[i] = np.sqrt(np.sum(np.square(weights - previous_weights)))
previous_weights = weights.copy()
utils.update_bn(loaders['train'], model, t=t)
tr_res = utils.test(loaders['train'], model, criterion, regularizer, t=t)
te_res = utils.test(loaders['test'], model, criterion, regularizer, t=t)
tr_loss[i] = tr_res['loss']
tr_nll[i] = tr_res['nll']
tr_acc[i] = tr_res['accuracy']
tr_err[i] = 100.0 - tr_acc[i]
te_loss[i] = te_res['loss']
te_nll[i] = te_res['nll']
te_acc[i] = te_res['accuracy']
te_err[i] = 100.0 - te_acc[i]
values = [t, tr_loss[i], tr_nll[i], tr_err[i], te_nll[i], te_err[i]]
table = tabulate.tabulate([values], columns, tablefmt='simple', floatfmt='10.4f')
if i % 40 == 0:
table = table.split('\n')
table = '\n'.join([table[1]] + table)
else:
table = table.split('\n')[2]
print(table)
def stats(values, dl):
min = np.min(values)
max = np.max(values)
avg = np.mean(values)
int = np.sum(0.5 * (values[:-1] + values[1:]) * dl[1:]) / np.sum(dl[1:])
return min, max, avg, int
tr_loss_min, tr_loss_max, tr_loss_avg, tr_loss_int = stats(tr_loss, dl)
tr_nll_min, tr_nll_max, tr_nll_avg, tr_nll_int = stats(tr_nll, dl)
tr_err_min, tr_err_max, tr_err_avg, tr_err_int = stats(tr_err, dl)
te_loss_min, te_loss_max, te_loss_avg, te_loss_int = stats(te_loss, dl)
te_nll_min, te_nll_max, te_nll_avg, te_nll_int = stats(te_nll, dl)
te_err_min, te_err_max, te_err_avg, te_err_int = stats(te_err, dl)
print('Length: %.2f' % np.sum(dl))
print(tabulate.tabulate([
['train loss', tr_loss[0], tr_loss[-1], tr_loss_min, tr_loss_max, tr_loss_avg, tr_loss_int],
['train error (%)', tr_err[0], tr_err[-1], tr_err_min, tr_err_max, tr_err_avg, tr_err_int],
['test nll', te_nll[0], te_nll[-1], te_nll_min, te_nll_max, te_nll_avg, te_nll_int],
['test error (%)', te_err[0], te_err[-1], te_err_min, te_err_max, te_err_avg, te_err_int],
], [
'', 'start', 'end', 'min', 'max', 'avg', 'int'
], tablefmt='simple', floatfmt='10.4f'))
np.savez(
os.path.join(args.dir, 'curve.npz'),
ts=ts,
dl=dl,
tr_loss=tr_loss,
tr_loss_min=tr_loss_min,
tr_loss_max=tr_loss_max,
tr_loss_avg=tr_loss_avg,
tr_loss_int=tr_loss_int,
tr_nll=tr_nll,
tr_nll_min=tr_nll_min,
tr_nll_max=tr_nll_max,
tr_nll_avg=tr_nll_avg,
tr_nll_int=tr_nll_int,
tr_acc=tr_acc,
tr_err=tr_err,
tr_err_min=tr_err_min,
tr_err_max=tr_err_max,
tr_err_avg=tr_err_avg,
tr_err_int=tr_err_int,
te_loss=te_loss,
te_loss_min=te_loss_min,
te_loss_max=te_loss_max,
te_loss_avg=te_loss_avg,
te_loss_int=te_loss_int,
te_nll=te_nll,
te_nll_min=te_nll_min,
te_nll_max=te_nll_max,
te_nll_avg=te_nll_avg,
te_nll_int=te_nll_int,
te_acc=te_acc,
te_err=te_err,
te_err_min=te_err_min,
te_err_max=te_err_max,
te_err_avg=te_err_avg,
te_err_int=te_err_int,
)