forked from Karel911/TRACER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (54 loc) · 2.13 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
import os
import shutil
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
import pprint
import random
import warnings
import torch
import numpy as np
from trainer import Trainer, Tester
from shutil import copyfile
from config import getConfig
warnings.filterwarnings('ignore')
cfg = getConfig()
def prepare_trained_model_file() -> str:
trained_model_path = os.path.join(cfg.model_path, cfg.dataset, f"TE{cfg.arch}_0")
os.makedirs(trained_model_path, exist_ok=True)
trained_model_file = os.path.join(cfg.model_path, f"TRACER-Efficient-{cfg.arch}.pth")
copyfile(trained_model_file, os.path.join(trained_model_path, "best_model.pth"))
return trained_model_path
def main(cfg):
print('<---- Training Params ---->')
pprint.pprint(cfg)
# Random Seed
seed = cfg.seed
os.environ['PYTHONHASHSEED'] = str(seed)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
save_path = os.path.join(cfg.model_path, cfg.dataset, f'TE{cfg.arch}_{str(cfg.exp_num)}')
if cfg.action == 'train':
# Create model directory
os.makedirs(save_path, exist_ok=True)
Trainer(cfg, save_path)
elif cfg.action == 'test':
datasets = ['DUTS', 'DUT-O', 'HKU-IS', 'ECSSD', 'PASCAL-S']
for dataset in datasets:
cfg.dataset = dataset
test_loss, test_mae, test_maxf, test_avgf, test_s_m = Tester(cfg, save_path).test()
print(f'Test Loss:{test_loss:.3f} | MAX_F:{test_maxf:.4f} '
f'| AVG_F:{test_avgf:.4f} | MAE:{test_mae:.4f} | S_Measure:{test_s_m:.4f}')
elif cfg.action == 'apply':
trained_model_path = prepare_trained_model_file()
Tester(cfg, save_path, have_gt=False).test()
print(trained_model_path)
input('')
shutil.rmtree(trained_model_path)
else:
raise ValueError("action should be train, test or apply.")
if __name__ == '__main__':
main(cfg)