styleGAN环境搭建 、 动漫模型效果测评

591 阅读3分钟

「这是我参与2022首次更文挑战的第5天,活动详情查看:2022首次更文挑战

  • 🎉 声明: 作为全网 AI 领域 干货最多的博主之一,❤️ 不负光阴不负卿 ❤️
  • 风格迁移 系列创作如下


    styleGAN环境搭建 | 动漫模型效果测试流程简记如下:


    环境搭建:


    • 服务器:ubuntu1~18.04 Quadro RTX 5000 16G (做测试11G的卡可以胜任)
    • CUDA版本 V10.0.130
    conda create -n tf15 python=3.6.6
    
    conda activate tf15
    
    pip install tensorflow-gpu==1.15 (1.15 测试时会有一些 WARNING,不影响测试)
    或者 
    pip install tensorflow-gpu==1.13
    
    pip install pillow
    
    pip install requests
    

    动漫模型效果测试开启:


    所使用代码: GitHub styleGAN 官方实现

    styleGAN论文: A Style-Based Generator Architecture for Generative Adversarial Networks

    项目目录结构如下:

    0

    模型下载(百度网盘):

    已训练好的动漫头像模型(动漫头像为主,512x512)

    # 该模型来自 https://blog.csdn.net/weixin_41943311/article/details/100539707 这位博主的发布
    
    链接:https://pan.baidu.com/s/1_N2y1F4BpwsufNK6xOqaig 
    提取码:2022
    

    新建 pretrained_example-dongman.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
    
    import os
    os.environ["CUDA_VISIBLE_DEVICES"] = "3"
    
    PREFIX = 'Anime'
    #PREFIX = 'Animation'
     
    TIMES_LOOP = 100
     
    def main():
        # Initialize TensorFlow.
        tflib.init_tf()
     
        # Load pre-trained network.
    
        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()
    

    测试运行命令如下:

    python pretrained_example-dongman.py 
    

    注意事项:

    一定要设置使用的卡,不然它默认会使用所有卡,把其他卡的程序干掉了(难受):
    import os
    os.environ["CUDA_VISIBLE_DEVICES"] = "1"
    
    单卡 GPU占用
    8271MiB
    

    生成的动漫女生头像如下: 1


    🚀🚀目前掘金平台已经创作的经典博文如下🚀🚀


    计算机视觉领域、经典博文

    AI 学习、深度学习环境搭建


    🚀🚀 墨理学AI


  • 🎉 作为全网 AI 领域 干货最多的博主之一,❤️ 不负光阴不负卿 ❤️
  • ❤️ 如果文章对你有帮助、点赞、评论鼓励博主的每一分认真创作
    • 🍊 目标检测、超分重建、图像修复、数据集整理 四大专栏持续更新、敬请关注

    8.png