Python 神经网络型混合故障
神经网络通过预先训练的文件从GitHub加载,并成功生成随机照片。这似乎是随机的。但是当您运行脚本generate_figures.py时,会显示另外两个的混合森林的照片,也是随机的。 问题。如何制作一个神经网络来混合两张用户定义的照片而不是生成的照片? 我对代码进行了更改,指定了照片的路径,但最终,它生成了与我的照片面无关的所有相同的随机图像Python 神经网络型混合故障,python,Python,神经网络通过预先训练的文件从GitHub加载,并成功生成随机照片。这似乎是随机的。但是当您运行脚本generate_figures.py时,会显示另外两个的混合森林的照片,也是随机的。 问题。如何制作一个神经网络来混合两张用户定义的照片而不是生成的照片? 我对代码进行了更改,指定了照片的路径,但最终,它生成了与我的照片面无关的所有相同的随机图像 import os import pickle import numpy as np import PIL.Image import dnnlib im
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()