-
Notifications
You must be signed in to change notification settings - Fork 81
/
semantic_manipulation.py
469 lines (388 loc) · 16.1 KB
/
semantic_manipulation.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
"""
StyleMapGAN
Copyright (c) 2021-present NAVER Corp.
This work is licensed under the Creative Commons Attribution-NonCommercial
4.0 International License. To view a copy of this license, visit
http://creativecommons.org/licenses/by-nc/4.0/ or send a letter to
Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
"""
import datetime
import random, os
import argparse
import numpy as np
import torch
from torch import nn
from torch.utils import data
from torchvision import transforms, utils
from training.model import Generator, Encoder
from training.dataset import MultiResolutionDataset
from tqdm import tqdm
from sklearn import svm
import cv2
seed = 0
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def data_sampler(dataset, shuffle):
if shuffle:
return data.RandomSampler(dataset)
else:
return data.SequentialSampler(dataset)
class Sampler_with_index(data.Sampler):
def __init__(self, indexes):
self.indexes = indexes
def __iter__(self):
return (i for i in self.indexes)
def __len__(self):
return len(self.indexes)
class Model(nn.Module):
def __init__(self, device="cuda"):
super(Model, self).__init__()
self.g_ema = Generator(
train_args.size,
train_args.mapping_layer_num,
train_args.latent_channel_size,
train_args.latent_spatial_size,
lr_mul=train_args.lr_mul,
channel_multiplier=train_args.channel_multiplier,
normalize_mode=train_args.normalize_mode,
small_generator=train_args.small_generator,
)
self.e_ema = Encoder(
train_args.size,
train_args.latent_channel_size,
train_args.latent_spatial_size,
channel_multiplier=train_args.channel_multiplier,
)
def forward(self, images, mode, direction_vector=None, mask=None):
if mode == "project_latent":
fake_stylecode = self.e_ema(images)
return fake_stylecode
elif mode == "transform_to_other_part":
get_cuda_device = images.get_device()
fake_stylecode = self.e_ema(images)
direction_vector = (
direction_vector.to(get_cuda_device)
.unsqueeze(0)
.repeat(images.shape[0], 1, 1, 1)
)
transformed_images = []
for distance in range(-20, 1, 5):
modified_stylecode = fake_stylecode + distance * direction_vector
transformed_image, _ = self.g_ema(
modified_stylecode, input_is_stylecode=True
)
transformed_images.append(transformed_image)
direction_vector[mask == 0] = 0
for distance in range(-20, 1, 5):
modified_stylecode = fake_stylecode + distance * direction_vector
transformed_image, _ = self.g_ema(
modified_stylecode, input_is_stylecode=True
)
transformed_images.append(transformed_image)
return torch.cat(transformed_images)
def sample_data(loader):
while True:
for batch in loader:
yield batch
if __name__ == "__main__":
device = "cuda"
parser = argparse.ArgumentParser()
parser.add_argument("--ckpt", type=str)
parser.add_argument("--save_dir", type=str, default="expr/semantic_manipulation")
parser.add_argument("--LMDB", type=str, default="data/celeba_hq/LMDB")
parser.add_argument("--batch", type=int, default=16)
parser.add_argument("--num_workers", type=int, default=2)
parser.add_argument(
"--part", type=str, default="Heavy_Makeup"
) # No_Beard Smiling ..
parser.add_argument("--svm_train_iter", type=int, default=None)
args = parser.parse_args()
args.attr_celeba_hq = "semantic_manipulation/list_attr_celeba_hq.txt"
args.train_lmdb = f"{args.LMDB}_train"
args.val_lmdb = f"{args.LMDB}_val"
args.test_lmdb = f"{args.LMDB}_test"
model_name = args.ckpt.split("/")[-1].replace(".pt", "")
os.makedirs(os.path.join(args.save_dir, args.part), exist_ok=True)
args.inverted_npy = os.path.join(args.save_dir, f"{model_name}_inverted.npy")
args.boundary = os.path.join(
args.save_dir, args.part, f"{model_name}_{args.part}_boundary.npy"
)
print(args)
ckpt = torch.load(args.ckpt)
train_args = ckpt["train_args"] # load arguments!
model = Model()
model.g_ema.load_state_dict(ckpt["g_ema"])
model.e_ema.load_state_dict(ckpt["e_ema"])
model = model.to(device)
transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True),
]
)
train_dataset = MultiResolutionDataset(args.train_lmdb, transform, train_args.size)
val_dataset = MultiResolutionDataset(args.val_lmdb, transform, train_args.size)
test_dataset = MultiResolutionDataset(args.test_lmdb, transform, train_args.size)
dataset = data.ConcatDataset([train_dataset, val_dataset, test_dataset])
with torch.no_grad():
# if inverted latent does not exist, save inverted latent as npy format
if os.path.isfile(args.inverted_npy):
print("inverted_npy exists!")
latent_codes = np.load(args.inverted_npy)
else:
loader = data.DataLoader(
dataset,
batch_size=args.batch,
sampler=data_sampler(dataset, shuffle=False),
num_workers=args.num_workers,
pin_memory=True,
)
latent_codes_enc = []
for images in tqdm(loader):
images = images.to(device)
project_latent = model(images, "project_latent")
latent_codes_enc.append(project_latent.cpu().numpy())
latent_codes = np.concatenate(latent_codes_enc, axis=0)
np.save(f"{args.inverted_npy}", latent_codes)
latent_code_shape = latent_codes[0].shape
print(f"latent_code_shape {latent_code_shape}")
# flatten latent: Nx8x8x64 -> N x 8*8*64
latent_codes = latent_codes.reshape(len(latent_codes), -1)
# get boundary
part = args.part
if os.path.isfile(args.boundary):
print(f"{part} boundary exists!")
boundary_infos = torch.load(args.boundary)
boundary = boundary_infos["boundary"]
part1_indexes = boundary_infos["part1_indexes"]
part2_indexes = boundary_infos["part2_indexes"]
else:
############################################
# get positive and negative indices by ranking using pretrained network (stylegan1)
# refer to https://github.com/NVlabs/stylegan/tree/master/metrics/linear_separability.py
part_pos_neg_indices = [
"Male",
"Smiling",
"Attractive",
"Wavy_Hair",
"Young",
"5_o_Clock_Shadow",
"Arched_Eyebrows",
"Bags_Under_Eyes",
"Bald",
"Bangs",
"Big_Lips",
"Big_Nose",
"Black_Hair",
"Blond_Hair",
"Blurry",
"Brown_Hair",
"Bushy_Eyebrows",
"Chubby",
"Double_Chin",
"Eyeglasses",
"Goatee",
"Gray_Hair",
"Heavy_Makeup",
"High_Cheekbones",
"Mouth_Slightly_Open",
"Mustache",
"Narrow_Eyes",
"No_Beard",
"Oval_Face",
"Pale_Skin",
"Pointy_Nose",
"Receding_Hairline",
"Rosy_Cheeks",
"Sideburns",
"Straight_Hair",
"Wearing_Earrings",
"Wearing_Hat",
"Wearing_Lipstick",
"Wearing_Necklace",
"Wearing_Necktie",
]
pruned_index = part_pos_neg_indices.index(part)
part1_indexes = np.load(
f"semantic_manipulation/{pruned_index}_pos_indices.npy"
)
part2_indexes = np.load(
f"semantic_manipulation/{pruned_index}_neg_indices.npy"
)
# get boundary using two parts.
testset_ratio = 0.1
np.random.shuffle(part1_indexes)
np.random.shuffle(part2_indexes)
positive_len = len(part1_indexes)
negative_len = len(part2_indexes)
positive_train = latent_codes[
part1_indexes[int(positive_len * testset_ratio) :]
]
positive_val = latent_codes[
part1_indexes[: int(positive_len * testset_ratio)]
]
negative_train = latent_codes[
part2_indexes[int(negative_len * testset_ratio) :]
]
negative_val = latent_codes[
part2_indexes[: int(negative_len * testset_ratio)]
]
# Training set.
train_data = np.concatenate([positive_train, negative_train], axis=0)
train_label = np.concatenate(
[
np.ones(len(positive_train), dtype=np.int),
np.zeros(len(negative_train), dtype=np.int),
],
axis=0,
)
# Validation set.
val_data = np.concatenate([positive_val, negative_val], axis=0)
val_label = np.concatenate(
[
np.ones(len(positive_val), dtype=np.int),
np.zeros(len(negative_val), dtype=np.int),
],
axis=0,
)
print(
f"positive_train: {len(positive_train)}, negative_train:{len(negative_train)}, positive_val:{len(positive_val)}, negative_val:{len(negative_val)}"
)
print(f"Training boundary. {datetime.datetime.now()}")
if args.svm_train_iter:
clf = svm.SVC(kernel="linear", max_iter=args.svm_train_iter)
else:
clf = svm.SVC(kernel="linear")
classifier = clf.fit(train_data, train_label)
print(f"Finish training. {datetime.datetime.now()}")
print(f"validate boundary.")
val_prediction = classifier.predict(val_data)
correct_num = np.sum(val_label == val_prediction)
print(
f"Accuracy for validation set: "
f"{correct_num} / {len(val_data)} = "
f"{correct_num / (len(val_data)):.6f}"
)
print("classifier.coef_.shape", classifier.coef_.shape)
boundary = classifier.coef_.reshape(1, -1).astype(np.float32)
boundary = boundary / np.linalg.norm(boundary)
boundary = boundary.reshape(latent_code_shape)
print("boundary.shape", boundary.shape)
boundary = torch.from_numpy(boundary).float()
torch.save(
{
"boundary": boundary,
"part1_indexes": part1_indexes,
"part2_indexes": part2_indexes,
},
args.boundary,
)
with open(args.attr_celeba_hq, "r") as fp:
total_number = int(fp.readline())
print(f"{total_number} images, {len(latent_codes)} latent_codes")
assert total_number == len(latent_codes)
attrs = fp.readline().strip().split(" ")
# print(attrs) # ['5_o_Clock_Shadow', 'Arched_Eyebrows', 'Attractive', 'Bags_Under_Eyes', 'Bald', 'Bangs', 'Big_Lips', 'Big_Nose', 'Black_Hair', 'Blond_Hair', 'Blurry', 'Brown_Hair', 'Bushy_Eyebrows', 'Chubby', 'Double_Chin', 'Eyeglasses', 'Goatee', 'Gray_Hair', 'Heavy_Makeup', 'High_Cheekbones', 'Male', 'Mouth_Slightly_Open', 'Mustache', 'Narrow_Eyes', 'No_Beard', 'Oval_Face', 'Pale_Skin', 'Pointy_Nose', 'Receding_Hairline', 'Rosy_Cheeks', 'Sideburns', 'Smiling', 'Straight_Hair', 'Wavy_Hair', 'Wearing_Earrings', 'Wearing_Hat', 'Wearing_Lipstick', 'Wearing_Necklace', 'Wearing_Necktie', 'Young']
# Please note that the order is different from part_pos_neg_indices
for i, attr in enumerate(attrs):
if attr == args.part:
part_index = i
print(part, part_index)
break
read_line = fp.readline()
GT_part1_indexes = [] # part1: Heavy_Makeup
GT_part2_indexes = [] # part2: others
img_i = 0
pivot = 0
f_names = sorted(list(os.listdir("data/celeba_hq/raw_images/test/images/")))
while read_line:
parse_info = read_line.replace(" ", " ").strip().split(" ")
attr_info = parse_info[1:]
f_name = parse_info[0] # first element is filename
if f_name == f_names[pivot]:
if attr_info[part_index] == "1":
GT_part1_indexes.append(pivot)
else:
GT_part2_indexes.append(pivot)
pivot += 1
if pivot == 500:
break
img_i += 1
read_line = fp.readline()
test_part1_indexes = GT_part1_indexes
test_part2_indexes = GT_part2_indexes
part1_loader = data.DataLoader(
test_dataset,
batch_size=args.batch,
sampler=Sampler_with_index(test_part1_indexes),
)
part1_loader = sample_data(part1_loader)
part2_loader = data.DataLoader(
test_dataset,
batch_size=args.batch,
sampler=Sampler_with_index(test_part2_indexes),
)
part2_loader = sample_data(part2_loader)
part2_img = next(part2_loader).to(device)
# heavy makup
ref_p_x, ref_p_y, width, height = 2, 5, 4, 2
mask = (ref_p_x, ref_p_y, width, height)
mask = torch.zeros(part2_img.shape[0], 64, 8, 8)
mask[:, :, ref_p_y : ref_p_y + height, ref_p_x : ref_p_x + width] = 1
part2_to_part1 = model(
part2_img, "transform_to_other_part", -boundary, mask=mask
)
utils.save_image(
torch.cat([part2_img, part2_to_part1]),
f"{args.save_dir}/{part}/{model_name}_others_to_{part}.png",
nrow=args.batch,
normalize=True,
range=(-1, 1),
)
# heavy makup
s1 = np.array(list(range(13, 16 * 11, 16)))[[0, 3, 6, -1]]
s2 = np.array(list(range(15, 16 * 11, 16)))[[0, 3, 6, -1]]
ratio = train_args.size // train_args.latent_spatial_size
for img, s_n in zip(
torch.cat([part2_img, part2_to_part1])[
np.concatenate((s1, s2), axis=0).tolist()
],
[
"img1_ori",
"img1_global",
"img1_local",
"img1_recon",
"img2_ori",
"img2_global",
"img2_local",
"img2_recon",
],
):
if "local" in s_n:
img = torch.clamp(img, min=-1.0, max=1.0)
img = (img + 1) / 2
img = img.cpu()
img = transforms.ToPILImage()(img)
img = np.asarray(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
img = cv2.rectangle(
img,
(ref_p_x * ratio, ref_p_y * ratio),
((ref_p_x + width) * ratio, (ref_p_y + height) * ratio),
(102, 255, 255),
1,
)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = transforms.ToTensor()(img)
img = (img - 0.5) * 2
utils.save_image(
img,
f"{args.save_dir}/{part}/{part}_{s_n}.png",
nrow=args.batch,
normalize=True,
range=(-1, 1),
)