-
Notifications
You must be signed in to change notification settings - Fork 10
/
visualization.py
157 lines (130 loc) · 5.25 KB
/
visualization.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
import numpy as np
from torchvision.transforms import ToPILImage
import matplotlib.pyplot as plt
from torchvision.utils import make_grid
import torch
def generate(generator, rep, return_latent = False):
if generator.type == "stylegan":
if generator.Z:
imgs, _ = generator([rep])
latent = rep
if generator.W:
if return_latent:
latent = generator.style(rep)
else:
latent = rep
imgs, _ = generator(styles = [latent], input_is_latent=True)
if generator.S:
raise NotImplementedError
elif generator.type == "sngan":
imgs = generator(rep)
latent = rep
else:
raise NotImplementedError
if return_latent:
return imgs, latent
else:
return imgs
def visualize_GAN(G, navigator, path, used_dim = 64, total_dim = 64):
plt.rcParams['figure.dpi'] = 1000
navigator.eval()
G.eval()
with torch.no_grad():
noise = torch.randn(1,G.generator_latent_dim).cuda()
imgs, style = generate(G, noise, return_latent = True)
samples = []
# only visualize the used directions
for k in range(used_dim):
interpolation = torch.arange(-16, 16, 3)
for val in interpolation:
z = torch.zeros(total_dim).cuda()
z[k] = val
shift = navigator(z)
sample = generate(G, style + shift)
sample = ((sample+1) / 2).clamp(0,1).cpu()
samples.append(sample)
samples = torch.cat(samples, dim = 0)
output = make_grid(samples, nrow= 11, padding = 0)
for i, y in enumerate(range(10, 10 + used_dim * G.size, G.size)):
plt.text(1, y, str(i), color="red", fontsize=1)
out = output.detach().permute(1,2,0).numpy()
plt.axis('off')
plt.imshow(out)
plt.savefig(path)
del output
# def visualize_Anime(G, mlp, path, line_number = 64, VAE_dim = 64):
# plt.rcParams['figure.dpi'] = 2000
# mlp.eval()
# G.eval()
# G.size = 64
# with torch.no_grad():
# style = torch.randn(1, 128).cuda()
# # first do W
# samples = []
# for k in range(line_number):
# interpolation = torch.arange(-16, 16, 3)
# for val in interpolation:
# z = torch.zeros(VAE_dim).cuda()
# z[k] = val
# shift = mlp(z)
# sample = G(style + shift)
# sample = ((sample+1) / 2).clamp(0,1).cpu()
# samples.append(sample)
# samples = torch.cat(samples, dim = 0)
# output = make_grid(samples, nrow= 11, padding = 0)
# for i, y in enumerate(range(10, 10 + line_number * G.size, G.size)):
# plt.text(1, y, str(i), color="red", fontsize=1)
# out = output.detach().permute(1,2,0).numpy()
# plt.imshow(out)
# plt.savefig(path)
# del output
# def visualize_onehot_glow(G, mlp, path, visual_noise, line_number = 64, VAE_dim = 64):
# plt.rcParams['figure.dpi'] = 1000
# mlp.eval()
# G.eval()
# with torch.no_grad():
# visual_noise[-1] = torch.randn(1, 96, 4, 4).cuda() * 0.7
# samples = []
# for k in range(line_number):
# visual_noise_temp = visual_noise.copy()
# interpolation = torch.arange(-16, 16, 3)
# for val in interpolation:
# z = torch.zeros(VAE_dim).cuda()
# z[k] = val
# shift = mlp(z).view(1, 96, 4, 4)
# visual_noise_temp[-1] = visual_noise[-1] + shift
# sample = G.reverse(visual_noise_temp)
# sample = ((sample - torch.min(sample))/ (torch.max(sample) - torch.min(sample))).cpu()
# samples.append(sample)
# samples = torch.cat(samples, dim = 0)
# output = make_grid(samples, nrow= 11, padding = 0)
# for i, y in enumerate(range(10, 10 + line_number * 64, 64)):
# plt.text(1, y, str(i), color="red", fontsize=1)
# out = output.detach().permute(1,2,0).numpy()
# plt.imshow(out)
# plt.savefig(path)
# del output
# def visualize_onehot_VAE(G, mlp, path, visual_noise, line_number = 64, VAE_dim = 64):
# plt.rcParams['figure.dpi'] = 1000
# mlp.eval()
# G.eval()
# with torch.no_grad():
# samples = []
# for k in range(line_number):
# visual_noise_temp = visual_noise.unsqueeze(0)
# interpolation = torch.arange(-16, 16, 3)
# for val in interpolation:
# z = torch.zeros(VAE_dim).cuda()
# z[k] = val
# shift = mlp(z)
# sample = G(visual_noise_temp + shift)
# sample = torch.sigmoid(sample).cpu()
# samples.append(sample)
# samples = torch.cat(samples, dim = 0)
# output = make_grid(samples, nrow= 11, padding = 0)
# for i, y in enumerate(range(10, 10 + line_number * 64, 64)):
# plt.text(1, y, str(i), color="red", fontsize=1)
# out = output.detach().permute(1,2,0).numpy()
# plt.imshow(out)
# plt.savefig(path)
# del output