-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
75 lines (60 loc) · 2.57 KB
/
main.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
import os
import random
import hydra
import numpy as np
from omegaconf import OmegaConf
from utils.func import *
from train import train, evaluate
from utils.metrics import Estimator
from data.builder import generate_dataset
from modules.builder import generate_model, load_weights
@hydra.main(config_path="configs", config_name="config")
def main(cfg):
# print configuration
print_msg('LOADING CONFIG FILE')
print(OmegaConf.to_yaml(cfg))
# create folder
save_path = cfg.dataset.save_path
if os.path.exists(save_path):
if cfg.base.overwrite:
print_msg('Save path {} exists and will be overwrited.'.format(save_path), warning=True)
else:
new_save_path = add_path_suffix(save_path)
cfg.dataset.save_path = new_save_path
warning = 'Save path {} exists. New save path is set to be {}.'.format(save_path, new_save_path)
print_msg(warning, warning=True)
os.makedirs(cfg.dataset.save_path, exist_ok=True)
OmegaConf.save(config=cfg, f=os.path.join(cfg.dataset.save_path, 'cfg.yaml'))
# check preloading
if cfg.dataset.preload_path:
assert os.path.exists(cfg.dataset.preload_path), 'Preload path does not exist.'
print_msg('Preloading is enabled using {}'.format(cfg.dataset.preload_path))
if cfg.base.random_seed >= 0:
set_seed(cfg.base.random_seed, cfg.base.cudnn_deterministic)
train_dataset, test_dataset, val_dataset = generate_dataset(cfg)
frozen_encoder, model = generate_model(cfg)
estimator = Estimator(cfg.train.metrics, cfg.dataset.num_classes, cfg.train.criterion)
train(
cfg=cfg,
frozen_encoder=frozen_encoder,
model=model,
train_dataset=train_dataset,
val_dataset=val_dataset,
estimator=estimator
)
print('This is the performance of the final model:')
checkpoint = os.path.join(cfg.dataset.save_path, 'final_weights.pt')
load_weights(model, checkpoint)
evaluate(cfg, frozen_encoder, model, test_dataset, estimator)
print('This is the performance of the best validation model:')
checkpoint = os.path.join(cfg.dataset.save_path, 'best_validation_weights.pt')
load_weights(model, checkpoint)
evaluate(cfg, frozen_encoder, model, test_dataset, estimator)
def set_seed(seed, deterministic=False):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = deterministic
if __name__ == '__main__':
main()