-
Notifications
You must be signed in to change notification settings - Fork 30
/
run_and_store_results.py
57 lines (41 loc) · 1.51 KB
/
run_and_store_results.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
import logging
import os
import pickle
from main import get_config, check_config
from libmultilabel.logging import add_stream_handler
from tests.nn.utils import get_names, get_components_from_trainer
def store_components_from_trainer(trainer):
"""Store the components in trainer to conduct API test.
Args:
trainer (TorchTrainer): A wrapper for training neural network models with pytorch lightning trainer.
"""
names = get_names()
components = get_components_from_trainer(trainer)
# Store the components by pickle
for name, component in zip(names, components):
with open(os.path.join(trainer.checkpoint_dir, f"{name}.p"), "wb") as f:
pickle.dump(component, f, protocol=pickle.HIGHEST_PROTOCOL)
logging.info(f"Components for testing saved to {trainer.checkpoint_dir}.")
def main():
# Get config
config = get_config()
check_config(config)
# Set up logger
log_level = logging.WARNING if config.silent else logging.INFO
add_stream_handler(log_level)
logging.info(f"Run name: {config.run_name}")
if config.linear:
from linear_trainer import linear_run
linear_run(config)
else:
from torch_trainer import TorchTrainer
trainer = TorchTrainer(config) # initialize trainer
store_components_from_trainer(trainer)
# train
if not config.eval:
trainer.train()
# test
if "test" in trainer.datasets:
trainer.test()
if __name__ == "__main__":
main()