#python
#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:UsersKurmyavochkaDesktopNNREALISMstylegan-masterresults1.png"), ((col 1) * w, 0))
for row, dst_image in enumerate(list(dst_images)):
canvas.paste(PIL.Image.open(r"C:UsersKurmyavochkaDesktopNNREALISMstylegan-masterresults2.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:
Вам нужно сгенерировать скрытое представление фотографий, которые вы хотите объединить. Чем создавать из них средний вектор. И, наконец, сгенерируйте изображение с помощью вашего скрипта. Хорошую реализацию вы можете найти здесь https://github.com/Puzer/stylegan-encoder