-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
43 lines (33 loc) · 1.28 KB
/
trainer.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
import hydra
from hydra.utils import instantiate
from pytorch_lightning.loggers import TensorBoardLogger
from dataloading.osteosarcomaDataModule import OsteosarcomaDataModule
from network_module import Net
@hydra.main(config_path="config", config_name="config", version_base=None)
def main(cfg):
if cfg.pretrained:
base_run_name = str(cfg.run_name) + "_pretrained"
else:
base_run_name = str(cfg.run_name)
for k in range(cfg.n_folds):
run_name = f"{cfg.experiment_name}/{base_run_name}/{cfg.datamodule.img_size}"
if cfg.n_folds > 1:
run_name += f"/{k}_fold"
tensorboard_logger = TensorBoardLogger(
save_dir="logs",
name=run_name,
)
dm = instantiate(cfg.datamodule, k=k)
dm.prepare_data()
model = instantiate(cfg.model.object, num_classes=dm.num_classes)
net = Net(
model=model,
criterion=instantiate(cfg.criterion, weight=dm.class_weights),
num_classes=dm.num_classes,
optimizer=instantiate(cfg.optimizer, params=model.parameters()),
scheduler=cfg.scheduler,
)
trainer = instantiate(cfg.trainer, logger=tensorboard_logger)
trainer.fit(net, dm)
if __name__ == "__main__":
main()