-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
73 lines (56 loc) · 1.99 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
import os
import logging
import numpy as np
from PIL import Image
import config as c
def config_log():
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s \n %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename=os.path.join(c.SAVE_PATH, "train.log"),
filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
def normalize_frames(frames):
"""
Convert frames from int8 [0, 255] to float32 [-1, 1].
@param frames: A numpy array. The frames to be converted.
@return: The normalized frames.
"""
new_frames = frames.astype(np.float32)
new_frames = new_frames * 3 / 255
# new_frames /= (255 / 2)
# new_frames -= 1
# if frames.min() == np.inf or frames.max() == np.inf or new_frames.min() == np.inf or new_frames.max() == np.inf:
# print(frames.min(), frames.max(), new_frames.min(), new_frames.max())
return new_frames
def denormalize_frames(frames):
"""
Performs the inverse operation of normalize_frames.
@param frames: A numpy array. The frames to be converted.
@return: The denormalized frames.
"""
new_frames = frames / 3 * 255
# new_frames = frames + 1
#new_frames *= (255 / 2)
# noinspection PyUnresolvedReferences
new_frames = new_frames.astype(np.uint8)
return new_frames
def save_png(data, path):
data[data < 0] = 0
if c.NORMALIZE:
data = denormalize_frames(data)
else:
data = data.astype(np.uint8)
if not os.path.exists(path):
os.makedirs(path)
shape = data.shape
data = data.reshape(shape[0], shape[-3], shape[-2])
i = 1
for img in data[:]:
img = Image.fromarray(img)
img.save(os.path.join(path, str(i) + ".png"))
i += 1