main.py
9.85 KB
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
# -*- coding: utf-8 -*-
"""main.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/16Rn0IDJTE6vJMOZtafDAI19AvGD9eK4C
"""
import argparse
import importlib
import json
import logging
import os
import pprint
import sys
import dill
import torch
import wandb
from box import Box
from torch.utils.data import DataLoader
from src.common.dataset import get_dataset
from lib.utils import logging as logging_utils, os as os_utils, optimizer as optimizer_utils
from lib.base_trainer import Trainer
import easydict
def parser_setup():
# define argparsers
str2bool = os_utils.str2bool
listorstr = os_utils.listorstr
parser = easydict.EasyDict({
"debug":False,
"config":None,
"seed":0,
"wandb_use":False,
"wandb_run_id":None,
"wandb.watch":False,
"project":"brain-age",
"exp_name":None,
"device":"cuda",
"result_folder":"a",
"mode":["test", "train"],
"statefile":None,
"data" : {
"name":"brain_age",
"root_path":"/content/drive/MyDrive/2d-slice-set-networks-for-brain-age-master/data",
"train_csv":"data/train0609.csv",
"valid_csv":"data/valid0609.csv",
"test_csv":"data/test0609.csv",
"feat_csv":None,
"train_num_sample":506,
"frame_dim":1,
"frame_keep_style":"random",
"frame_keep_fraction":1,
"impute":"drop",
},
"model" : {
"name":"regression",
"arch": {
"file":"src/arch/brain_age_3d.py",
"lstm_feat_dim":2,
"lstm_latent_dim":128,
"attn_num_heads":1,
"attn_dim":32,
"attn_drop":False,
"agg_fn":"attention"
}
},
"train":{
"batch_size":8,
"patience":100,
"max_epoch":100,
"optimizer":"adam",
"lr":1e-3,
"weight_decay":1e-4,
"gradient_norm_clip":-1,
"save_strategy":["best", "last"],
"log_every":100,
"stopping_criteria":"loss",
"stopping_criteria_direction":"lower",
"evaluations":None,
"optimizer_momentum":None,
"scheduler":None,
"scheduler_gamma":None,
"scheduler_milestones":None,
"scheduler_patience":None,
"scheduler_step_size":None,
"scheduler_load_on_reduce":None,
},
"test":{
"batch_size":8,
"evaluations":None,
"eval_model":"best",
},
"_actions":None,
"_defaults":None
})
print(parser.seed)
return parser
if __name__ == "__main__":
# set seeds etc here
torch.backends.cudnn.benchmark = True
# define logger etc
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s")
logger = logging.getLogger()
parser = parser_setup()
#config = os_utils.parse_args(parser)
config = parser
logger.info("Config:")
logger.info(pprint.pformat(config, indent=4))
os_utils.safe_makedirs(config.result_folder)
statefile, run_id, result_folder = os_utils.get_state_params(
config.wandb_use, config.wandb_run_id, config.result_folder, config.statefile
)
config.statefile = statefile
config.wandb_run_id = run_id
config.result_folder = result_folder
if statefile is not None:
data = torch.load(open(statefile, "rb"), pickle_module=dill)
epoch = data["epoch"]
if epoch >= config.train.max_epoch:
logger.error("Aleady trained upto max epoch; exiting")
sys.exit()
if config.wandb_use:
wandb.init(
name=config.exp_name if config.exp_name is not None else config.result_folder,
config=config.to_dict(),
project=config.project,
dir=config.result_folder,
resume=config.wandb_run_id,
id=config.wandb_run_id,
sync_tensorboard=True,
)
logger.info(f"Starting wandb with id {wandb_run_id}")
# NOTE: WANDB creates git patch so we probably can get rid of this in future
os_utils.copy_code("src", config.result_folder, replace=True,)
json.dump(
config,
open(f"{wandb_run.dir if config.wandb_use else config.result_folder}/config.json", "w")
)
logger.info("Getting data and dataloaders")
data, meta = get_dataset(**config.data, device=config.device, replace=True, frac=2000)
# num_workers = max(min(os.cpu_count(), 8), 1)
num_workers = os.cpu_count()
logger.info(f"Using {num_workers} workers")
train_loader = DataLoader(data["train"], shuffle=True, batch_size=config.train.batch_size,
num_workers=num_workers)
valid_loader = DataLoader(data["valid"], shuffle=False, batch_size=config.test.batch_size,
num_workers=num_workers)
test_loader = DataLoader(data["test"], shuffle=False, batch_size=config.test.batch_size,
num_workers=num_workers)
logger.info("Getting model")
# load arch module
arch_module = importlib.import_module(config.model.arch.file.replace("/", ".")[:-3])
model_arch = arch_module.get_arch(
input_shape=meta.get("input_shape"), output_size=meta.get("num_class"),
**config.model.arch,
slice_dim=config.data.frame_dim
)
# declaring models
if config.model.name in "regression":
from src.models.regression import Regression
model = Regression(**model_arch)
else:
raise Exception("Unknown model")
model.to(config.device)
model.stats()
if config.wandb_use and config.wandb_watch:
wandb_watch(model, log="all")
# declaring trainer
optimizer, scheduler = optimizer_utils.get_optimizer_scheduler(
model,
lr=config.train.lr,
optimizer=config.train.optimizer,
opt_params={
"weight_decay": config.train.get("weight_decay", 1e-4),
"momentum" : config.train.get("optimizer_momentum", 0.9)
},
scheduler=config.train.get("scheduler", None),
scheduler_params={
"gamma" : config.train.get("scheduler_gamma", 0.1),
"milestones" : config.train.get("scheduler_milestones", [100, 200, 300]),
"patience" : config.train.get("scheduler_patience", 100),
"step_size" : config.train.get("scheduler_step_size", 100),
"load_on_reduce": config.train.get("scheduler_load_on_reduce"),
"mode" : "max" if config.train.get(
"stopping_criteria_direction") == "bigger" else "min"
},
)
trainer = Trainer(model, optimizer, scheduler=scheduler, statefile=config.statefile,
result_dir=config.result_folder, log_every=config.train.log_every,
save_strategy=config.train.save_strategy,
patience=config.train.patience,
max_epoch=config.train.max_epoch,
stopping_criteria=config.train.stopping_criteria,
gradient_norm_clip=config.train.gradient_norm_clip,
stopping_criteria_direction=config.train.stopping_criteria_direction,
evaluations=Box({"train": config.train.evaluations,
"test" : config.test.evaluations}))
if "train" in config.mode:
logger.info("starting training")
print(train_loader.dataset)
trainer.train(train_loader, valid_loader)
logger.info("Training done;")
# copy current step and write test results to
step_to_write = trainer.step
step_to_write += 1
if "test" in config.mode and config.test.eval_model == "best":
if os.path.exists(f"{trainer.result_dir}/best_model.pt"):
logger.info("Loading best model")
trainer.load(f"{trainer.result_dir}/best_model.pt")
else:
logger.info("eval_model is best, but best model not found ::: evaling last model")
else:
logger.info("eval model is not best, so skipping loading at end of training")
if "test" in config.mode:
logger.info("evaluating model on test set")
logger.info(f"Model was trained upto {trainer.epoch}")
# copy current step and write test results to
step_to_write = trainer.step
step_to_write += 1
loss, aux_loss = trainer.test(train_loader, test_loader)
logging_utils.loss_logger_helper(loss, aux_loss, writer=trainer.summary_writer,
force_print=True, step=step_to_write,
epoch=trainer.epoch,
log_every=trainer.log_every, string="test",
new_line=True)
loss, aux_loss = trainer.test(train_loader, train_loader)
logging_utils.loss_logger_helper(loss, aux_loss, writer=trainer.summary_writer,
force_print=True, step=step_to_write,
epoch=trainer.epoch,
log_every=trainer.log_every, string="train_eval",
new_line=True)
loss, aux_loss = trainer.test(train_loader, valid_loader)
logging_utils.loss_logger_helper(loss, aux_loss, writer=trainer.summary_writer,
force_print=True, step=step_to_write,
epoch=trainer.epoch,
log_every=trainer.log_every, string="valid_eval",
new_line=True)