rolux / stylegan2encoder

StyleGAN2 - Official TensorFlow Implementation
http://arxiv.org/abs/1912.04958
Other
380 stars 71 forks source link

Uploaded images Style-mixing #10

Open shahik opened 4 years ago

shahik commented 4 years ago

Hi, thanks for adding this. how can I perform Style-mixing with two/more Uploaded Images? here's code that I tried.

  def style_mixing_example(network_pkl, src_dlatents, dst_dlatents, 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_images_to_uint8, nchw_to_nhwc=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 = np.load('latent_representations/2_01.npy')
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]

#image generation 
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))}

 '''

# these 2 lines are from stylegan-encoder!  
src_images = Gs.components.synthesis.run(src_dlatents, **Gs_syn_kwargs) # [minibatch, height, width, channel]
dst_images = Gs.components.synthesis.run(dst_dlatents,**Gs_syn_kwargs )

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]
        image_dict[(row_seed, col_seed)] = image

print('Saving images...')
for (row_seed, col_seed), image in image_dict.items():
    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 = Gs.output_shape
canvas = PIL.Image.new('RGB', (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.paste(PIL.Image.fromarray(image_dict[key], 'RGB'), (W * col_idx, H * row_idx))
canvas.save(dnnlib.make_run_dir_path('grid.png'))

   tflib.init_tf()
   synthesis_kwargs = dict(output_transform=dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True), minibatch_size=1)
   _Gs_cache = dict()

  style_mixing_example(network_pkl, src_dlatents=2_01.reshape((1, 18, 512)), dst_dlatents=01.reshape((1, 18, 512)), 1, col_styles=[range(6,14)], minibatch_size=4)

Greetings!