python - 神经网络 StyleGAN 风格混合麻烦

标签 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()


最佳答案

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

关于python - 神经网络 StyleGAN 风格混合麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54961015/

相关文章:

python - IF 语句不比较从文件接收的值

python - flask mysql 不为空

python - PyAutoGUI 在屏幕上定位,在 linux 上出现错误

python - 为什么代码检测到最外层)

python - 使用 Python 操作(非常)长的数据文件

python - 如何在Python中获得不同的组合?

python - 在 argparse 中有一个带有破折号的选项

python - mailutils 无法通过 python 中的 subprocess.run 工作

python - tkinter 返回动态复选框的名称

python - 使用 numpy/pandas 从两列创建一个联合命中数矩阵