-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_StyleALAE.py
199 lines (174 loc) · 8.93 KB
/
test_StyleALAE.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
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
import torch
import os
import math
from utils.progress_bar import progress_bar
from torchvision import transforms
import cv2
from dnn.models.ALAE import StyleALAE
import glob
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
config = {
"z_dim": 256,
"w_dim": 256,
"image_dim": 64,
"mapping_layers": 8,
"resolutions": [4, 8, 16, 32, 64, 64],
"channels": [256, 256, 128, 128, 64, 32, 16],
"learning_rates": [0.001, 0.001, 0.001, 0.001, 0.001, 0.001],
"phase_lengths": [600_000, 600_000, 600_000, 600_000, 600_000, 1000_000],
"batch_sizes": [128, 128, 128, 64, 32, 32],
"n_critic": 1,
"dump_imgs_freq": 5000,
"checkpoint_freq": 10000
}
def inverse_normalize(tensor, mean, std):
for t, m, s in zip(tensor, mean, std):
t.mul_(s).add_(m)
return tensor
def single_tensor_to_img(tensor, w_size, h_size):
cvimg = tensor.cpu().numpy()
cvimg = cvimg.transpose(1, 2, 0)
cvimg = cv2.cvtColor(cvimg, cv2.COLOR_RGB2BGR)
cvimg = cv2.resize(cvimg, dsize=(w_size, h_size))
return cvimg
def generate_img(model, config, saved_path, show, generation_size, alpha=0.4, batch_size=32):
if not os.path.exists(saved_path):
os.makedirs(saved_path)
cnt = 0
for i in range(math.ceil(generation_size/batch_size)):
if i == math.ceil(generation_size/batch_size) - 1:
batch_size = generation_size - i*batch_size
test_samples_z = torch.randn(batch_size, config['z_dim'], dtype=torch.float32).to(device)
with torch.no_grad():
generated_images = model.generate(test_samples_z, final_resolution_idx=model.res_idx, alpha=alpha)
generated_images = generated_images * 0.5 + 0.5
# generated_images = inverse_normalize(tensor=generated_images, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
for tensor in generated_images:
cvimg = single_tensor_to_img(tensor, 128, 128)
image_name = str(cnt) + ".jpg"
cnt += 1
file_name = os.path.join(saved_path, image_name)
if show == True:
cv2.imshow("restored_image", cvimg)
cv2.waitKey(0)
cvimg = cvimg * 255
cv2.imwrite(file_name, cvimg)
progress_bar(i, math.ceil(generation_size/batch_size))
cv2.destroyAllWindows()
print("Finished")
def generate_style_mixing_img(model, config, alpha=0.4, copystyleto=[4,8]):
z_main = torch.randn(1, config['z_dim'], dtype=torch.float32).to(device)
z_copy = torch.randn(1, config['z_dim'], dtype=torch.float32).to(device)
if type(copystyleto) != type([[]]):
with torch.no_grad():
main_images = model.generate(z_main, final_resolution_idx=model.res_idx, alpha=alpha)
main_images = main_images * 0.5 + 0.5
copy_images = model.generate(z_copy, final_resolution_idx=model.res_idx, alpha=alpha)
copy_images = copy_images * 0.5 + 0.5
combined_images = model.generate_style_mixing(z_main, z_copy, copystyleto=copystyleto, final_resolution_idx=model.res_idx, alpha=alpha)
combined_images = combined_images * 0.5 + 0.5
main_image = single_tensor_to_img(main_images[0], 128, 128)
copy_image = single_tensor_to_img(copy_images[0], 128, 128)
combined_image = single_tensor_to_img(combined_images[0], 128, 128)
cv2.imshow("Main Image", main_image)
cv2.imshow("Copy Image", copy_image)
cv2.imshow("Combined Image", combined_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
im_v = []
for resolution in copystyleto:
with torch.no_grad():
main_images = model.generate(z_main, final_resolution_idx=model.res_idx, alpha=alpha)
main_images = main_images * 0.5 + 0.5
copy_images = model.generate(z_copy, final_resolution_idx=model.res_idx, alpha=alpha)
copy_images = copy_images * 0.5 + 0.5
combined_images = model.generate_style_mixing(z_main, z_copy, copystyleto=resolution,
final_resolution_idx=model.res_idx, alpha=alpha)
combined_images = combined_images * 0.5 + 0.5
main_image = single_tensor_to_img(main_images[0], 128, 128)
copy_image = single_tensor_to_img(copy_images[0], 128, 128)
combined_image = single_tensor_to_img(combined_images[0], 128, 128)
# cv2.imshow("Main Image", main_image)
# cv2.imshow("Copy Image", copy_image)
# cv2.imshow("Combined Image@resolution "+str(resolution), combined_image)
im_h = cv2.hconcat([main_image, copy_image, combined_image])
pos = (int(im_h.shape[0]*0.1), int(im_h.shape[1]*0.1))
cv2.putText(im_h,"Res:" + str(resolution), pos, cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0),2)
im_v.append(im_h)
im_v = cv2.vconcat(im_v)
cv2.imshow("Main , Copy, Combined ", im_v)
cv2.waitKey(0)
cv2.destroyAllWindows()
print("Finished")
def generate_img_with_truncation(model, config, alpha=0.4):
batch_size = 1
test_samples_z = torch.randn(batch_size, config['z_dim'], dtype=torch.float32).to(device)
style = 0
while style < 1:
with torch.no_grad():
generated_images = model.generate_with_truncation(test_samples_z, style=style, final_resolution_idx=model.res_idx, alpha=alpha)
generated_images = generated_images * 0.5 + 0.5
# generated_images = inverse_normalize(tensor=generated_images, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
for cvimg in generated_images.cpu().numpy():
cvimg = cvimg.transpose(1,2,0)
cvimg = cv2.cvtColor(cvimg, cv2.COLOR_RGB2BGR)
cvimg = cv2.resize( cvimg, dsize=(256,256) )
cv2.imshow("Img", cvimg)
cv2.waitKey(30)
print(style)
style = style + 0.01
cvimg = cvimg * 255
cv2.imwrite("./Truncation/" + str(int(style*100)) + ".jpg", cvimg)
cv2.destroyAllWindows()
print("Finished")
def reconstruct_img(model, config, path, saved_path, show, alpha=0.4):
test_samples_z = torch.randn(batch_size, config['z_dim'], dtype=torch.float32).to(device)
image_paths = []
rename = []
image_extensions = ["png", "jpg"]
for ext in image_extensions:
print("Looking for images in", os.path.join(path, "*.{}".format(ext)))
for impath in glob.glob(os.path.join(path, "*.{}".format(ext))):
image_paths.append(impath)
rename.append(impath[-9:-4])
with torch.no_grad():
for i in range(len(image_paths)):
tran = transforms.ToTensor()
img = cv2.imread(image_paths[i])
cv2.imshow("orig", img)
img = cv2.resize(img, dsize=(64,64))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = img/127.5 - 1
img = img.transpose(2, 0, 1)
img_tensor = torch.tensor(img, dtype=torch.float32).to(device).unsqueeze(0)
# img_tensor = tran(img).to(device).unsqueeze(0)
restored_images = model.decode(model.encode(img_tensor, final_resolution_idx=model.res_idx, alpha=alpha),
final_resolution_idx=model.res_idx, alpha=alpha)
restored_images = restored_images * 0.5 + 0.5
restored_image = restored_images[0].cpu().numpy().transpose(1,2,0)
restored_image = cv2.cvtColor(restored_image, cv2.COLOR_RGB2BGR)
restored_image = cv2.resize( restored_image, dsize=(128,128) )
image_name = rename[i] + ".jpg"
if not os.path.exists(saved_path):
os.makedirs(saved_path)
file_name = os.path.join(saved_path, image_name)
if show == True:
cv2.imshow("restored_image", restored_image)
cv2.waitKey(0)
restored_image = restored_image * 255
cv2.imwrite(file_name, restored_image)
if show==True:
cv2.destroyAllWindows()
print("Finished Reconstruction")
if __name__ == '__main__':
path = "./data/FFHQ-thumbnails/thumbnails128x128"
reconstruction_saved_path = "./data/FFHQ-thumbnails/reconstructed_thumbnails128x128"
generation_saved_path = "./data/FFHQ-thumbnails/generated_thumbnails128x128"
model = StyleALAE(model_config=config, device=device)
model.load_train_state('./archived/FFHQ/StyleALAE-z-256_w-256_prog-(4,256)-(8,256)-(16,128)-(32,128)-(64,64)-(64,32)/checkpoints/ckpt_gs-120000_res-5=64x64_alpha-0.40.pt')
batch_size = 32
# generate_img(model, config, generation_saved_path, True, 3, alpha=0.4, batch_size=32)
# reconstruct_img(model, config, path, reconstruction_saved_path, True, alpha=0.4)
# generate_style_mixing_img(model, config, alpha=0.4, copystyleto=[[4,8],[8,16],[16,32],[32,64]])
generate_img_with_truncation(model, config, alpha=0.4)