-
Notifications
You must be signed in to change notification settings - Fork 0
/
invert.py
405 lines (326 loc) · 13.2 KB
/
invert.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import os
import math
import numpy as np
import argparse
import torch
from torchvision import transforms, utils
from PIL import Image
from tqdm import tqdm
from exaggeration_model import StyleCariGAN
class perceptual_module(torch.nn.Module):
def __init__(self):
import torchvision
super().__init__()
perceptual = torchvision.models.vgg16(pretrained=True)
self.module1_1 = torch.nn.Sequential(
*list(perceptual.children())[0][:1])
self.module1_2 = torch.nn.Sequential(
*list(perceptual.children())[0][1:3])
self.module3_2 = torch.nn.Sequential(
*list(perceptual.children())[0][3:13])
self.module4_2 = torch.nn.Sequential(
*list(perceptual.children())[0][13:20])
def forward(self, x):
outputs = {}
out = self.module1_1(x)
outputs['1_1'] = out
out = self.module1_2(out)
outputs['1_2'] = out
out = self.module3_2(out)
outputs['3_2'] = out
out = self.module4_2(out)
outputs['4_2'] = out
return outputs
# re-normalize image into vgg image normalization scheme
class TO_VGG(object):
def __init__(self, device="cpu"):
self.s_mean = torch.from_numpy(np.asarray([0.5, 0.5, 0.5])).view(
1, 3, 1, 1).type(torch.FloatTensor).to(device)
self.s_std = torch.from_numpy(np.asarray([0.5, 0.5, 0.5])).view(
1, 3, 1, 1).type(torch.FloatTensor).to(device)
self.t_mean = torch.from_numpy(np.asarray([0.485, 0.456, 0.406])).view(
1, 3, 1, 1).type(torch.FloatTensor).to(device)
self.t_std = torch.from_numpy(np.asarray([0.229, 0.224, 0.225])).view(
1, 3, 1, 1).type(torch.FloatTensor).to(device)
def __call__(self, t):
t = (t + 1) / 2
t = (t - self.t_mean) / self.t_std
# t = t * self.t_std + self.t_mean
return t
def make_noise(batch, latent_dim, n_noise, device):
if n_noise == 1:
return torch.randn(batch, latent_dim, device=device)
noises = torch.randn(n_noise, batch, latent_dim, device=device).unbind(0)
return noises
def mixing_noise(batch, latent_dim, device):
return make_noise(batch, latent_dim, 1, device)
def noise_regularize(noises):
loss = 0
for noise in noises:
size = noise.shape[2]
while True:
loss = (
loss +
(noise * torch.roll(noise, shifts=1, dims=3)).mean().pow(2) +
(noise * torch.roll(noise, shifts=1, dims=2)).mean().pow(2))
if size <= 8:
break
noise = noise.reshape([-1, 1, size // 2, 2, size // 2, 2])
noise = noise.mean([3, 5])
size //= 2
return loss
def noise_normalize_(noises):
for noise in noises:
mean = noise.mean()
std = noise.std()
noise.data.add_(-mean).div_(std)
def make_noise(batch, latent_dim, n_noise, device):
if n_noise == 1:
return torch.randn(batch, latent_dim, device=device)
noises = torch.randn(n_noise, batch, latent_dim, device=device).unbind(0)
return noises
def mixing_noise(batch, latent_dim, prob, device):
return make_noise(batch, latent_dim, 1, device)
def l2loss(input1, input2):
diff = input1 - input2
diff = diff.pow(2).mean().sqrt().squeeze()
return diff
def requires_grad(model, flag=True):
for p in model.parameters():
p.requires_grad = flag
def invert(g_ema, perceptual, real_img, device, args):
save = args.save
result = {}
to_vgg = TO_VGG()
requires_grad(perceptual, True)
requires_grad(g_ema, True)
log_size = int(math.log(256, 2))
num_layers = (log_size - 2) * 2 + 1
w = args.mean_w.clone().detach().to(device).unsqueeze(0) # 1 * 512
w.requires_grad = True
wplr = args.wlr
optimizer = torch.optim.Adam(
[w],
lr=args.wlr,
)
with torch.no_grad():
sample, _ = g_ema([w], input_is_latent=True, randomize_noise=True)
if save:
utils.save_image(
sample,
args.result_dir + f"/{args.image_name}_recon_w_initial.png",
nrow=int(sample.shape[0]**0.5),
normalize=True,
range=(-1, 1),
)
utils.save_image(
real_img,
args.result_dir + f"/{args.image_name}_input.png",
nrow=int(real_img.shape[0]**0.5),
normalize=True,
range=(-1, 1),
)
print('optimizing w')
# loop for w
pbar = range(args.w_iterations)
pbar = tqdm(pbar, initial=0, dynamic_ncols=True, smoothing=0.01)
for idx in pbar:
if idx + 1 % (args.w_iterations // 2) == 0:
for g in optimizer.param_groups:
g['lr'] = g['lr'] * args.lr_decay_rate
wplr = wplr * args.lr_decay_rate
real_img_vgg = to_vgg(real_img)
t = 1
w_tilde = w + torch.randn(w.shape, device=device) * t * t
fake_img, _ = g_ema([w_tilde],
input_is_latent=True,
randomize_noise=True)
fake_img_vgg = to_vgg(fake_img)
fake_feature = perceptual(fake_img_vgg)
real_feature = perceptual(real_img_vgg)
loss_pixel = l2loss(fake_img, real_img)
loss_feature = []
for (fake_feat, real_feat) in zip(fake_feature.values(),
real_feature.values()):
loss_feature.append(l2loss(fake_feat, real_feat))
loss_feature = torch.mean(torch.stack(loss_feature))
loss = args.lambda_l2 * loss_pixel +\
args.lambda_p * loss_feature
optimizer.zero_grad()
loss.backward()
optimizer.step()
pbar.set_description((
f"optimizing w: loss_pixel: {loss_pixel:.4f}; loss_feature: {loss_feature:.4f}"
))
if idx % (args.w_iterations // 3) == 0 and save:
with torch.no_grad():
sample, _ = g_ema([w],
input_is_latent=True,
randomize_noise=True)
utils.save_image(
sample,
args.result_dir + f"/{args.image_name}_recon_w_{idx}.png",
nrow=int(sample.shape[0]**0.5),
normalize=True,
range=(-1, 1),
)
result['w'] = w.squeeze().cpu()
if save:
with torch.no_grad():
sample, _ = g_ema([w], input_is_latent=True, randomize_noise=True)
utils.save_image(
sample,
args.result_dir +
f"/{args.image_name}_recon_w_final.png", # should be same as recon_w_final.png
nrow=int(sample.shape[0]**0.5),
normalize=True,
range=(-1, 1),
)
print('optimizing wp')
# starting point for w : mean w
wp = w.unsqueeze(1).repeat(1, args.num_layers,
1).detach().clone() # single image
wp.requires_grad = True
noises = []
for layer_idx in range(num_layers):
res = (layer_idx + 5) // 2
shape = [1, 1, 2**res, 2**res]
noises.append(torch.randn(*shape, device=device).normal_())
noises[layer_idx].requires_grad = True
optimizer = torch.optim.Adam(
[wp] + noises,
lr=wplr,
)
if save:
with torch.no_grad():
sample, _ = g_ema(wp,
noise=noises,
input_is_w_plus=True,
randomize_noise=False)
utils.save_image(
sample,
args.result_dir +
f"/{args.image_name}_recon_wp_initial.png", # should be same as recon_w_final.png
nrow=int(sample.shape[0]**0.5),
normalize=True,
range=(-1, 1),
)
# loop for wp
pbar = range(args.wp_iterations)
pbar = tqdm(pbar, initial=0, dynamic_ncols=True, smoothing=0.01)
for idx in pbar:
if idx + 1 % (args.wp_iterations // 6) == 0:
for g in optimizer.param_groups:
g['lr'] = g['lr'] * args.lr_decay_rate
real_img_vgg = to_vgg(real_img)
# loss
t = max(1 - 3 * idx / args.wp_iterations, 0)
wp_tilde = wp + torch.randn(wp.shape, device=device) * t * t
fake_img, _ = g_ema(wp_tilde,
noise=noises,
input_is_w_plus=True,
randomize_noise=False)
fake_img_vgg = to_vgg(fake_img)
fake_feature = perceptual(fake_img_vgg)
real_feature = perceptual(real_img_vgg)
loss_pixel = l2loss(fake_img, real_img)
loss_feature = []
for (fake_feat, real_feat) in zip(fake_feature.values(),
real_feature.values()):
loss_feature.append(l2loss(fake_feat, real_feat))
loss_feature = torch.mean(torch.stack(loss_feature))
loss_noise = noise_regularize(noises)
loss = args.lambda_l2 * loss_pixel +\
args.lambda_p * loss_feature +\
args.lambda_noise * loss_noise
optimizer.zero_grad()
loss.backward()
optimizer.step()
noise_normalize_(noises)
# update pbar
pbar.set_description((
f"optimizing wp: loss_pixel: {loss_pixel:.4f}; loss_feature: {loss_feature:.4f}"
))
# save progress
if idx % (args.wp_iterations // 3) == 0 and save:
with torch.no_grad():
sample, _ = g_ema(wp,
noise=noises,
input_is_w_plus=True,
randomize_noise=False)
utils.save_image(
sample,
args.result_dir + f"/{args.image_name}_recon_wp_{idx}.png",
nrow=int(sample.shape[0]**0.5),
normalize=True,
range=(-1, 1),
)
# end of optimization - save results
with torch.no_grad():
fake_img, _ = g_ema(wp,
noise=noises,
input_is_w_plus=True,
randomize_noise=False)
if save:
utils.save_image(
fake_img,
args.result_dir + f"/{args.image_name}_recon_final.png",
nrow=int(fake_img.shape[0]**0.5),
normalize=True,
range=(-1, 1),
)
result['wp'] = wp.squeeze().cpu()
result['noise'] = [n.cpu() for n in noises]
torch.save(result, args.result_dir + f'/{args.image_name}.pt')
def parse_args():
parser = argparse.ArgumentParser(description="StyleGAN2 encoder test")
parser.add_argument("--w_iterations", type=int, default=250)
parser.add_argument("--wp_iterations", type=int, default=2000)
parser.add_argument('--image',
type=str,
required=True,
help='path to the image to invert')
parser.add_argument("--size",
type=int,
default=256,
help="image sizes for the model")
parser.add_argument("--ckpt", type=str, required=True)
parser.add_argument("--lambda_l2", type=float, default=1)
parser.add_argument("--lambda_p", type=float, default=1)
parser.add_argument("--lambda_noise", type=float, default=1e5)
parser.add_argument("--wlr", type=float, default=4e-3)
parser.add_argument("--lr_decay_rate", type=float, default=0.2)
parser.add_argument("--result_dir", type=str, default='./invert_result')
args = parser.parse_args()
args.save = False # no need to save optimized image
return args
if __name__ == "__main__":
device = "cpu" #"cuda" if torch.cuda.is_available() else "cpu"
args = parse_args()
args.latent = 512
args.num_layers = 14
g_ema = StyleCariGAN(args.size, args.latent, 8,
channel_multiplier=2).to(device)
checkpoint = torch.load(args.ckpt)
g_ema.load_state_dict(checkpoint['g_ema'], strict=False)
g_ema = g_ema.photo_generator
g_ema.eval()
perceptual = perceptual_module().to(device)
perceptual.eval()
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True),
])
n = 50000
samples = 256
w = []
for _ in range(n // samples):
sample_z = mixing_noise(samples, args.latent, 0, device=device)
w.append(g_ema.style(sample_z))
w = torch.cat(w, dim=0)
args.mean_w = w.mean(dim=0)
os.makedirs(args.result_dir, exist_ok=True)
photo = transform(Image.open(
args.image).convert('RGB')).unsqueeze(0).to(device)
args.image_name = args.image.split('/')[-1].split('.')[0]
wp = invert(g_ema, perceptual, photo, device, args)