-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
79 lines (60 loc) · 1.64 KB
/
utils.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
76
77
78
79
import shutil
import os
import sys
import errno
import numpy as np
import torch
import random
import torch.backends.cudnn as cudnn
def rm(path):
try:
shutil.rmtree(path)
except OSError as e:
if e.errno != errno.ENOENT:
raise
def mkdir(path):
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
class AttrDict(dict):
"""
Subclass dict and define getter-setter.
This behaves as both dict and obj.
"""
def __getattr__(self, key):
return self[key]
def __setattr__(self, key, value):
if key in self.__dict__:
self.__dict__[key] = value
else:
self[key] = value
class Logger(object):
def __init__(self,filename="Default.log"):
self.terminal = sys.stdout
self.log = open(filename,'a')
def write(self,message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
def fix_random_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
def fix_seed_all(cfg):
# fix sedd
fix_random_seed(cfg.BASIC.SEED)
cudnn.benchmark = cfg.CUDNN.BENCHMARK
cudnn.deterministic = cfg.CUDNN.DETERMINISTIC
cudnn.enabled = cfg.CUDNN.ENABLE
def backup_codes(root_dir, res_dir, backup_list):
if os.path.exists(res_dir):
shutil.rmtree(res_dir) # delete
os.makedirs(res_dir)
for name in backup_list:
shutil.copytree(os.path.join(root_dir, name), os.path.join(res_dir, name))
print('codes backup at {}'.format(os.path.join(res_dir, name)))