-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrun_generator.py
executable file
·496 lines (377 loc) · 25.1 KB
/
run_generator.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
# ------------------------------------------------------------------------
# Generator Script for 3D StyleGAN
# ------------------------------------------------------------------------
# Original StyleGAN2 Copyright
# ------------------------------------------------------------------------
# Copyright (c) 2019, NVIDIA Corporation. All rights reserved.
#
# This work is made available under the Nvidia Source Code License-NC.
# To view a copy of this license, visit
# https://nvlabs.github.io/stylegan2/license.html
import argparse
import numpy as np
import PIL.Image
import dnnlib
import dnnlib.tflib as tflib
import re
import sys
import os
import pretrained_networks
import nibabel as nib
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
#asd
#----------------------------------------------------------------------------
# def generate_images(network_pkl, seeds, truncation_psi):
# print('Loading networks from "%s"...' % network_pkl)
# _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
# noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
# Gs_kwargs = dnnlib.EasyDict()
# Gs_kwargs.output_transform = dict(func=tflib.convert_3d_images_to_uint8, nchwd_to_nhwdc=True)
# Gs_kwargs.randomize_noise = False
# if truncation_psi is not None:
# Gs_kwargs.truncation_psi = truncation_psi
# for seed_idx, seed in enumerate(seeds):
# print('Generating image for seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds)))
# rnd = np.random.RandomState(seed)
# z = rnd.randn(1, *Gs.input_shape[1:]) # [minibatch, component]
# tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars}) # [height, width]
# images = Gs.run(z, None, **Gs_kwargs) # [minibatch, height, width, channel]
# img = nib.Nifti1Image( images[0, :, :, :, 0 ], np.eye(4))
# nib.save( img, dnnlib.make_run_dir_path('seed%04d.nii.gz' % seed) )
# PIL.Image.fromarray(images[0, images.shape[1]//2, :, :, 0 ], 'L').save(dnnlib.make_run_dir_path('seed%04d_x.png' % seed))
# PIL.Image.fromarray(images[0, :, images.shape[2]//2, :, 0 ], 'L').save(dnnlib.make_run_dir_path('seed%04d_y.png' % seed))
# PIL.Image.fromarray(images[0, :, :, images.shape[3]//2, 0 ], 'L').save(dnnlib.make_run_dir_path('seed%04d_z.png' % seed))
def generate_images(network_pkl, seeds, truncation_psi):
print('Loading networks from "%s"...' % network_pkl)
_G, _D, Gs = pretrained_networks.load_networks(network_pkl)
noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
w_avg = Gs.get_var('dlatent_avg') # [component]
Gs_syn_kwargs = dnnlib.EasyDict()
Gs_syn_kwargs.output_transform = dict(func=tflib.convert_3d_images_to_uint8, nchwd_to_nhwdc=True)
Gs_syn_kwargs.randomize_noise = True
Gs_syn_kwargs.minibatch_size = 1
# Gs_kwargs = dnnlib.EasyDict()
# Gs_kwargs.output_transform = dict(func=tflib.convert_3d_images_to_uint8, nchwd_to_nhwdc=True)
# Gs_kwargs.randomize_noise = False
# if truncation_psi is not None:
# Gs_kwargs.truncation_psi = truncation_psi
for seed_idx, seed in enumerate(seeds):
print('Generating image for seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds)))
rnd = np.random.RandomState(seed)
z = rnd.randn(1, *Gs.input_shape[1:]) # [minibatch, component]
w = Gs.components.mapping.run( z, None )
w = w_avg + (w - w_avg) * truncation_psi # [minibatch, layer, component]
images = Gs.components.synthesis.run( w, **Gs_syn_kwargs)
# tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars}) # [height, width]
# images = Gs.run(z, None, **Gs_kwargs) # [minibatch, height, width, channel]
img = nib.Nifti1Image( images[0, :, :, :, 0 ], np.eye(4))
nib.save( img, dnnlib.make_run_dir_path('seed%04d.nii.gz' % seed) )
PIL.Image.fromarray(images[0, images.shape[1]//2, :, :, 0 ], 'L').save(dnnlib.make_run_dir_path('seed%04d_x.png' % seed))
PIL.Image.fromarray(images[0, :, images.shape[2]//2, :, 0 ], 'L').save(dnnlib.make_run_dir_path('seed%04d_y.png' % seed))
PIL.Image.fromarray(images[0, :, :, images.shape[3]//2, 0 ], 'L').save(dnnlib.make_run_dir_path('seed%04d_z.png' % seed))
#----------------------------------------------------------------------------
def style_mixing_example(network_pkl, row_seeds, col_seeds, truncation_psi, col_styles, minibatch_size=4):
print('Loading networks from "%s"...' % network_pkl)
_G, _D, Gs = pretrained_networks.load_networks(network_pkl)
w_avg = Gs.get_var('dlatent_avg') # [component]
Gs_syn_kwargs = dnnlib.EasyDict()
Gs_syn_kwargs.output_transform = dict(func=tflib.convert_3d_images_to_uint8, nchwd_to_nhwdc=True)
Gs_syn_kwargs.randomize_noise = True
Gs_syn_kwargs.minibatch_size = minibatch_size
print('Generating W vectors...')
all_seeds = list(set(row_seeds + col_seeds))
# if os.path.exists( "/data/vision/polina/users/razvan/sungmin/stylegan2/_all_z_temp.npy" ):
# all_z = np.load( "/data/vision/polina/users/razvan/sungmin/stylegan2/_all_z_temp.npy" )
# else:
# all_z = np.stack([np.random.RandomState(seed).randn(*Gs.input_shape[1:]) for seed in all_seeds]) # [minibatch, component]
# np.save( "/data/vision/polina/users/razvan/sungmin/stylegan2/_all_z_temp.npy", all_z )
# all_z = np.stack([np.random.RandomState(seed).randn(*Gs.input_shape[1:]) for seed in all_seeds]) # [minibatch, component]
# np.save( "/data/vision/polina/users/razvan/sungmin/stylegan2/_all_z_temp.npy", all_z )
all_z = np.stack([np.random.RandomState(seed).randn(*Gs.input_shape[1:]) for seed in all_seeds]) # [minibatch, component]
np.save( "/data/vision/polina/users/razvan/sungmin/stylegan2/_all_z_temp.npy", all_z )
all_w = Gs.components.mapping.run(all_z, None) # [minibatch, layer, component]
print( "=========================" )
print( "all_z" )
print( "=========================" )
print( all_z )
print( "=========================" )
print( "all_w" )
print( "=========================" )
print( all_w )
print( "=========================" )
print( "all_z.shape" )
print( "=========================" )
print( all_z.shape )
print( "=========================" )
print( "all_w.shape" )
print( "=========================" )
print( all_w.shape )
all_w = w_avg + (all_w - w_avg) * truncation_psi # [minibatch, layer, component]
w_dict = {seed: w for seed, w in zip(all_seeds, list(all_w))} # [layer, component]
print('Generating images...')
all_images = Gs.components.synthesis.run(all_w, **Gs_syn_kwargs) # [minibatch, height, width, channel]
image_dict = {(seed, seed): image for seed, image in zip(all_seeds, list(all_images))}
print('Generating style-mixed images...')
for row_seed in row_seeds:
for col_seed in col_seeds:
w = w_dict[row_seed].copy()
w[col_styles] = w_dict[col_seed][col_styles]
image = Gs.components.synthesis.run(w[np.newaxis], **Gs_syn_kwargs)[0]
print( "=======================================" )
print( image.shape )
print( "========================================" )
image_dict[(row_seed, col_seed)] = image
print('Saving images...')
for (row_seed, col_seed), image in image_dict.items():
img = nib.Nifti1Image( image[ :, :, :, 0 ], np.eye(4))
nib.save( img, dnnlib.make_run_dir_path('%d-%d.nii.gz' % (row_seed, col_seed)) )
PIL.Image.fromarray(image[image.shape[0]//2, :, :, 0 ], 'L').save(dnnlib.make_run_dir_path('%d-%d_x.png' % (row_seed, col_seed)))
PIL.Image.fromarray(image[ :, image.shape[1]//2, :, 0 ], 'L').save(dnnlib.make_run_dir_path('%d-%d_y.png' % (row_seed, col_seed)))
PIL.Image.fromarray(image[ :, :, image.shape[2]//2, 0 ], 'L').save(dnnlib.make_run_dir_path('%d-%d_z.png' % (row_seed, col_seed)))
# PIL.Image.fromarray(image, 'RGB').save(dnnlib.make_run_dir_path('%d-%d.png' % (row_seed, col_seed)))
print('Saving image grid...')
_N, _C, H, W, D = Gs.output_shape
canvas_x = PIL.Image.new('L', ( W * (len(col_seeds) + 1), H * (len(row_seeds) + 1)), 'black')
for row_idx, row_seed in enumerate([None] + row_seeds):
for col_idx, col_seed in enumerate([None] + col_seeds):
if row_seed is None and col_seed is None:
continue
key = (row_seed, col_seed)
if row_seed is None:
key = (col_seed, col_seed)
if col_seed is None:
key = (row_seed, row_seed)
canvas_x.paste(PIL.Image.fromarray(image_dict[key][ :, :, D//2, 0 ], 'L'), (W * col_idx, H * row_idx))
canvas_x.save(dnnlib.make_run_dir_path('grid_x.png'))
canvas_y = PIL.Image.new('L', (D * (len(col_seeds) + 1), W * (len(row_seeds) + 1)), 'black')
for row_idx, row_seed in enumerate([None] + row_seeds):
for col_idx, col_seed in enumerate([None] + col_seeds):
if row_seed is None and col_seed is None:
continue
key = (row_seed, col_seed)
if row_seed is None:
key = (col_seed, col_seed)
if col_seed is None:
key = (row_seed, row_seed)
canvas_y.paste(PIL.Image.fromarray(image_dict[key][ H//2, :, :, 0 ], 'L'), (D * col_idx, W * row_idx))
canvas_y.save(dnnlib.make_run_dir_path('grid_y.png'))
canvas_z = PIL.Image.new('L', (D * (len(col_seeds) + 1), H * (len(row_seeds) + 1)), 'black')
for row_idx, row_seed in enumerate([None] + row_seeds):
for col_idx, col_seed in enumerate([None] + col_seeds):
if row_seed is None and col_seed is None:
continue
key = (row_seed, col_seed)
if row_seed is None:
key = (col_seed, col_seed)
if col_seed is None:
key = (row_seed, row_seed)
canvas_z.paste(PIL.Image.fromarray(image_dict[key][ :, W//2, :, 0 ], 'L'), (D * col_idx, H * row_idx))
canvas_z.save(dnnlib.make_run_dir_path('grid_z.png'))
#----------------------------------------------------------------------------
def interpolation_example(network_pkl, row_seeds, col_seeds, truncation_psi, col_styles, minibatch_size=4):
print('Loading networks from "%s"...' % network_pkl)
_G, _D, Gs = pretrained_networks.load_networks(network_pkl)
w_avg = Gs.get_var('dlatent_avg') # [component]
Gs_syn_kwargs = dnnlib.EasyDict()
Gs_syn_kwargs.output_transform = dict(func=tflib.convert_3d_images_to_uint8, nchwd_to_nhwdc=True)
Gs_syn_kwargs.randomize_noise = False
Gs_syn_kwargs.minibatch_size = minibatch_size
print('Generating W vectors...')
all_seeds = list(set(row_seeds + col_seeds))
all_z = np.stack([np.random.RandomState(seed).randn(*Gs.input_shape[1:]) for seed in all_seeds]) # [minibatch, component]
all_w = Gs.components.mapping.run(all_z, None) # [minibatch, layer, component]
all_w = w_avg + (all_w - w_avg) * truncation_psi # [minibatch, layer, component]
w_dict = {seed: w for seed, w in zip(all_seeds, list(all_w))} # [layer, component]
print('Generating images...')
all_images = Gs.components.synthesis.run(all_w, **Gs_syn_kwargs) # [minibatch, height, width, channel]
interp_ratio = np.linspace( 0.0, 1.0, num=10, endpoint=True )
image_dict = {(seed, seed, interp_idx ): image for seed, interp_idx, image in zip(all_seeds, np.arange( len( interp_ratio ) ), list(all_images))}
print('Generating interpolated images...')
for row_seed in row_seeds:
for col_seed in col_seeds:
w_row = w_dict[ row_seed ].copy()
w_col = w_dict[ col_seed ].copy()
for interp_idx in range( len ( interp_ratio ) ):
w = w_dict[row_seed].copy()
w[ col_styles ] = ( 1.0 - interp_ratio[ interp_idx ] ) * w_row[ col_styles ] + ( interp_ratio[ interp_idx ] * w_col[ col_styles ] )
image = Gs.components.synthesis.run(w[np.newaxis], **Gs_syn_kwargs)[0]
image_dict[(row_seed, col_seed, interp_idx ) ] = image
print('Saving images...')
for (row_seed, col_seed, interp_idx ), image in image_dict.items():
img = nib.Nifti1Image( image[ :, :, :, 0 ], np.eye(4))
nib.save( img, dnnlib.make_run_dir_path('%d-%d-%d.nii.gz' % (row_seed, col_seed, interp_idx ) ) )
PIL.Image.fromarray(image[image.shape[0]//2, :, :, 0 ], 'L').save(dnnlib.make_run_dir_path('x-%d-%d-%d.png' % (row_seed, col_seed, interp_idx )))
PIL.Image.fromarray(image[ :, image.shape[1]//2, :, 0 ], 'L').save(dnnlib.make_run_dir_path('y-%d-%d-%d.png' % (row_seed, col_seed, interp_idx)))
PIL.Image.fromarray(image[ :, :, image.shape[2]//2, 0 ], 'L').save(dnnlib.make_run_dir_path('z-%d-%d-%d.png' % (row_seed, col_seed, interp_idx)))
# PIL.Image.fromarray(image, 'RGB').save(dnnlib.make_run_dir_path('%d-%d.png' % (row_seed, col_seed)))
# print('Saving image grid...')
# _N, _C, H, W, D = Gs.output_shape
# canvas_x = PIL.Image.new('L', ( W * (len(col_seeds) + 1), H * (len(row_seeds) + 1)), 'black')
# for row_idx, row_seed in enumerate([None] + row_seeds):
# for col_idx, col_seed in enumerate([None] + col_seeds):
# if row_seed is None and col_seed is None:
# continue
# key = (row_seed, col_seed)
# if row_seed is None:
# key = (col_seed, col_seed)
# if col_seed is None:
# key = (row_seed, row_seed)
# canvas_x.paste(PIL.Image.fromarray(image_dict[key][ :, :, D//2, 0 ], 'L'), (W * col_idx, H * row_idx))
# canvas_x.save(dnnlib.make_run_dir_path('grid_x.png'))
# canvas_y = PIL.Image.new('L', (D * (len(col_seeds) + 1), W * (len(row_seeds) + 1)), 'black')
# for row_idx, row_seed in enumerate([None] + row_seeds):
# for col_idx, col_seed in enumerate([None] + col_seeds):
# if row_seed is None and col_seed is None:
# continue
# key = (row_seed, col_seed)
# if row_seed is None:
# key = (col_seed, col_seed)
# if col_seed is None:
# key = (row_seed, row_seed)
# canvas_y.paste(PIL.Image.fromarray(image_dict[key][ H//2, :, :, 0 ], 'L'), (D * col_idx, W * row_idx))
# canvas_y.save(dnnlib.make_run_dir_path('grid_y.png'))
# canvas_z = PIL.Image.new('L', (D * (len(col_seeds) + 1), H * (len(row_seeds) + 1)), 'black')
# for row_idx, row_seed in enumerate([None] + row_seeds):
# for col_idx, col_seed in enumerate([None] + col_seeds):
# if row_seed is None and col_seed is None:
# continue
# key = (row_seed, col_seed)
# if row_seed is None:
# key = (col_seed, col_seed)
# if col_seed is None:
# key = (row_seed, row_seed)
# canvas_z.paste(PIL.Image.fromarray(image_dict[key][ :, W//2, :, 0 ], 'L'), (D * col_idx, H * row_idx))
# canvas_z.save(dnnlib.make_run_dir_path('grid_z.png'))
#----------------------------------------------------------------------------
def average_image_example(network_pkl, num_seeds, truncation_psi, col_styles, minibatch_size=4):
print('Loading networks from "%s"...' % network_pkl)
_G, _D, Gs = pretrained_networks.load_networks(network_pkl)
w_avg = Gs.get_var('dlatent_avg') # [component]
Gs_syn_kwargs = dnnlib.EasyDict()
Gs_syn_kwargs.output_transform = dict(func=tflib.convert_3d_images_to_uint8, nchwd_to_nhwdc=True)
Gs_syn_kwargs.randomize_noise = False
Gs_syn_kwargs.minibatch_size = minibatch_size
print('Generating W vectors...')
seeds = np.random.randint( 1, high=100000, size=num_seeds )
all_seeds = list( seeds )
all_z = np.stack([np.random.RandomState(seed).randn(*Gs.input_shape[1:]) for seed in all_seeds]) # [minibatch, component]
all_w = Gs.components.mapping.run(all_z, None) # [minibatch, layer, component]
all_w = w_avg + (all_w - w_avg) * truncation_psi # [minibatch, layer, component]
w_dict = {seed: w for seed, w in zip(all_seeds, list(all_w))} # [layer, component]
avg_w = np.average( all_w, axis=0 )
print('Generating images...')
all_images = Gs.components.synthesis.run(all_w, **Gs_syn_kwargs) # [minibatch, height, width, channel]
image_dict = {(seed): image for seed, image in zip(all_seeds, list(all_images))}
print('Generating interpolated images...')
for seed in seeds:
w = w_dict[ seed ].copy()
image = Gs.components.synthesis.run(w[np.newaxis], **Gs_syn_kwargs)[0]
image_dict[ ( seed ) ] = image
print('Saving images...')
for ( seed ), image in image_dict.items():
img = nib.Nifti1Image( image[ :, :, :, 0 ], np.eye(4))
nib.save( img, dnnlib.make_run_dir_path('%d.nii.gz' % ( seed ) ) )
PIL.Image.fromarray(image[image.shape[0]//2, :, :, 0 ], 'L').save(dnnlib.make_run_dir_path('x-%d.png' % ( seed ) ) )
PIL.Image.fromarray(image[ :, image.shape[1]//2, :, 0 ], 'L').save(dnnlib.make_run_dir_path('y-%d.png' % ( seed ) ) )
PIL.Image.fromarray(image[ :, :, image.shape[2]//2, 0 ], 'L').save(dnnlib.make_run_dir_path('z-%d.png' % ( seed ) ) )
print( 'Generating the average image' )
avg_gen_img = Gs.components.synthesis.run(avg_w[np.newaxis], **Gs_syn_kwargs)[0]
avg_img = nib.Nifti1Image( avg_gen_img[ :, :, :, 0 ], np.eye(4))
nib.save( avg_img, dnnlib.make_run_dir_path('average.nii.gz' ) )
PIL.Image.fromarray(avg_gen_img[avg_gen_img.shape[0]//2, :, :, 0 ], 'L').save(dnnlib.make_run_dir_path('x-average.png' ) )
PIL.Image.fromarray(avg_gen_img[ :, avg_gen_img.shape[1]//2, :, 0 ], 'L').save(dnnlib.make_run_dir_path('y-average.png' ) )
PIL.Image.fromarray(avg_gen_img[ :, :, avg_gen_img.shape[2]//2, 0 ], 'L').save(dnnlib.make_run_dir_path('z-average.png' ) )
print( 'Linear interpolation from the average image to the generated images' )
interp_ratio = np.linspace( 0.0, 1.0, num=10, endpoint=True )
interp_image_dict = {( seed, interp_idx ): image for seed, interp_idx, image in zip(all_seeds, np.arange( len( interp_ratio ) ), list(all_images))}
for seed in seeds:
w = avg_w.copy()
w_in = w_dict[ seed ].copy()
for interp_idx in range( len( interp_ratio ) ):
w[ col_styles ] = ( 1.0 - interp_ratio[ interp_idx ] ) * avg_w[ col_styles ] + ( interp_ratio[ interp_idx ] * w_in[ col_styles ] )
image = Gs.components.synthesis.run(w[np.newaxis], **Gs_syn_kwargs)[0]
interp_image_dict[ (seed, interp_idx ) ] = image
for ( seed, interp_idx ), image in interp_image_dict.items():
img = nib.Nifti1Image( image[ :, :, :, 0 ], np.eye(4))
nib.save( img, dnnlib.make_run_dir_path('%d-%d.nii.gz' % (seed, interp_idx ) ) )
PIL.Image.fromarray(image[image.shape[0]//2, :, :, 0 ], 'L').save(dnnlib.make_run_dir_path('x-%d-%d.png' % (seed, interp_idx ) ) )
PIL.Image.fromarray(image[ :, image.shape[1]//2, :, 0 ], 'L').save(dnnlib.make_run_dir_path('y-%d-%d.png' % (seed, interp_idx ) ) )
PIL.Image.fromarray(image[ :, :, image.shape[2]//2, 0 ], 'L').save(dnnlib.make_run_dir_path('z-%d-%d.png' % (seed, interp_idx ) ) )
#----------------------------------------------------------------------------
def _parse_num_range(s):
'''Accept either a comma separated list of numbers 'a,b,c' or a range 'a-c' and return as a list of ints.'''
range_re = re.compile(r'^(\d+)-(\d+)$')
m = range_re.match(s)
if m:
return list(range(int(m.group(1)), int(m.group(2))+1))
vals = s.split(',')
return [int(x) for x in vals]
#----------------------------------------------------------------------------
_examples = '''examples:
# Generate ffhq uncurated images (matches paper Figure 12)
python %(prog)s generate-images --network=gdrive:networks/stylegan2-ffhq-config-f.pkl --seeds=6600-6625 --truncation-psi=0.5
# Generate ffhq curated images (matches paper Figure 11)
python %(prog)s generate-images --network=gdrive:networks/stylegan2-ffhq-config-f.pkl --seeds=66,230,389,1518 --truncation-psi=1.0
# Generate uncurated car images (matches paper Figure 12)
python %(prog)s generate-images --network=gdrive:networks/stylegan2-car-config-f.pkl --seeds=6000-6025 --truncation-psi=0.5
# Generate style mixing example (matches style mixing video clip)
python %(prog)s style-mixing-example --network=gdrive:networks/stylegan2-ffhq-config-f.pkl --row-seeds=85,100,75,458,1500 --col-seeds=55,821,1789,293 --truncation-psi=1.0
'''
#----------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
description='''StyleGAN2 generator.
Run 'python %(prog)s <subcommand> --help' for subcommand help.''',
epilog=_examples,
formatter_class=argparse.RawDescriptionHelpFormatter
)
subparsers = parser.add_subparsers(help='Sub-commands', dest='command')
parser_generate_images = subparsers.add_parser('generate-images', help='Generate images')
parser_generate_images.add_argument('--network', help='Network pickle filename', dest='network_pkl', required=True)
parser_generate_images.add_argument('--seeds', type=_parse_num_range, help='List of random seeds', required=True)
parser_generate_images.add_argument('--truncation-psi', type=float, help='Truncation psi (default: %(default)s)', default=0.5)
parser_generate_images.add_argument('--result-dir', help='Root directory for run results (default: %(default)s)', default='results', metavar='DIR')
parser_style_mixing_example = subparsers.add_parser('style-mixing-example', help='Generate style mixing video')
parser_style_mixing_example.add_argument('--network', help='Network pickle filename', dest='network_pkl', required=True)
parser_style_mixing_example.add_argument('--row-seeds', type=_parse_num_range, help='Random seeds to use for image rows', required=True)
parser_style_mixing_example.add_argument('--col-seeds', type=_parse_num_range, help='Random seeds to use for image columns', required=True)
parser_style_mixing_example.add_argument('--col-styles', type=_parse_num_range, help='Style layer range (default: %(default)s)', default='0-6')
parser_style_mixing_example.add_argument('--truncation-psi', type=float, help='Truncation psi (default: %(default)s)', default=0.5)
parser_style_mixing_example.add_argument('--result-dir', help='Root directory for run results (default: %(default)s)', default='results', metavar='DIR')
parser_interpolation_example = subparsers.add_parser('interpolation-example', help='Generate interpolation video')
parser_interpolation_example.add_argument('--network', help='Network pickle filename', dest='network_pkl', required=True)
parser_interpolation_example.add_argument('--row-seeds', type=_parse_num_range, help='Random seeds to use for image rows', required=True)
parser_interpolation_example.add_argument('--col-seeds', type=_parse_num_range, help='Random seeds to use for image columns', required=True)
parser_interpolation_example.add_argument('--col-styles', type=_parse_num_range, help='Style layer range (default: %(default)s)', default='0-6')
parser_interpolation_example.add_argument('--truncation-psi', type=float, help='Truncation psi (default: %(default)s)', default=0.5)
parser_interpolation_example.add_argument('--result-dir', help='Root directory for run results (default: %(default)s)', default='results', metavar='DIR')
parser_interpolation_example = subparsers.add_parser('average-image-example', help='Generate interpolation video')
parser_interpolation_example.add_argument('--network', help='Network pickle filename', dest='network_pkl', required=True)
parser_interpolation_example.add_argument('--num-seeds', type=int, help='Number of random seeds to use for image generation (Equal to the number of images)', required=True)
parser_interpolation_example.add_argument('--col-styles', type=_parse_num_range, help='Style layer range (default: %(default)s)', default='0-6')
parser_interpolation_example.add_argument('--truncation-psi', type=float, help='Truncation psi (default: %(default)s)', default=0.5)
parser_interpolation_example.add_argument('--result-dir', help='Root directory for run results (default: %(default)s)', default='results', metavar='DIR')
args = parser.parse_args()
kwargs = vars(args)
subcmd = kwargs.pop('command')
if subcmd is None:
print ('Error: missing subcommand. Re-run with --help for usage.')
sys.exit(1)
sc = dnnlib.SubmitConfig()
sc.num_gpus = 1
sc.submit_target = dnnlib.SubmitTarget.LOCAL
sc.local.do_not_copy_source_files = True
sc.run_dir_root = kwargs.pop('result_dir')
sc.run_desc = subcmd
func_name_map = {
'generate-images': 'run_generator.generate_images',
'style-mixing-example': 'run_generator.style_mixing_example',
'interpolation-example': 'run_generator.interpolation_example',
'average-image-example': 'run_generator.average_image_example'
}
dnnlib.submit_run(sc, func_name_map[subcmd], **kwargs)
#----------------------------------------------------------------------------
if __name__ == "__main__":
main()
#----------------------------------------------------------------------------