-
Notifications
You must be signed in to change notification settings - Fork 84
/
base_runner.py
201 lines (152 loc) · 5.38 KB
/
base_runner.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
import time
from typing import Dict
import setproctitle
import torch
from torch import nn
from torch.utils.data import DataLoader
from easytorch import Runner
from easytorch.utils import master_only
from easytorch.core.data_loader import build_data_loader
class BaseRunner(Runner):
"""
An expanded easytorch runner for benchmarking time series models.
- Support test loader and test process.
- Support setup_graph for the models acting like tensorflow.
"""
def __init__(self, cfg: dict):
"""Init
Args:
cfg (dict): all in one configurations
"""
super().__init__(cfg)
# validate every `val_interval` epoch
self.val_interval = cfg["VAL"].get("INTERVAL", 1)
# test every `test_interval` epoch
self.test_interval = cfg["TEST"].get("INTERVAL", 1)
# declare data loader
self.train_data_loader = None
self.val_data_loader = None
# set proctitle
proctitle_name = "{0}({1})".format(cfg["MODEL"].get(
"NAME", " "), cfg.get("DATASET_NAME", " "))
setproctitle.setproctitle("{0}@BasicTS".format(proctitle_name))
@staticmethod
def define_model(cfg: Dict) -> nn.Module:
return cfg["MODEL"]["ARCH"](**cfg.MODEL.PARAM)
def build_train_data_loader(self, cfg: dict) -> DataLoader:
"""Support "setup_graph" for the models acting like tensorflow.
Args:
cfg (dict): all in one configurations
Returns:
DataLoader: train dataloader
"""
train_data_loader = super().build_train_data_loader(cfg)
if cfg["TRAIN"].get("SETUP_GRAPH", False):
for data in train_data_loader:
self.setup_graph(data)
break
return train_data_loader
def setup_graph(self, data: torch.Tensor):
"""Setup all parameters and the computation graph.
Args:
data (torch.Tensor): data necessary for a forward pass
"""
pass
def init_training(self, cfg: dict):
"""Initialize training and support test dataloader.
Args:
cfg (dict): config
"""
super().init_training(cfg)
# init test
if hasattr(cfg, "TEST"):
self.init_test(cfg)
@master_only
def init_test(self, cfg: dict):
"""Initialize test.
Args:
cfg (dict): config
"""
self.test_interval = cfg["TEST"].get("INTERVAL", 1)
self.test_data_loader = self.build_test_data_loader(cfg)
self.register_epoch_meter("test_time", "test", "{:.2f} (s)", plt=False)
def build_test_data_loader(self, cfg: dict) -> DataLoader:
"""Build val dataset and dataloader.
Build dataset by calling ```self.build_train_dataset```,
build dataloader by calling ```build_data_loader```.
Args:
cfg (dict): config
Returns:
val data loader (DataLoader)
"""
dataset = self.build_test_dataset(cfg)
return build_data_loader(dataset, cfg["TEST"]["DATA"])
@staticmethod
def build_test_dataset(cfg: dict):
"""It can be implemented to a build dataset for test.
Args:
cfg (dict): config
Returns:
val dataset (Dataset)
"""
raise NotImplementedError()
# support test process
def on_epoch_end(self, epoch: int):
"""Callback at the end of an epoch.
Args:
epoch (int): current epoch.
"""
# print train meters
self.print_epoch_meters("train")
# tensorboard plt meters
self.plt_epoch_meters("train", epoch)
# validate
if self.val_data_loader is not None and epoch % self.val_interval == 0:
self.validate(train_epoch=epoch)
# test
if self.test_data_loader is not None and epoch % self.test_interval == 0:
self.test_process(train_epoch=epoch)
# save model
self.save_model(epoch)
# reset meters
self.reset_epoch_meters()
@torch.no_grad()
@master_only
def test_process(self, cfg: dict = None, train_epoch: int = None):
"""The whole test process.
Args:
cfg (dict, optional): config
train_epoch (int, optional): current epoch if in training process.
"""
# init test if not in training process
if train_epoch is None:
self.init_test(cfg)
self.on_test_start()
test_start_time = time.time()
self.model.eval()
# test
self.test()
test_end_time = time.time()
self.update_epoch_meter("test_time", test_start_time - test_end_time)
# print test meters
self.print_epoch_meters("test")
if train_epoch is not None:
# tensorboard plt meters
self.plt_epoch_meters("test", train_epoch // self.test_interval)
self.on_test_end()
@master_only
def on_test_start(self):
"""Callback at the start of testing.
"""
pass
@master_only
def on_test_end(self):
"""Callback at the end of testing.
"""
pass
def test(self, train_epoch: int = None):
"""It can be implemented to define testing details.
Args:
train_epoch (int, optional): current epoch if in training process.
"""
raise NotImplementedError()