-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
60 lines (52 loc) · 3.49 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
import inspect
import os
import random
import shutil
import sys
import time
import numpy as np
import torch
COLORS = np.array([0, 0, 0, 0.000, 0.447, 0.741, 0.850, 0.325, 0.098, 0.929, 0.694, 0.125, 0.494, 0.184, 0.556, 0.466, 0.674, 0.188, 0.301, 0.745, 0.933, 0.635, 0.078, 0.184, 0.300, 0.300, 0.300, 0.600, 0.600, 0.600, 1.000, 0.000, 0.000, 1.000, 0.500, 0.000, 0.749, 0.749, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000, 1.000, 0.667, 0.000, 1.000, 0.333, 0.333, 0.000, 0.333, 0.667, 0.000, 0.333, 1.000, 0.000, 0.667, 0.333, 0.000, 0.667, 0.667, 0.000, 0.667, 1.000, 0.000, 1.000, 0.333, 0.000, 1.000, 0.667, 0.000, 1.000, 1.000, 0.000, 0.000, 0.333, 0.500, 0.000, 0.667, 0.500, 0.000, 1.000, 0.500, 0.333, 0.000, 0.500, 0.333, 0.333, 0.500, 0.333, 0.667, 0.500, 0.333, 1.000, 0.500, 0.667, 0.000, 0.500, 0.667, 0.333, 0.500, 0.667, 0.667, 0.500, 0.667, 1.000, 0.500, 1.000, 0.000, 0.500, 1.000, 0.333, 0.500, 1.000, 0.667, 0.500, 1.000, 1.000, 0.500, 0.000, 0.333, 1.000, 0.000, 0.667, 1.000, 0.000, 1.000, 1.000, 0.333, 0.000, 1.000, 0.333, 0.333, 1.000, 0.333, 0.667, 1.000, 0.333, 1.000, 1.000, 0.667, 0.000, 1.000, 0.667, 0.333, 1.000, 0.667, 0.667, 1.000, 0.667, 1.000, 1.000, 1.000, 0.000, 1.000, 1.000, 0.333, 1.000, 1.000, 0.667, 1.000, 0.333, 0.000, 0.000, 0.500, 0.000, 0.000, 0.667, 0.000, 0.000, 0.833, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000, 0.167, 0.000, 0.000, 0.333, 0.000, 0.000, 0.500, 0.000, 0.000, 0.667, 0.000, 0.000, 0.833, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000, 0.167, 0.000, 0.000, 0.333, 0.000, 0.000, 0.500, 0.000, 0.000, 0.667, 0.000, 0.000, 0.833, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000, 0.143, 0.143, 0.143, 0.286, 0.286, 0.286, 0.429, 0.429, 0.429, 0.571, 0.571, 0.571, 0.714, 0.714, 0.714, 0.857, 0.857, 0.857, 0.000, 0.447, 0.741, 0.314, 0.717, 0.741, 0.50, 0.5, 0]).astype(np.float32).reshape(-1, 3)
def save_dependencies_files(root_path):
abs_cwd = os.path.abspath(os.getcwd())
# 获取当前加载的所有模块
modules = list(sys.modules.values())
# 获取所有依赖的文件的路径
dependency_files = []
for module in modules:
try:
file_path = os.path.abspath(inspect.getfile(module))
if os.path.isfile(file_path) and os.path.commonpath([abs_cwd, file_path]) == abs_cwd:
dependency_files.append(file_path)
except TypeError:
# 忽略无法获取文件路径的模块
continue
dependency_files = list(set(dependency_files))
for file_path in dependency_files:
save_path = os.path.join(root_path, os.path.relpath(file_path, abs_cwd))
os.makedirs(os.path.dirname(save_path), exist_ok=True)
if os.path.exists(save_path):
os.remove(save_path)
shutil.copy(file_path, save_path)
return dependency_files
def fix_seeds(seed, with_torch=True, with_cuda=True):
"""Fixed available seeds for reproducibility.
Args:
seed: [int] Seed value.
with_torch: Flag. If true, torch-related seeds are fixed.
with_cuda: Flag. If true, torch+cuda-related seeds are fixed
"""
random.seed(seed)
np.random.seed(seed)
if with_torch:
torch.manual_seed(seed)
if with_cuda:
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = False
return lambda worker_id: fix_seeds(seed+worker_id)
def time_synchronized():
if torch.cuda.is_available():
torch.cuda.synchronize()
return time.time()