-
Notifications
You must be signed in to change notification settings - Fork 80
/
infer_celeba.py
123 lines (111 loc) · 3.9 KB
/
infer_celeba.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
"""Train script.
Usage:
infer_celeba.py <hparams> <dataset_root> <z_dir>
"""
import os
import cv2
import random
import torch
import vision
import numpy as np
from docopt import docopt
from torchvision import transforms
from glow.builder import build
from glow.config import JsonConfig
def select_index(name, l, r, description=None):
index = None
while index is None:
print("Select {} with index [{}, {}),"
"or {} for random selection".format(name, l, r, l - 1))
if description is not None:
for i, d in enumerate(description):
print("{}: {}".format(i, d))
try:
line = int(input().strip())
if l - 1 <= line < r:
index = line
if index == l - 1:
index = random.randint(l, r - 1)
except Exception:
pass
return index
def run_z(graph, z):
graph.eval()
x = graph(z=torch.tensor([z]).cuda(), eps_std=0.3, reverse=True)
img = x[0].permute(1, 2, 0).detach().cpu().numpy()
img = img[:, :, ::-1]
img = cv2.resize(img, (256, 256))
return img
def save_images(images, names):
if not os.path.exists("pictures/infer/"):
os.makedirs("pictures/infer/")
for img, name in zip(images, names):
img = (np.clip(img, 0, 1) * 255).astype(np.uint8)
cv2.imwrite("pictures/infer/{}.png".format(name), img)
cv2.imshow("img", img)
cv2.waitKey()
if __name__ == "__main__":
args = docopt(__doc__)
hparams = args["<hparams>"]
dataset_root = args["<dataset_root>"]
z_dir = args["<z_dir>"]
assert os.path.exists(dataset_root), (
"Failed to find root dir `{}` of dataset.".format(dataset_root))
assert os.path.exists(hparams), (
"Failed to find hparams josn `{}`".format(hparams))
if not os.path.exists(z_dir):
print("Generate Z to {}".format(z_dir))
os.makedirs(z_dir)
generate_z = True
else:
print("Load Z from {}".format(z_dir))
generate_z = False
hparams = JsonConfig("hparams/celeba.json")
dataset = vision.Datasets["celeba"]
# set transform of dataset
transform = transforms.Compose([
transforms.CenterCrop(hparams.Data.center_crop),
transforms.Resize(hparams.Data.resize),
transforms.ToTensor()])
# build
graph = build(hparams, False)["graph"]
dataset = dataset(dataset_root, transform=transform)
# get Z
if not generate_z:
# try to load
try:
delta_Z = []
for i in range(hparams.Glow.y_classes):
z = np.load(os.path.join(z_dir, "detla_z_{}.npy".format(i)))
delta_Z.append(z)
except FileNotFoundError:
# need to generate
generate_z = True
print("Failed to load {} Z".format(hparams.Glow.y_classes))
quit()
if generate_z:
delta_Z = graph.generate_attr_deltaz(dataset)
for i, z in enumerate(delta_Z):
np.save(os.path.join(z_dir, "detla_z_{}.npy".format(i)), z)
print("Finish generating")
# interact with user
base_index = select_index("base image", 0, len(dataset))
attr_index = select_index("attritube", 0, len(delta_Z), dataset.attrs)
attr_name = dataset.attrs[attr_index]
z_delta = delta_Z[attr_index]
graph.eval()
z_base = graph.generate_z(dataset[base_index]["x"])
# begin to generate new image
images = []
names = []
images.append(run_z(graph, z_base))
names.append("reconstruct_origin")
interplate_n = 5
for i in range(0, interplate_n+1):
d = z_delta * float(i) / float(interplate_n)
images.append(run_z(graph, z_base + d))
names.append("attr_{}_{}".format(attr_name, interplate_n + i))
if i > 0:
images.append(run_z(graph, z_base - d))
names.append("attr_{}_{}".format(attr_name, interplate_n - i))
save_images(images, names)