轻轻松松使用StyleGAN(一):创建令人惊讶的黄种人脸和专属于自己的老婆动漫头像

NVIDIA(英伟达)开源了StyleGAN,用它可以生成令人惊讶的逼真人脸;也可以像某些人所说的,生成专属于自己的老婆动漫头像。这些生成的人脸或者动漫头像都是此前这个世界上从来没有过的,完全是被“伟大的你”所创造出来的。

StyleGAN的源码可以到这里下载:

https://github.com/NVlabs/stylegan

有好事者把StyleGAN的论文翻译成中文,虽然阅读起来也并不是很容易懂,但还是比看英文论文要容易多了:

https://blog.csdn.net/a312863063/article/details/88761977

那么问题来了,怎样试一试StyleGAN的神奇魔力呢?

步骤如下:

(1)拥有或安装NVIDIA(英伟达)的显卡,比如:我的HP笔记本的操作系统是Windows 10,带有NVIDIA GeForce GTX 1060显卡,虽然配置并不高,也一样可以把StyleGAN跑起来;

(2)安装cuda 、cudnn、tensorflow-gpu 、 pillow等模块,建议使用Anaconda来安装,具体过程可以参考:

https://blog.csdn.net/weixin_41943311/article/details/91866987

https://blog.csdn.net/weixin_41943311/article/details/93747924

扫描二维码关注公众号,回复: 9333295 查看本文章

(3)下载StyleGAN源码:

下载完成后,把ZIP包解压缩,放到自己的工作目录下。

(4)下载模型文件:

有好事者已经把文件放到了百度网盘上,这样下载起来比较方便,

(4.1)已训练好的人脸模型(各个人种都有,但黄种人脸偏少,1024x1024):karras2019stylegan-ffhq-1024x1024.pkl

百度网盘:https://pan.baidu.com/s/1ujItgpnHSw14Fw8I3Ai7Jw

提取码:ossw 

(4.2)已训练好的动漫头像模型(日本动漫少女为主,512x512):2019-03-08-stylegan-animefaces-network-02051-021980.pkl

百度网盘:https://pan.baidu.com/s/1KmauV9eEho9v6nINAdRmKA

提取码:dnbd

(4.3)一位中国研究生,从FFHQ人脸数据集中筛选了一些黄种人脸进行训练,生成了黄种人的人脸模型(1024x1204):generator_yellow.pkl

百度网盘:https://pan.baidu.com/s/18cpaM6wJg4ozmwlFNY21kw

提取码:fx23

(5)修改生成人脸或少女动漫头像的文件

(5.1)进入stylegan-master目录(如:F:\AI\stylegan-master),创建cache目录,把人脸模型和动漫头像模型复制到cache目录下;

(5.2)修改pretrained_example.py文件,或者按下面的内容创建自己的pretrained_example001.py文件:

# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# This work is licensed under the Creative Commons Attribution-NonCommercial
# 4.0 International License. To view a copy of this license, visit
# http://creativecommons.org/licenses/by-nc/4.0/ or send a letter to
# Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.

"""Minimal script for generating an image using pre-trained StyleGAN generator."""

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

PREFIX = 'Person'
#PREFIX = 'Animation'

SEED = random.randint(0,18000)

def main():
    # Initialize TensorFlow.
    tflib.init_tf()

    # Load pre-trained network.
    Model = './cache/karras2019stylegan-ffhq-1024x1024.pkl'
    #Model = './cache/generator_yellow.pkl'
    #Model = './cache/2019-03-08-stylegan-animefaces-network-02051-021980.pkl'
    
    model_file = glob.glob(Model)
    if len(model_file) == 1:
        model_file = open(model_file[0], "rb")
    else:
        raise Exception('Failed to find the model')

    _G, _D, Gs = pickle.load(model_file)
    # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run.
    # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run.
    # Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot.

    # Print network details.
    Gs.print_layers()

    # Pick latent vector.
    rnd = np.random.RandomState(SEED)
    latents = rnd.randn(1, Gs.input_shape[1])

    # Generate image.
    fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt)

    # Save image.
    os.makedirs(config.result_dir, exist_ok=True)
    save_name = PREFIX + '_' + str(random.getrandbits(64)) + '.png'
    save_path = os.path.join(config.result_dir, save_name)

    PIL.Image.fromarray(images[0], 'RGB').save(save_path)

if __name__ == "__main__":
    main()

上面的Python文件是用来生成人脸的,如果需要生成动漫头像,只需要把

Model = './cache/karras2019stylegan-ffhq-1024x1024.pkl'

或:Model = './cache/generator_yellow.pkl'

修改为:

Model = './cache/2019-03-08-stylegan-animefaces-network-02051-021980.pkl'

即可。

(5.3)在stylegan-master目录下运行程序:python  pretrained_example001.py

(5.4)到stylegan-master/results目录下查看生成的图像,That's all.

(5.5)也可以批量生成人脸或动漫头像,按下面的内容创建自己的pretrained_example002.py文件:

# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# This work is licensed under the Creative Commons Attribution-NonCommercial
# 4.0 International License. To view a copy of this license, visit
# http://creativecommons.org/licenses/by-nc/4.0/ or send a letter to
# Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.

"""Minimal script for generating an image using pre-trained StyleGAN generator."""

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

PREFIX = 'Person'
#PREFIX = 'Animation'

TIMES_LOOP = 100

def main():
    # Initialize TensorFlow.
    tflib.init_tf()

    # Load pre-trained network.
    Model = './cache/generator_yellow.pkl'
    #Model = './cache/karras2019stylegan-ffhq-1024x1024.pkl'
    #Model = './cache/2019-03-08-stylegan-animefaces-network-02051-021980.pkl'

    model_file = glob.glob(Model)
    if len(model_file) == 1:
        model_file = open(model_file[0], "rb")
    else:
        raise Exception('Failed to find the model')

    _G, _D, Gs = pickle.load(model_file)
    # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run.
    # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run.
    # Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot.

    # Print network details.
    Gs.print_layers()

    for i in range(TIMES_LOOP):
        # Pick latent vector.
        SEED = random.randint(0, 18000)
        rnd = np.random.RandomState(SEED)
        latents = rnd.randn(1, Gs.input_shape[1])

         # Generate image.
        fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
        images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt)

         # Generate and Save image.
        os.makedirs(config.result_dir, exist_ok=True)
        save_name = PREFIX + '_' + str(random.getrandbits(64)) + '.png'
        save_path = os.path.join(config.result_dir, save_name)
        PIL.Image.fromarray(images[0], 'RGB').save(save_path)

if __name__ == "__main__":
    main()

然后,在stylegan-master目录下运行程序:python  pretrained_example002.py

生成的黄种人头像:

 生成的动漫少女头像:

对StyleGAN生成的人脸和动漫头像,你有什么评价?

参考:

https://www.gongyesheji.org/?p=963

http://www.seeprettyface.com/

(完)

后续文章:

轻轻松松使用StyleGAN(二):源代码初探+中文注释,generate_figure.py

轻轻松松使用StyleGAN(三):基于ResNet50构造StyleGAN的逆向网络,从目标图像提取特征码

轻轻松松使用StyleGAN(四):对StyleGAN的逆向网络的训练过程进行优化

轻轻松松使用StyleGAN(五):提取真实人脸特征码的一些探索

轻轻松松使用StyleGAN(六):StyleGAN Encoder找到真实人脸对应的特征码,核心源代码+中文注释

轻轻松松使用StyleGAN(七):用StyleGAN Encoder为女朋友制作美丽头像

发布了32 篇原创文章 · 获赞 75 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_41943311/article/details/100539707
今日推荐