Repository navigation
Expand file tree
/
Copy pathutils_misc.py
More file actions
55 lines (45 loc) · 1.51 KB
/
Copy pathutils_misc.py
File metadata and controls
55 lines (45 loc) · 1.51 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
import torch
import os
import random
import numpy as np
import pickle
import datetime
import os.path
def prepare_save_dir(args, filename):
""" Create saving directory."""
runner_name = os.path.basename(filename).split(".")[0]
model_dir = 'data/experiments/{}/{}/'.format(runner_name, args.name)
args.savedir = model_dir
if not os.path.exists(args.savedir):
os.makedirs(args.savedir)
return args
def write_txt(args, string):
""" Write the string in a text file."""
with open(args.savedir + 'out.txt', 'a') as f:
f.write(string + " \n")
def seed_torch(seed=1029):
""" Seed the run."""
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def create_logger(args, metrics):
""" Create a logger."""
args.logger = {}
args.logger['time_start'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
i = 0
while os.path.isfile(args.savedir + str(i) + '.pkl'):
i += 1
args.logger['pkl_path'] = args.savedir + str(i) + '.pkl'
args.logger['path'] = args.savedir + str(i)
for metric in metrics:
args.logger[metric] = []
return args
def save_logger(args):
""" Save the logger."""
with open(args.logger['pkl_path'], "wb") as output_file:
pickle.dump(vars(args), output_file)