-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
80 lines (69 loc) · 2.34 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
80
import json
import numpy as np
import time
from vae import VDVAE
from vqvae import VQVAE
import os
from gan_helpers import Generator
def logger(log_prefix):
'Prints the arguments out to stdout, .txt, and .jsonl files'
jsonl_path = f'{log_prefix}.jsonl'
txt_path = f'{log_prefix}.txt'
def log(*args, pprint=False, **kwargs):
t = time.ctime()
argdict = {'time': t}
if len(args) > 0:
argdict['message'] = ' '.join([str(x) for x in args])
argdict.update(kwargs)
txt_str = []
args_iter = sorted(argdict) if pprint else argdict
for k in args_iter:
val = argdict[k]
if isinstance(val, np.ndarray):
val = val.tolist()
elif isinstance(val, np.integer):
val = int(val)
elif isinstance(val, np.floating):
val = float(val)
argdict[k] = val
if isinstance(val, float):
val = f'{val:.5f}'
txt_str.append(f'{k}: {val}')
txt_str = ', '.join(txt_str)
if pprint:
json_str = json.dumps(argdict, sort_keys=True)
txt_str = json.dumps(argdict, sort_keys=True, indent=4)
else:
json_str = json.dumps(argdict)
print(txt_str, flush=True)
with open(txt_path, "a+") as f:
print(txt_str, file=f, flush=True)
with open(jsonl_path, "a+") as f:
print(json_str, file=f, flush=True)
return log
def tile_images(images, d1=4, d2=4, border=1):
id1, id2, c = images[0].shape
out = np.ones([d1 * id1 + border * (d1 + 1),
d2 * id2 + border * (d2 + 1),
c], dtype=np.uint8)
out *= 255
if len(images) != d1 * d2:
raise ValueError('Wrong num of images')
for imgnum, im in enumerate(images):
num_d1 = imgnum // d2
num_d2 = imgnum % d2
start_d1 = num_d1 * id1 + border * (num_d1 + 1)
start_d2 = num_d2 * id2 + border * (num_d2 + 1)
out[start_d1:start_d1 + id1, start_d2:start_d2 + id2, :] = im
return out
def model_fn(H):
if H.gan:
return Generator(H)
elif H.model == 'vqvae':
return VQVAE(H)
elif H.model == 'vdvae':
return VDVAE(H)
else:
raise NotImplementedError
def mkdir_p(path):
os.makedirs(path, exist_ok=True)