-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpolation.py
312 lines (251 loc) · 8.58 KB
/
interpolation.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import os
import torch
from torchvision import utils
from model2 import Generator
import glob
from PIL import Image
import sys
from tqdm import tqdm
import numpy as np
if not os.path.isdir('/sample/'):
os.mkdir('sample')
if not os.path.isdir('/sg/'):
os.mkdir('sg')
# need args:
# size
# sample
# npics
# checkpoint
# device
def gen(npics, G, device, seeds = [1], nsample = 1, styledim = 512, truncation = 1.0, trunc_mean = 4096):
with torch.no_grad():
G.eval()
for i in tqdm(range(npics)):
torch.manual_seed(seeds[i])
sample_z = torch.randn(nsample, styledim, device = device)
sample, _ = G(
[sample_z], truncation = truncation, truncation_latent = trunc_mean
)
utils.save_image(
sample,
f"sample/{str(i).zfill(6)}.png",
nrow = 1,
normalize = True,
range = (-1, 1),
)
device = "cuda"
G = Generator(
size = 128, style_dim = 512, n_mlp = 8
).to(device)
checkpoint = torch.load("/content/drive/MyDrive/style-based-gan-pytorch/checkpoints_corgi_reg_aug/040000.pt")
G.load_state_dict(checkpoint["g_ema"], strict = False)
n = 25000
gen(npics = n, G = G, device = "cuda", seeds = range(n))
# linear interpolation
def linterp(z, steps):
out = []
for i in range(len(z)-1):
for index in range(steps):
t = index/float(steps)
out.append(z[i+1] * t + z[i] * (1-t))
return out
# linear interpolation in z_space
def gen_linterp_z(G, device, nsteps = 5, seeds = [0, 2], styledim = 512, truncation = 1.0, trunc_mean = 4096):
with torch.no_grad():
G.eval()
torch.manual_seed(seeds[0])
start = torch.randn(1, styledim, device = device)
torch.manual_seed(seeds[1])
end = torch.randn(1, styledim, device = device)
zs = linterp([start, end], steps = nsteps)
for i in tqdm(range(nsteps)):
sample, _ = G(
[zs[i]], truncation = truncation, truncation_latent = trunc_mean
)
utils.save_image(
sample,
f"sample/{str(i).zfill(4)}.png",
nrow = nsteps,
normalize = True,
range = (-1, 1),
)
gen_linterp_z(G = G, device = "cuda", nsteps = 25)
# filepaths
fp_in = "/sg/sample/*.png"
fp_out = "/sg/sample/linterp.gif"
# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
img.save(fp=fp_out, format='GIF', append_images=imgs,
save_all=True, duration=200, loop=0)
z = start = torch.randn(1, 512, device = device)
w = G.style(z)
z[0,0], w[0,0]
smp, _ = G(w, input_is_latent = True)
utils.save_image(
smp,
f"sample/test.png",
nrow = 1,
normalize = True,
range = (-1, 1),
)
# linear interpolation in w_space
# G(ws, input_is_latent = True)
def gen_linterp_w(G, device, nsteps = 5, seeds = [0, 2], styledim = 512, truncation = 1.0, trunc_mean = 4096):
with torch.no_grad():
G.eval()
torch.manual_seed(seeds[0])
start = torch.randn(1, styledim, device = device)
torch.manual_seed(seeds[1])
end = torch.randn(1, styledim, device = device)
# pass through style network
start_w = G.style(start)
end_w = G.style(end)
ws = linterp([start_w, end_w], steps = nsteps)
for i in tqdm(range(nsteps)):
sample, _ = G(
[ws[i]],
truncation = truncation,
truncation_latent = trunc_mean,
input_is_latent = True
)
utils.save_image(
sample,
f"sample_w/{str(i).zfill(4)}.png",
nrow = nsteps,
normalize = True,
range = (-1, 1),
)
if not os.path.isdir("/sg/sample_w/"):
os.mkdir("/sg/sample_w/")
gen_linterp_w(G = G, device = "cuda", nsteps = 25)
# generate gif
fp_in = "/content/sg/sample_w/*.png"
fp_out = "/content/sg/sample_w/linterp_w.gif"
# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
img.save(fp=fp_out, format='GIF', append_images=imgs,
save_all=True, duration=200, loop=0)
torch.manual_seed(0)
start = torch.randn(1, 2, device = "cpu")
torch.manual_seed(2)
end = torch.randn(1, 2, device = "cpu")
# spherical interpolation
def spherical_interp(steps, start, end):
out = []
for i in range(steps):
t = i / (steps - 1)
if t <= 0:
out.append(start)
elif t >= 1:
out.append(end)
elif torch.allclose(start, end):
out.append(start)
omega = torch.arccos(torch.tensordot(start/torch.linalg.norm(start), end/torch.linalg.norm(end)))
sin_omega = torch.sin(omega)
out.append(np.sin((1.0 - t) * omega) / sin_omega * start + torch.sin(t * omega) / sin_omega * end)
return out
# spherical interpolation in z_space
def gen_slerp_z(G, device, nsteps = 5, seeds = [0, 2], styledim = 512, truncation = 1.0, trunc_mean = 4096):
with torch.no_grad():
G.eval()
torch.manual_seed(seeds[0])
start = torch.randn(1, styledim, device = device)
torch.manual_seed(seeds[1])
end = torch.randn(1, styledim, device = device)
zs = spherical_interp(steps = nsteps, start = start.cpu(), end = end.cpu())
zs = torch.stack(zs)
zs = zs.to(torch.device('cuda'))
for i in tqdm(range(nsteps)):
sample, _ = G(
[zs[i]], truncation = truncation, truncation_latent = trunc_mean
)
utils.save_image(
sample,
f"sample_spherical/{str(i).zfill(4)}.png",
nrow = nsteps,
normalize = True,
range = (-1, 1),
)
# spherical interpolation in w_space
# G(ws, input_is_latent = True)
def gen_slerp_w(G, device, nsteps = 5, seeds = [0, 2], styledim = 512, truncation = 1.0, trunc_mean = 4096):
with torch.no_grad():
G.eval()
torch.manual_seed(seeds[0])
start = torch.randn(1, styledim, device = device)
torch.manual_seed(seeds[1])
end = torch.randn(1, styledim, device = device)
# pass through style network
start_w = G.style(start)
end_w = G.style(end)
ws = spherical_interp(steps = nsteps, start = start_w.cpu(), end = end_w.cpu())
ws = torch.stack(ws)
ws = ws.to(torch.device('cuda'))
for i in tqdm(range(nsteps)):
sample, _ = G(
[ws[i]],
truncation = truncation,
truncation_latent = trunc_mean,
input_is_latent = True
)
utils.save_image(
sample,
f"sample_spherical_w/{str(i).zfill(4)}.png",
nrow = nsteps,
normalize = True,
range = (-1, 1),
)
if not os.path.exists("/sg/sample_spherical/"):
os.mkdir("/sg/sample_spherical/")
if not os.path.exists("/sg/sample_spherical_w/"):
os.mkdir("/sg/sample_spherical_w/")
gen_slerp_z(G = G, device = "cuda", nsteps = 25)
gen_slerp_w(G = G, device = "cuda", nsteps = 25)
# generate gif
fp_in = "/sg/sample_spherical/*.png"
fp_out = "/sg/sample_spherical/slerp.gif"
# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
img.save(fp=fp_out, format='GIF', append_images=imgs,
save_all=True, duration=200, loop=0)
# generate gif (should probably wrap this in function at this point)
fp_in = "/sg/sample_spherical_w/*.png"
fp_out = "/sg/sample_spherical_w/slerp_w.gif"
# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
img.save(fp=fp_out, format='GIF', append_images=imgs,
save_all=True, duration=200, loop=0)
if not os.path.exists("/sg/zippedfiles"):
os.mkdir("/sg/zippedfiles")
# function to combine multiple images
def imgcombine(path):
fp_in = os.path.join(path, "*.png")
fp_out = os.path.join(path, "combined.png")
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
widths, heights = zip(*(i.size for i in imgs))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in imgs:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
new_im.save(fp_out)
imgcombine("/sg/sample_spherical_w/")
imgcombine("/sg/sample_spherical/")
imgcombine("/sg/sample/")
imgcombine("/sg/sample_w/")
# function to combine the combined images
def imgcombine(path):
fp_in = os.path.join(path, "*.png")
fp_out = os.path.join(path, "combined.png")
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
widths, heights = zip(*(i.size for i in imgs))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in imgs:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
new_im.save(fp_out)