Spaces:
Build error
Build error
File size: 5,656 Bytes
a446b0b |
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
import json
import copy
import torch
import numpy as np
import contextlib
from distutils.dir_util import mkpath
from tqdm import tqdm
def make_new_tensor_from_list(items, device_num, dtype=torch.float32):
if device_num is not None:
device = torch.device("cuda:{}".format(device_num))
else:
device = torch.device("cpu")
return torch.tensor(items, dtype=dtype, device=device)
# is_dir look ast at whether the name we make
# should be a directory or a filename
def make_name(opt, prefix="", eval_=False, is_dir=True, set_epoch=None,
do_epoch=True):
string = prefix
string += "{}-{}".format(opt.dataset, opt.exp)
string += "/"
string += "{}-{}-{}".format(opt.trainer, opt.cycle, opt.iters)
string += "/"
string += opt.model
if opt.mle:
string += "-{}".format(opt.mle)
string += "/"
string += make_name_string(opt.data) + "/"
string += make_name_string(opt.net) + "/"
string += make_name_string(opt.train.static) + "/"
if eval_:
string += make_name_string(opt.eval) + "/"
# mkpath caches whether a directory has been created
# In IPython, this can be a problem if the kernel is
# not reset after a dir is deleted. Trying to recreate
# that dir will be a problem because mkpath will think
# the directory already exists
if not is_dir:
mkpath(string)
string += make_name_string(
opt.train.dynamic, True, do_epoch, set_epoch)
if is_dir:
mkpath(string)
return string
def make_name_string(dict_, final=False, do_epoch=False, set_epoch=None):
if final:
if not do_epoch:
string = "{}_{}_{}".format(
dict_.lr, dict_.optim, dict_.bs)
elif set_epoch is not None:
string = "{}_{}_{}_{}".format(
dict_.lr, dict_.optim, dict_.bs, set_epoch)
else:
string = "{}_{}_{}_{}".format(
dict_.lr, dict_.optim, dict_.bs, dict_.epoch)
return string
string = ""
for k, v in dict_.items():
if type(v) == DD:
continue
if isinstance(v, list):
val = "#".join(is_bool(str(vv)) for vv in v)
else:
val = is_bool(v)
if string:
string += "-"
string += "{}_{}".format(k, val)
return string
def is_bool(v):
if str(v) == "False":
return "F"
elif str(v) == "True":
return "T"
return v
def generate_config_files(type_, key, name="base", eval_mode=False):
with open("config/default.json".format(type_), "r") as f:
base_config = json.load(f)
with open("config/{}/default.json".format(type_), "r") as f:
base_config_2 = json.load(f)
if eval_mode:
with open("config/{}/eval_changes.json".format(type_), "r") as f:
changes_by_machine = json.load(f)
else:
with open("config/{}/changes.json".format(type_), "r") as f:
changes_by_machine = json.load(f)
base_config.update(base_config_2)
if name in changes_by_machine:
changes = changes_by_machine[name]
else:
changes = changes_by_machine["base"]
# for param in changes[key]:
# base_config[param] = changes[key][param]
replace_params(base_config, changes[key])
mkpath("config/{}".format(type_))
with open("config/{}/config_{}.json".format(type_, key), "w") as f:
json.dump(base_config, f, indent=4)
def replace_params(base_config, changes):
for param, value in changes.items():
if isinstance(value, dict) and param in base_config:
replace_params(base_config[param], changes[param])
else:
base_config[param] = value
def initialize_progress_bar(data_loader_list):
num_examples = sum([len(tensor) for tensor in
data_loader_list.values()])
return set_progress_bar(num_examples)
def set_progress_bar(num_examples):
bar = tqdm(total=num_examples)
bar.update(0)
return bar
def merge_list_of_dicts(L):
result = {}
for d in L:
result.update(d)
return result
def return_iterator_by_type(data_type):
if isinstance(data_type, dict):
iterator = data_type.items()
else:
iterator = enumerate(data_type)
return iterator
@contextlib.contextmanager
def temp_seed(seed):
state = np.random.get_state()
np.random.seed(seed)
try:
yield
finally:
np.random.set_state(state)
def flatten(outer):
return [el for inner in outer for el in inner]
def zipped_flatten(outer):
return [(key, fill, el) for key, fill, inner in outer for el in inner]
def remove_none(l):
return [e for e in l if e is not None]
# Taken from Jobman 0.1
class DD(dict):
def __getattr__(self, attr):
if attr == '__getstate__':
return super(DD, self).__getstate__
elif attr == '__setstate__':
return super(DD, self).__setstate__
elif attr == '__slots__':
return super(DD, self).__slots__
return self[attr]
def __setattr__(self, attr, value):
# Safety check to ensure consistent behavior with __getattr__.
assert attr not in ('__getstate__', '__setstate__', '__slots__')
# if attr.startswith('__'):
# return super(DD, self).__setattr__(attr, value)
self[attr] = value
def __str__(self):
return 'DD%s' % dict(self)
def __repr__(self):
return str(self)
def __deepcopy__(self, memo):
z = DD()
for k, kv in self.items():
z[k] = copy.deepcopy(kv, memo)
return z
|