-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.py
376 lines (293 loc) · 11.4 KB
/
callbacks.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import os
import platform
from typing import Any, Optional
import pytorch_lightning as pl
import yaml
from pytorch_lightning.utilities.types import STEP_OUTPUT
try:
import fcntl
def lock_file(f):
fcntl.lockf(f, fcntl.LOCK_EX)
def unlock_file(f):
f.flush()
os.fsync(f.fileno())
fcntl.lockf(f, fcntl.LOCK_UN)
except ModuleNotFoundError:
def lock_file(f):
pass
def unlock_file(f):
pass
class ValidateTestMixin:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.best_val_score = None
self.val_score = None
self.val_corrects = None
self.n_vals = None
self.test_score = None
self.test_corrects = None
self.n_tests = None
self.do_test = None
def is_best(self, score):
if self.best_val_score is None:
return True
return score > self.best_val_score
def _on_validation_epoch_start(self):
self.n_vals = 0
self.val_corrects = 0
self.do_test = None
self.on_real_validation_epoch_start()
def _on_validation_batch_start(self, batch, batch_idx, dataloader_idx):
if batch is None:
return
if batch_idx == 0 and dataloader_idx == 1:
if self.end_validation():
self.start_test()
def _on_validation_batch_end(self, outputs, batch, batch_idx, dataloader_idx):
if batch is None:
return
if dataloader_idx == 0:
self.end_validation_batch(outputs, batch, batch_idx)
elif dataloader_idx == 1 and self.do_test:
self.end_test_batch(outputs, batch, batch_idx)
def end_validation_batch(self, outputs, batch, batch_idx):
predictions, corrects = outputs
self.val_corrects += sum(corrects)
self.n_vals += len(predictions)
self.on_real_validation_batch_end(outputs, batch, batch_idx)
def end_test_batch(self, outputs, batch, batch_idx):
predictions, corrects = outputs
self.test_corrects += sum(corrects)
self.n_tests += len(predictions)
self.on_real_test_batch_end(outputs, batch, batch_idx)
def _on_validation_epoch_end(self):
if self.do_test:
self.end_test()
else:
self.end_validation()
def end_validation(self):
if self.n_vals == 0:
return
self.val_score = self.val_corrects / self.n_vals * 100
updated = self.update_best_val_score(self.val_score)
self.on_real_validation_epoch_end()
return updated
def update_best_val_score(self, score):
if self.is_best(score):
self.best_val_score = score
self.on_update_best_validation_score()
return True
return False
def start_test(self):
self.do_test = True
self.n_tests = 0
self.test_corrects = 0
self.on_real_test_epoch_start()
def end_test(self):
if self.n_tests == 0:
return
self.test_score = self.test_corrects / self.n_tests * 100
self.on_real_test_epoch_end()
def on_real_validation_epoch_start(self):
pass
def on_real_validation_batch_end(self, outputs, batch, batch_idx: int):
pass
def on_real_validation_epoch_end(self):
pass
def on_update_best_validation_score(self):
pass
def on_real_test_epoch_start(self):
pass
def on_real_test_batch_end(self, outputs, batch, batch_idx: int):
pass
def on_real_test_epoch_end(self):
pass
class ValidateTestHook(ValidateTestMixin):
def on_validation_epoch_start(self):
super()._on_validation_epoch_start()
def on_validation_batch_start(self, batch, batch_idx, dataloader_idx=0):
super()._on_validation_batch_start(batch, batch_idx, dataloader_idx)
def on_validation_batch_end(self, outputs, batch, batch_idx, dataloader_idx=0):
super()._on_validation_batch_end(outputs, batch, batch_idx, dataloader_idx)
def on_validation_epoch_end(self):
super()._on_validation_epoch_end()
def on_test_epoch_start(self):
super().start_test()
def on_test_batch_end(self, outputs, batch, batch_idx: int, dataloader_idx: int = 0):
super().end_test_batch(outputs, batch, batch_idx)
def on_test_epoch_end(self):
super().end_test()
class ValidateTestCallback(ValidateTestMixin, pl.callbacks.Callback):
def __init__(self):
super().__init__()
self.current_epoch = None
def on_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"):
self.current_epoch = trainer.current_epoch
def on_validation_epoch_start(self, trainer: pl.Trainer, pl_module: pl.LightningModule):
super()._on_validation_epoch_start()
def on_validation_batch_start(
self,
trainer: pl.Trainer,
pl_module: pl.LightningModule,
batch,
batch_idx,
dataloader_idx=0,
):
super()._on_validation_batch_start(batch, batch_idx, dataloader_idx)
def on_validation_batch_end(
self,
trainer: pl.Trainer,
pl_module: pl.LightningModule,
outputs,
batch,
batch_idx,
dataloader_idx=0,
):
super()._on_validation_batch_end(outputs, batch, batch_idx, dataloader_idx)
def on_validation_epoch_end(self, trainer: pl.Trainer, pl_module: pl.LightningModule):
super()._on_validation_epoch_end()
def on_test_epoch_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"):
super().start_test()
def on_test_epoch_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"):
super().end_test()
def on_test_batch_end(
self,
trainer: "pl.Trainer",
pl_module: "pl.LightningModule",
outputs: Optional[STEP_OUTPUT],
batch: Any,
batch_idx: int,
dataloader_idx: int = 0,
):
super().end_test_batch(outputs, batch, batch_idx)
class BestScoreSummary(ValidateTestCallback):
DEFAULT_FILENAME = "result"
def __init__(
self, filename, val_keys, test_keys, node=None, save_dir="results", force_filename=False
):
super().__init__()
print(f"Summary : {filename=} {val_keys=} {test_keys=} {node=}")
self.val_keys = [val_keys] if isinstance(val_keys, str) else val_keys
self.test_keys = [test_keys] if isinstance(test_keys, str) else test_keys
self.node = platform.node() if node is None else node
self._init_path(filename, save_dir, force_filename)
self.best_val_epoch = None
def _format_file(self, filename):
return f"{filename}.{self.node}.yaml"
def _init_path(self, name, save_dir, force_filename):
os.makedirs(save_dir, exist_ok=True)
if name is None:
name = self.DEFAULT_FILENAME
filename = name
if not force_filename:
version_cnt = 0
while os.path.exists(os.path.join(save_dir, self._format_file(filename))):
version_cnt += 1
filename = f"{name}.{version_cnt:02d}"
self.filename = filename
print("Summary filename :", filename)
self._path = os.path.join(save_dir, self._format_file(filename))
def on_update_best_validation_score(self):
self.update_result(
f"{self.best_val_score} (epoch={self.current_epoch})", keys=self.val_keys
)
def on_real_test_epoch_end(self):
self.update_result(f"{self.test_score} (epoch={self.current_epoch})", keys=self.test_keys)
def update_result(self, result, keys):
if os.path.exists(self._path):
with open(self._path, "r+") as f:
lock_file(f)
results = yaml.load(f, yaml.FullLoader)
unlock_file(f)
if results is None:
results = {}
else:
results = {}
target = results
for key in keys[:-1]:
if key not in target:
target[key] = {}
if not isinstance(target[key], dict):
if not isinstance(target[key], list):
target[key] = [target[key], {}]
target = target[key][1]
else:
target = target[key]
key = keys[-1]
if key not in target:
target[key] = result
elif isinstance(target[key], list):
target[key][0] = result
elif isinstance(target[key], dict):
target[key] = [result, target[key]]
else:
target[key] = result
with open(self._path, "w") as f:
lock_file(f)
yaml.dump(results, f)
unlock_file(f)
class LogEvaluation(ValidateTestCallback):
def __init__(self, filename, train_setting, save_dir="outputs", node=None):
super().__init__()
print(f"LogTest : {filename=}")
self.best_score = None
self.node = platform.node() if node is None else node
self.train_setting = train_setting
self.valid_path = self._init_path(f"{self.node}.valid.{filename}", save_dir)
self.test_path = self._init_path(f"{self.node}.test.{filename}", save_dir)
print("Log filename :", self.valid_path, self.test_path)
self.outputs = []
@staticmethod
def _format_file(filename):
for c in '<>\\:"/|?*':
filename = filename.replace(c, "")
return f"{filename}.txt"
def _init_path(self, filename, save_dir):
os.makedirs(save_dir, exist_ok=True)
version_cnt = 0
path = os.path.join(save_dir, self._format_file(filename))
while True:
try:
open(path, "x").close()
break
except FileExistsError:
path = os.path.join(
save_dir,
self._format_file(f"{filename}.{(version_cnt := version_cnt + 1):02d}"),
)
return path
def on_real_validation_epoch_start(self):
self.outputs = []
def on_update_best_validation_score(self):
self.write_outputs(
self.valid_path,
f"Epoch:{self.current_epoch} val_score:{self.best_val_score} {self.train_setting}",
)
def on_real_validation_batch_end(self, outputs, batch, batch_idx: int):
self.append_outputs(batch, outputs)
def on_real_test_epoch_start(self):
self.outputs = []
def on_real_test_epoch_end(self):
self.write_outputs(
self.test_path,
f"Epoch:{self.current_epoch} val_score:{self.best_val_score} test_score:{self.test_score} {self.train_setting}",
)
def on_real_test_batch_end(self, outputs, batch, batch_idx: int):
self.append_outputs(batch, outputs)
def append_outputs(self, batch, outputs):
predictions, corrects = outputs
self.outputs += [
(problem, equation, prediction.expr, correct)
for problem, equation, prediction, correct in zip(
batch["problem"], batch["equation"], predictions, corrects
)
]
def write_outputs(self, path, info):
with open(path, "w") as f:
lock_file(f)
f.write(f"{info}\n\n")
for i, (problem, equation, expression, correct) in enumerate(self.outputs):
f.write(f"{i + 1:03}. {problem}\n")
f.write(f"solution : {equation}\n")
f.write(f"predict : {expression} ({'O' if correct else 'X'})\n\n")
unlock_file(f)