-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_option.py
128 lines (104 loc) · 4.18 KB
/
utils_option.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import json
import os
from datetime import datetime
from typing import Any, Dict
import commentjson
'''
# --------------------------------------------
# Hongyi Zheng (github: https://github.com/natezhenghy)
# 07/Apr/2021
# --------------------------------------------
# Kai Zhang (github: https://github.com/cszn)
# 03/Mar/2019
# --------------------------------------------
# https://github.com/xinntao/BasicSR
# --------------------------------------------
'''
def get_timestamp() -> str:
return datetime.now().strftime('_%y%m%d_%H%M%S')
def parse(opt_path: str, is_train: bool = True) -> Dict[str, Any]:
# ----------------------------------------
# initialize opt
# ----------------------------------------
with open(opt_path) as file:
opt: Dict[str, Any] = commentjson.load(file)
opt['opt_path'] = opt_path
opt['is_train'] = is_train
# ----------------------------------------
# data
# ----------------------------------------
if 'scale' not in opt['data']:
opt['data']['scale'] = 1
# ----------------------------------------
# datasets
# ----------------------------------------
for phase in ['train', 'test']:
dataset = opt['data'][phase]
dataset['type'] = opt['data']['type']
dataset['phase'] = phase
dataset['scale'] = opt['data']['scale'] # broadcast
dataset['n_channels'] = opt['data']['n_channels'] # broadcast
if 'k_size' in opt['data']:
dataset['k_size'] = opt['data']['k_size'] # broadcast
if 'dataroot_HX' in dataset and dataset['dataroot_HX'] is not None:
dataset['dataroot_HX'] = os.path.expanduser(dataset['dataroot_HX'])
if 'dataroot_HY' in dataset and dataset['dataroot_HY'] is not None:
dataset['dataroot_HY'] = os.path.expanduser(dataset['dataroot_HY'])
if 'dataroot_L' in dataset and dataset['dataroot_L'] is not None:
dataset['dataroot_L'] = os.path.expanduser(dataset['dataroot_L'])
# ----------------------------------------
# path
# ----------------------------------------
for key, path in opt['path'].items():
if path and key in opt['path']:
opt['path'][key] = os.path.expanduser(path)
path_task = os.path.join(opt['path']['root'], opt['task'])
opt['path']['task'] = path_task
opt['path']['log'] = path_task
opt['path']['options'] = os.path.join(path_task, 'options')
if is_train:
opt['path']['models'] = os.path.join(path_task, 'models')
opt['path']['images'] = os.path.join(path_task, 'images')
else: # test
opt['path']['images'] = os.path.join(path_task, 'test_images')
# ----------------------------------------
# network
# ----------------------------------------
opt['netG']['type'] = opt['data']['type']
opt['netG']['in_nc'] = opt['netG']['out_nc'] = opt['data']['n_channels']
opt['netG']['scale'] = opt['data']['scale']
# ----------------------------------------
# GPU devices
# ----------------------------------------
gpu_list = ','.join(str(x) for x in opt['gpu_ids'])
os.environ['CUDA_VISIBLE_DEVICES'] = gpu_list
print('export CUDA_VISIBLE_DEVICES=' + gpu_list)
return opt
'''
# --------------------------------------------
# convert the opt into json file
# --------------------------------------------
'''
def save(opt: Dict[str, Any]):
opt_path = opt['opt_path']
opt_path_copy = opt['path']['options']
_, filename_ext = os.path.split(opt_path)
filename, ext = os.path.splitext(filename_ext)
dump_path = os.path.join(opt_path_copy, filename + get_timestamp() + ext)
with open(dump_path, 'w') as dump_file:
json.dump(opt, dump_file, indent=2)
'''
# --------------------------------------------
# dict to string for logger
# --------------------------------------------
'''
def dict2str(opt: Dict[str, Any], indent_l: int = 1):
msg: str = ''
for k, v in opt.items():
if isinstance(v, dict):
msg += ' ' * (indent_l * 2) + k + ':[\n'
msg += dict2str(v, indent_l + 1)
msg += ' ' * (indent_l * 2) + ']\n'
else:
msg += ' ' * (indent_l * 2) + k + ': ' + str(v) + '\n'
return msg