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

举报
墨理学AI 发表于 2022/01/22 10:10:55 2022/01/22
【摘要】 动漫头像生成
  • 🎉 声明: 作为全网 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 领域八大干货专栏、诚不我欺


    📙 预祝各位 2022 前途似锦、可摘星辰

  • 🎉 作为全网 AI 领域 干货最多的博主之一,❤️ 不负光阴不负卿 ❤️
  • ❤️ 如果文章对你有帮助、点赞、评论鼓励博主的每一分认真创作
  • ❤️ 比寻找温暖更重要的是,让自己成为一盏灯火 ❤️

    9.png

    【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
    • 点赞
    • 收藏
    • 关注作者

    评论(0

    0/1000
    抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    举报
    请填写举报理由
    0/200