-
Notifications
You must be signed in to change notification settings - Fork 15
/
misc.py
57 lines (43 loc) · 1.77 KB
/
misc.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
import json
import os
import random
from datetime import date
import numpy as np
import torch
import torch.backends.cudnn as cudnn
def _get_experiment_index(experiment_path):
idx = 0
while os.path.exists(experiment_path + "_" + str(idx)):
idx += 1
return idx
def create_experiment_export_folder(args):
experiment_dir, experiment_description = args.experiment_dir, args.experiment_description
if not os.path.exists(experiment_dir):
os.mkdir(experiment_dir)
experiment_path = get_name_of_experiment_path(experiment_dir, experiment_description)
print(os.path.abspath(experiment_path))
os.mkdir(experiment_path)
print("folder created: " + os.path.abspath(experiment_path))
return experiment_path
def export_experiments_config_as_json(args, experiment_path):
with open(os.path.join(experiment_path, 'config.json'), 'w') as outfile:
json.dump(vars(args), outfile, indent=2)
def get_name_of_experiment_path(experiment_dir, experiment_description):
experiment_path = os.path.join(experiment_dir, (experiment_description + "_" + str(date.today())))
idx = _get_experiment_index(experiment_path)
experiment_path = experiment_path + "_" + str(idx)
return experiment_path
def fix_random_seed_as(random_seed):
if random_seed == -1:
random_seed = np.random.randint(100000)
print("RANDOM SEED: {}".format(random_seed))
random.seed(random_seed)
torch.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed)
np.random.seed(random_seed)
cudnn.deterministic = True
cudnn.benchmark = False
def set_up_gpu(args):
os.environ['CUDA_VISIBLE_DEVICES'] = args.device_idx
args.num_gpu = len(args.device_idx.split(","))
args.device = 'cuda' if torch.cuda.is_available() else 'cpu'