-
Notifications
You must be signed in to change notification settings - Fork 22
/
test.py
543 lines (508 loc) · 21.8 KB
/
test.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# python 3.7
"""Unit tests.
This script can also be used as a sample of code usage.
TODO: StyleGAN and StyleGAN2 models scannot be converted simultaneously, since
the scripts in `stylegan_tf_official` and `stylegan2_tf_official` conflict to
each other.
"""
import os
import argparse
import numpy as np
from models.model_settings import USE_CUDA, MODEL_POOL
from models.helper import build_generator
from utils.logger import setup_logger
from utils.visualizer import HtmlPageVisualizer, get_grid_shape
from utils.editor import parse_indices, interpolate, mix_style, manipulate
from utils.editor import get_layerwise_manipulation_strength
from utils.boundary_searcher import train_boundary, project_boundary
TEST_BATCH_SIZE = 1 # Small batch size in case of converting tensorflow weight.
RESULT_DIR = 'results'
if __name__ == '__main__':
parser = argparse.ArgumentParser(
'Test modules defined in folder `models` and `units`.')
parser.add_argument('--test_num', type=int, default=10,
help='Number of test samples. (default: 10)')
parser.add_argument('--verbose', action='store_true',
help='Wether to test all availabel models. (default: '
'False)')
parser.add_argument('--pggan', action='store_true',
help='Whether to test on PGGAN models. (default: False)')
parser.add_argument('--stylegan', action='store_true',
help='Whether to test on StyleGAN models. (default: '
'False)')
parser.add_argument('--stylegan2', action='store_true',
help='Whether to test on StyleGAN2 models. (default: '
'False)')
parser.add_argument('--editor', action='store_true',
help='Whether to test the editing functions. (default: '
'False)')
parser.add_argument('--boundary', action='store_true',
help='Whether to test the boundary related functions. '
'(default: False)')
parser.add_argument('--all', action='store_true',
help='Whether to execute all test. (default: False)')
args = parser.parse_args()
if not USE_CUDA:
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
else:
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
TEST_FLAG = False
###########################
#### Model Test Starts ####
###########################
# PGGAN Generator.
if args.pggan or args.all:
print('==== PGGAN Generator Test ====')
if args.verbose:
model_list = []
for model_name, model_setting in MODEL_POOL.items():
if model_setting['gan_type'] == 'pggan':
model_list.append(model_name)
else:
model_list = ['pggan_celebahq', 'pggan_bedroom']
for model_name in model_list:
logger = setup_logger(work_dir=RESULT_DIR,
logfile_name=f'{model_name}_generator_test.log',
logger_name=f'{model_name}_generator_logger')
G = build_generator(model_name, logger=logger)
G.batch_size = TEST_BATCH_SIZE
z = G.easy_sample(args.test_num)
x = G.easy_synthesize(z)['image']
visualizer = HtmlPageVisualizer(grid_size=args.test_num)
for i in range(visualizer.num_rows):
for j in range(visualizer.num_cols):
visualizer.set_cell(i, j, image=x[i * visualizer.num_cols + j])
visualizer.save(f'{RESULT_DIR}/{model_name}_generator_test.html')
del G
print('Pass!')
TEST_FLAG = True
# StyleGAN Generator.
if args.stylegan or args.all:
print('==== StyleGAN Generator Test ====')
if args.verbose:
model_list = []
for model_name, model_setting in MODEL_POOL.items():
if model_setting['gan_type'] == 'stylegan':
model_list.append(model_name)
else:
model_list = ['stylegan_ffhq', 'stylegan_car', 'stylegan_bedroom']
for model_name in model_list:
logger = setup_logger(work_dir=RESULT_DIR,
logfile_name=f'{model_name}_generator_test.log',
logger_name=f'{model_name}_generator_logger')
G = build_generator(model_name, logger=logger)
G.batch_size = TEST_BATCH_SIZE
z = G.easy_sample(args.test_num)
x = G.easy_synthesize(z)['image']
visualizer = HtmlPageVisualizer(grid_size=args.test_num)
for i in range(visualizer.num_rows):
for j in range(visualizer.num_cols):
visualizer.set_cell(i, j, image=x[i * visualizer.num_cols + j])
visualizer.save(f'{RESULT_DIR}/{model_name}_generator_test.html')
del G
print('Pass!')
TEST_FLAG = True
# StyleGAN2 Generator.
if args.stylegan2 or args.all:
print('==== StyleGAN2 Generator Test ====')
if args.verbose:
model_list = []
for model_name, model_setting in MODEL_POOL.items():
if model_setting['gan_type'] == 'stylegan2':
model_list.append(model_name)
else:
model_list = ['stylegan2_ffhq', 'stylegan2_car', 'stylegan2_church']
for model_name in model_list:
logger = setup_logger(work_dir=RESULT_DIR,
logfile_name=f'{model_name}_generator_test.log',
logger_name=f'{model_name}_generator_logger')
G = build_generator(model_name, logger=logger)
G.batch_size = TEST_BATCH_SIZE
z = G.easy_sample(args.test_num)
x = G.easy_synthesize(z)['image']
visualizer = HtmlPageVisualizer(grid_size=args.test_num)
for i in range(visualizer.num_rows):
for j in range(visualizer.num_cols):
visualizer.set_cell(i, j, image=x[i * visualizer.num_cols + j])
visualizer.save(f'{RESULT_DIR}/{model_name}_generator_test.html')
del G
print('Pass!')
TEST_FLAG = True
#########################
#### Model Test Ends ####
#########################
############################
#### Editor Test Starts ####
############################
if args.editor or args.all:
print('==== Grip Shape Test ====')
assert get_grid_shape(0) == (0, 0)
assert get_grid_shape(1) == (1, 1)
assert get_grid_shape(10) == (2, 5)
assert get_grid_shape(100) == (10, 10)
assert get_grid_shape(17) == (1, 17)
assert get_grid_shape(15) == (3, 5)
assert get_grid_shape(24) == (4, 6)
assert get_grid_shape(50) == (5, 10)
assert get_grid_shape(512) == (16, 32)
assert get_grid_shape(36) == (6, 6)
assert get_grid_shape(36, row=12) == (12, 3)
assert get_grid_shape(36, col=12) == (3, 12)
assert get_grid_shape(36, row=12, col=12) == (6, 6)
assert get_grid_shape(0, is_portrait=True) == (0, 0)
assert get_grid_shape(1, is_portrait=True) == (1, 1)
assert get_grid_shape(10, is_portrait=True) == (5, 2)
assert get_grid_shape(100, is_portrait=True) == (10, 10)
assert get_grid_shape(17, is_portrait=True) == (17, 1)
assert get_grid_shape(15, is_portrait=True) == (5, 3)
assert get_grid_shape(24, is_portrait=True) == (6, 4)
assert get_grid_shape(50, is_portrait=True) == (10, 5)
assert get_grid_shape(512, is_portrait=True) == (32, 16)
assert get_grid_shape(36, row=12, is_portrait=True) == (12, 3)
assert get_grid_shape(36, col=12, is_portrait=True) == (3, 12)
assert get_grid_shape(36, row=12, col=12, is_portrait=True) == (6, 6)
print('Pass!')
print('==== Index Parser Test ====')
assert parse_indices(None) == []
assert parse_indices('') == []
assert parse_indices([]) == []
assert parse_indices(0) == [0]
assert parse_indices('1,2,3') == [1, 2, 3]
assert parse_indices('1, 2, 3') == [1, 2, 3]
assert parse_indices('1, 2, 3, 5-7') == [1, 2, 3, 5, 6, 7]
assert (parse_indices('1, 2, 3, 5-7, 10, 12, 15-16') ==
[1, 2, 3, 5, 6, 7, 10, 12, 15, 16])
assert (parse_indices('1, 5-7, 2, 3, 10, 12, 15-16, 20') ==
[1, 2, 3, 5, 6, 7, 10, 12, 15, 16, 20])
print('Pass!')
num_layers = 18
dim = 512
print('==== Interpolation Test (single latent code)====')
step = 5
num = 16
a = np.random.randint(0, high=10000, size=(num, dim))
b = np.random.randint(0, high=10000, size=(num, dim))
res = interpolate(a, b, step=step)
assert res.shape == (num, step, dim)
assert np.all(res[:, 0] == a)
assert np.all(res[:, -1] == b)
error = 0
diff = (b - a) / (step - 1)
for s in range(1, step):
error += np.average(np.abs((res[:, s] - res[:, s - 1] - diff)))
print('Error:', error)
print('==== Interpolation Test (layer-wise latent code)====')
step = 5
num = 16
a = np.random.randint(0, high=10000, size=(num, num_layers, dim))
b = np.random.randint(0, high=10000, size=(num, num_layers, dim))
res = interpolate(a, b, step=step)
assert res.shape == (num, step, num_layers, dim)
assert np.all(res[:, 0] == a)
assert np.all(res[:, -1] == b)
error = 0
diff = (b - a) / (step - 1)
for s in range(1, step):
error += np.average(np.abs((res[:, s] - res[:, s - 1] - diff)))
print('Error:', error)
print('==== Style Mixing Test (single latent code) ====')
s_num = 16
c_num = 32
indices = parse_indices('0-1', min_val=0, max_val=num_layers - 1)
s = np.random.randint(0, high=10000, size=(s_num, dim))
c = np.random.randint(0, high=10000, size=(c_num, dim))
res = mix_style(style_codes=s,
content_codes=c,
num_layers=num_layers,
mix_layers=indices,
is_style_layerwise=False,
is_content_layerwise=False)
assert res.shape == (s_num, c_num, num_layers, dim)
error = 0
for i in range(s_num):
for j in range(c_num):
for k in range(num_layers):
if k in indices:
error += np.average(np.abs((res[i, j, k] - s[i])))
else:
error += np.average(np.abs((res[i, j, k] - c[j])))
print('Error:', error)
print('==== Style Mixing Test (layer-wise latent code) ====')
s_num = 32
c_num = 16
indices = parse_indices('3, 6, 9, 12', min_val=0, max_val=num_layers - 1)
s = np.random.randint(0, high=10000, size=(s_num, num_layers, dim))
c = np.random.randint(0, high=10000, size=(c_num, num_layers, dim))
res = mix_style(style_codes=s,
content_codes=c,
num_layers=num_layers,
mix_layers=indices)
assert res.shape == (s_num, c_num, num_layers, dim)
error = 0
for i in range(s_num):
for j in range(c_num):
for k in range(num_layers):
if k in indices:
error += np.average(np.abs((res[i, j, k] - s[i, k])))
else:
error += np.average(np.abs((res[i, j, k] - c[j, k])))
print('Error:', error)
print('==== Single Manipulation Test (single latent code) ====')
num = 64
start_distance = -10
end_distance = -start_distance
step = 21
strength = 0.7
x = np.random.randint(0, high=10000, size=(num, dim))
b = np.random.randint(0, high=10000, size=(1, dim))
res = manipulate(latent_codes=x,
boundary=b,
start_distance=start_distance,
end_distance=end_distance,
step=step,
layerwise_manipulation=False,
num_layers=1,
manipulate_layers=None,
is_code_layerwise=False,
is_boundary_layerwise=False,
layerwise_manipulation_strength=strength)
assert res.shape == (num, step, dim)
assert np.all(res[:, step // 2] == x)
diff = (end_distance - start_distance) / (step - 1) * b[0]
error = 0
for i in range(num):
for j in range(step):
error += np.average(np.abs(res[i, j] - res[i, 0] - diff * j))
print('Error:', error)
print('==== Layer-wise Manipulation Test (single latent code, '
'single boundary) ====')
num = 64
start_distance = -10
end_distance = -start_distance
step = 21
truncation_psi = 1.0
truncation_layers = 10
strength = get_layerwise_manipulation_strength(
num_layers, truncation_psi, truncation_layers)
indices = parse_indices('0-8, 10-12', min_val=0, max_val=num_layers - 1)
x = np.random.randint(0, high=10000, size=(num, dim))
b = np.random.randint(0, high=10000, size=(1, dim))
res = manipulate(latent_codes=x,
boundary=b,
start_distance=start_distance,
end_distance=end_distance,
step=step,
layerwise_manipulation=True,
num_layers=num_layers,
manipulate_layers=indices,
is_code_layerwise=False,
is_boundary_layerwise=False,
layerwise_manipulation_strength=strength)
assert res.shape == (num, step, num_layers, dim)
assert np.all(res[:, step // 2] == np.tile(x[:, np.newaxis],
(1, num_layers, 1)))
diff = (end_distance - start_distance) / (step - 1) * b[0]
error = 0
for i in range(num):
for j in range(step):
for k in range(num_layers):
if k in indices:
_diff = diff * (truncation_psi if k < truncation_layers else 1.0)
error += np.average(np.abs(res[i, j, k] - res[i, 0, k] - _diff * j))
else:
error += np.average(np.abs(res[i, j, k] - x[i]))
print('Error:', error)
print('==== Layer-wise Manipulation Test (layer-wise latent code, '
'single boundary) ====')
num = 64
start_distance = -10
end_distance = -start_distance
step = 21
truncation_psi = 0.7
truncation_layers = 0
strength = get_layerwise_manipulation_strength(
num_layers, truncation_psi, truncation_layers)
indices = parse_indices('1, 4, 7, 9, 17', min_val=0, max_val=num_layers - 1)
x = np.random.randint(0, high=10000, size=(num, num_layers, dim))
b = np.random.randint(0, high=10000, size=(1, dim))
res = manipulate(latent_codes=x,
boundary=b,
start_distance=start_distance,
end_distance=end_distance,
step=step,
layerwise_manipulation=True,
num_layers=num_layers,
manipulate_layers=indices,
is_code_layerwise=True,
is_boundary_layerwise=False,
layerwise_manipulation_strength=strength)
assert res.shape == (num, step, num_layers, dim)
assert np.all(res[:, step // 2] == x)
diff = (end_distance - start_distance) / (step - 1) * b[0]
error = 0
for i in range(num):
for j in range(step):
for k in range(num_layers):
if k in indices:
_diff = diff * (truncation_psi if k < truncation_layers else 1.0)
error += np.average(np.abs(res[i, j, k] - res[i, 0, k] - _diff * j))
else:
error += np.average(np.abs(res[i, j, k] - x[i, k]))
print('Error:', error)
print('==== Layer-wise Manipulation Test (single latent code, '
'layer-wise boundary) ====')
num = 64
start_distance = -10
end_distance = -start_distance
step = 21
truncation_psi = 0.5
truncation_layers = 10
strength = get_layerwise_manipulation_strength(
num_layers, truncation_psi, truncation_layers)
indices = parse_indices('0, 3, 17', min_val=0, max_val=num_layers - 1)
x = np.random.randint(0, high=10000, size=(num, dim))
b = np.random.randint(0, high=10000, size=(1, num_layers, dim))
res = manipulate(latent_codes=x,
boundary=b,
start_distance=start_distance,
end_distance=end_distance,
step=step,
layerwise_manipulation=True,
num_layers=num_layers,
manipulate_layers=indices,
is_code_layerwise=False,
is_boundary_layerwise=True,
layerwise_manipulation_strength=strength)
assert res.shape == (num, step, num_layers, dim)
assert np.all(res[:, step // 2] == np.tile(x[:, np.newaxis],
(1, num_layers, 1)))
error = 0
for i in range(num):
for j in range(step):
for k in range(num_layers):
diff = (end_distance - start_distance) / (step - 1) * b[0, k]
if k in indices:
_diff = diff * (truncation_psi if k < truncation_layers else 1.0)
error += np.average(np.abs(res[i, j, k] - res[i, 0, k] - _diff * j))
else:
error += np.average(np.abs(res[i, j, k] - x[i]))
print('Error:', error)
print('==== Layer-wise Manipulation Test (layer-wise latent code, '
'layer-wise boundary) ====')
num = 64
start_distance = -10
end_distance = -start_distance
step = 21
truncation_psi = 0.5
truncation_layers = num_layers
strength = get_layerwise_manipulation_strength(
num_layers, truncation_psi, truncation_layers)
indices = parse_indices('2, 5, 6-8, 10', min_val=0, max_val=num_layers - 1)
x = np.random.randint(0, high=10000, size=(num, num_layers, dim))
b = np.random.randint(0, high=10000, size=(1, num_layers, dim))
res = manipulate(latent_codes=x,
boundary=b,
start_distance=start_distance,
end_distance=end_distance,
step=step,
layerwise_manipulation=True,
num_layers=num_layers,
manipulate_layers=indices,
is_code_layerwise=True,
is_boundary_layerwise=True,
layerwise_manipulation_strength=strength)
assert res.shape == (num, step, num_layers, dim)
assert np.all(res[:, step // 2] == x)
error = 0
for i in range(num):
for j in range(step):
for k in range(num_layers):
diff = (end_distance - start_distance) / (step - 1) * b[0, k]
if k in indices:
_diff = diff * (truncation_psi if k < truncation_layers else 1.0)
error += np.average(np.abs(res[i, j, k] - res[i, 0, k] - _diff * j))
else:
error += np.average(np.abs(res[i, j, k] - x[i, k]))
print('Error:', error)
TEST_FLAG = True
#########################
#### Editor Test End ####
#########################
#####################################
#### Boundary Search Test Starts ####
#####################################
if args.boundary or args.all:
dim = 512
print('==== Boundary Projection Test (no condition) ====')
a = np.random.randn(1, dim).astype(np.float32)
a = a / np.linalg.norm(a)
proj = project_boundary(a)
print(f'Boundary Norm: {np.linalg.norm(proj)}')
print(f'Error: {1 - np.sum(proj * a)}')
print('==== Boundary Projection Test (single condition) ====')
a = np.random.randn(1, dim).astype(np.float32)
a = a / np.linalg.norm(a)
b = np.random.randn(1, dim).astype(np.float32)
b = b / np.linalg.norm(b)
proj = project_boundary(a, b)
print(f'Boundary Norm: {np.linalg.norm(proj)}')
print(f'Error: {np.sum(proj * b)}')
print('==== Boundary Projection Test (multiple conditions) ====')
a = np.random.randn(1, dim).astype(np.float32)
a = a / np.linalg.norm(a)
b = np.random.randn(1, dim).astype(np.float32)
b = b / np.linalg.norm(b)
c = np.random.randn(1, dim).astype(np.float32)
c = c / np.linalg.norm(c)
proj = project_boundary(a, b, c)
print(f'Boundary Norm: {np.linalg.norm(proj)}')
print(f'Error: {np.sum(proj * b) + np.sum(proj * c)}')
num = 1000
chosen_ratio = 0.3
data = np.random.randn(num, dim).astype(np.float32)
logger = setup_logger(work_dir=RESULT_DIR,
logfile_name=f'boundary_search_test.log',
logger_name=f'boundary_search_logger')
print('==== Boundary Search Test (using labels) ====')
labels = (np.random.randn(num) > 0.5).astype(np.bool)
boundary = train_boundary(data,
labels=labels,
verbose_test=True,
logger=logger)
assert boundary.shape == (1, dim)
print(f'Boundary norm: {np.linalg.norm(boundary)}')
print('==== Boundary Search Test (using scores with chosen num) ====')
scores = np.random.randn(num).astype(np.float32)
boundary = train_boundary(data,
scores=scores,
chosen_num_or_ratio=chosen_ratio * num,
verbose_test=True,
logger=logger)
assert boundary.shape == (1, dim)
print(f'Boundary norm: {np.linalg.norm(boundary)}')
print('==== Boundary Search Test (using scores with chosen ratio) ====')
scores = np.random.randn(num).astype(np.float32)
boundary = train_boundary(data,
scores=scores,
chosen_num_or_ratio=chosen_ratio,
verbose_test=True,
logger=logger)
assert boundary.shape == (1, dim)
print(f'Boundary norm: {np.linalg.norm(boundary)}')
print('==== Boundary Search Test (using scores with filtering) ====')
scores = np.random.randn(num).astype(np.float32)
boundary = train_boundary(data,
scores=scores,
invalid_value=scores[0],
chosen_num_or_ratio=chosen_ratio,
verbose_test=True,
logger=logger)
assert boundary.shape == (1, dim)
print(f'Boundary norm: {np.linalg.norm(boundary)}')
TEST_FLAG = True
###################################
#### Boundary Search Test Ends ####
###################################
if not TEST_FLAG:
raise SystemExit('No test has been executed! '
'Please use --help to see detailed usage.')