-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_utils.py
295 lines (224 loc) · 6.38 KB
/
train_utils.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
import sys, os, torch, json, time, utils
import math
from tqdm import tqdm
def model_train_batch(batch, net, opt):
loss, br = net.model_train_batch(batch)
if opt is not None:
if net.acc_count == 0:
opt.zero_grad()
aloss = loss / (net.acc_period * 1.)
aloss.backward()
net.acc_count += 1
if net.acc_count == net.acc_period:
opt.step()
net.acc_count = 0
return br
def model_train(loader, net, opt):
if opt is None:
net.eval()
log_period = 1e20
else:
net.train()
if 'log_period' in net.__dict__:
log_period = net.acc_period * net.log_period
else:
log_period = 1e20
ep_result = {}
bc = 0.
for batch in loader:
bc += 1.
if bc > log_period:
break
if isinstance(batch, dict):
bk = 'vdata'
else:
bk = 0
if 'iter_num' in loader.__dict__:
loader.iter_num += batch[bk].shape[0]
batch_result = model_train_batch(batch, net, opt)
for key in batch_result:
if key not in ep_result:
ep_result[key] = batch_result[key]
else:
ep_result[key] += batch_result[key]
ep_result['batch_count'] = bc
return ep_result
def run_train_epoch(
args,
res,
net,
opt,
train_loader,
val_loader,
LOG_INFO,
do_print,
epoch = None
):
json.dump(res, open(f"{args.outpath}/{args.exp_name}/res.json" ,'w'))
t = time.time()
if epoch is None:
itn = train_loader.iter_num
if do_print:
utils.log_print(f"\nBatch Iter {itn}:", args)
else:
itn = epoch
if do_print:
utils.log_print(f"\nEpoch {itn}:", args)
if train_loader is not None:
train_loader.mode = 'train'
if val_loader is not None:
val_loader.mode = 'train'
train_result = model_train(
train_loader,
net,
opt
)
if epoch is None:
train_itn = train_loader.iter_num
slice_name = 'iters'
else:
train_itn = epoch
slice_name = 'epochs'
utils.update_res(
LOG_INFO,
res['train_plots']['train'],
train_result,
slice_name,
train_itn
)
if do_print:
with torch.no_grad():
val_result = model_train(
val_loader,
net,
None,
)
utils.update_res(
LOG_INFO,
res['train_plots']['val'],
val_result,
slice_name,
train_itn,
)
utils.log_print(
f"Train results: ", args
)
utils.print_results(
LOG_INFO,
train_result,
args,
)
utils.log_print(
f"Val results: ", args
)
utils.print_results(
LOG_INFO,
val_result,
args,
)
utils.make_info_plots(
LOG_INFO,
res['train_plots'],
slice_name,
'train',
args,
)
utils.log_print(
f" Time = {time.time() - t}",
args
)
def run_eval_epoch(
args,
res,
net,
eval_data,
EVAL_LOG_INFO,
itn
):
with torch.no_grad():
net.eval()
t = time.time()
eval_results = {}
for key, loader in eval_data:
if loader.mode == 'train':
loader.mode = 'eval'
net.vis_mode = (key, itn)
net.init_vis_logic()
eval_results[key] = model_eval(
args,
loader,
net,
)
net.save_vis_logic()
utils.log_print(
f"Evaluation {key} set results:",
args
)
utils.print_results(
EVAL_LOG_INFO,
eval_results[key],
args
)
utils.log_print(f"Eval Time = {time.time() - t}", args)
res['eval_iters'].append(itn)
utils.make_comp_plots(
EVAL_LOG_INFO,
eval_results,
res['eval_plots'],
res['eval_iters'],
args,
'eval'
)
def check_early_stop(res, args, obj_dir):
eps = res['eval_iters']
if 'val' not in res['eval_plots'] or \
args.es_metric not in res['eval_plots']['val']:
utils.log_print("!! SKIPPING EARLY STOP !!", args)
return -1
metric_res = torch.tensor(res['eval_plots']['val'][args.es_metric])
cur_ep = eps[-1]
for i, ep in enumerate(eps[:metric_res.shape[0]]):
if cur_ep - ep <= args.es_patience:
metric_res[i] -= args.es_threshold
if obj_dir == 'high':
best_ep_ind = metric_res.argmax().item()
elif obj_dir == 'low':
best_ep_ind = metric_res.argmin().item()
else:
assert False
best_ep = eps[best_ep_ind]
# early stopping logic
if cur_ep - best_ep > args.es_patience:
utils.log_print(
f"Stopping early at epoch {cur_ep}, "
f"choosing iter {best_ep} with val {args.es_metric} "
f"of {metric_res[best_ep_ind].item()}",
args
)
utils.log_print(
f"Final test value for {args.es_metric} : {res['eval_plots']['test'][args.es_metric][best_ep_ind]}",
args
)
return best_ep
return -1
def model_eval(
args,
loader,
net,
):
res = {}
pbar = tqdm(total=math.ceil(loader.eval_size / loader.eval_batch_size))
for count, batch in enumerate(loader):
_res = net.model_eval_fn(
batch
)
for k,v in _res.items():
if k not in res:
res[k] = v
else:
res[k] += v
pbar.update(1)
res['count'] = count + 1
res['nc'] = 1
pbar.close()
return res