神经网络样式GAN样式混合问题

时间:2019-03-02 17:17:06

标签: python

神经网络从GitHub加载了经过预训练的文件,并成功生成了随机照片。这似乎是随机的。但是,当您运行脚本时,generate_figures.py会显示另外两个的混合森林的照片,也是随机的。 题。如何使神经网络混合两个用户定义的照片而不是生成的照片? 我对代码进行了更改,指定了照片的路径,但是最后,它会生成与我的照片脸无关的所有相同随机数。

import os
import pickle
import numpy as np
import PIL.Image
import dnnlib
import dnnlib.tflib as tflib
import config

#----------------------------------------------------------------------------
# Helpers for loading and using pre-trained generators.

url_ffhq        = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl

synthesis_kwargs = dict(output_transform=dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True), minibatch_size=4)

_Gs_cache = dict()

def load_Gs(url):
    if url not in _Gs_cache:
        with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f:
            _G, _D, Gs = pickle.load(f)
        _Gs_cache[url] = Gs
    return _Gs_cache[url]

图3:样式混合。

def draw_style_mixing_figure(png, Gs, w, h, src_seeds, dst_seeds, style_ranges):
   print(png)

   src_latents = np.stack(np.random.RandomState(seed).randn(Gs.input_shape[1]) for seed in src_seeds)
   dst_latents = np.stack(np.random.RandomState(seed).randn(Gs.input_shape[1]) for seed in dst_seeds)
   src_dlatents = Gs.components.mapping.run(src_latents, None) # [seed, layer, component]
   dst_dlatents = Gs.components.mapping.run(dst_latents, None) # [seed, layer, component]
   src_images = Gs.components.synthesis.run(src_dlatents, randomize_noise=False, **synthesis_kwargs)
   dst_images = Gs.components.synthesis.run(dst_dlatents, randomize_noise=False, **synthesis_kwargs)

   canvas = PIL.Image.new('RGB', (w * (len(src_seeds) + 1), h * (len(dst_seeds) + 1)), 'white')

   for col, src_image in enumerate(list(src_images)):
       canvas.paste(PIL.Image.open(r"C:\Users\Kurmyavochka\Desktop\NN\REALISM\stylegan-master\results\1.png"), ((col + 1) * w, 0))
   for row, dst_image in enumerate(list(dst_images)):
       canvas.paste(PIL.Image.open(r"C:\Users\Kurmyavochka\Desktop\NN\REALISM\stylegan-master\results\2.png"), (0, (row + 1) * h))

       row_dlatents = np.stack([dst_dlatents[row]] * len(src_seeds))
       row_dlatents[:, style_ranges[row]] = src_dlatents[:, style_ranges[row]]

       row_images = Gs.components.synthesis.run(row_dlatents, randomize_noise=False, **synthesis_kwargs)

       for col, image in enumerate(list(row_images)):
           canvas.paste(PIL.Image.fromarray(image, 'RGB'), ((col + 1) * w, (row + 1) * h))
   canvas.save(png)

def main():

   tflib.init_tf()
   os.makedirs(config.result_dir, exist_ok=True)

   issa = 5067
   for iter in range(1):
       draw_style_mixing_figure(
           os.path.join(config.result_dir,
                        str(issa) + 'figure03-style-mixing.png'),
           load_Gs(url_ffhq),
           w=1024,
           h=1024,
           src_seeds=[0],
           dst_seeds=[0],
           style_ranges=[range(0, 4)] * 3 + [range(4, 8)] * 2 +
           [range(8, 18)])
       issa = issa + 1


if __name__ == "__main__":
   main()


1 个答案:

答案 0 :(得分:0)

您需要生成要融合的照片的潜在表示。比从它们创建平均向量。最后,使用脚本生成图像。不错的实现,您可以在这里https://github.com/Puzer/stylegan-encoder