天天看點

styleGAN環境搭建 、 動漫模型效果測評

  • ???? 聲明: 作為全網 AI 領域 幹貨最多的部落客之一,❤️ 不負光陰不負卿 ❤️
風格遷移 系列創作如下
  • ​​StyleMapGAN、有趣的風格遷移——評測【一】 ​​
  • ​​StyleMapGAN之celeba_hq 風格遷移 - 圖像編輯、實驗測評【二】 ​​

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​​

項目目錄結構如下:

styleGAN環境搭建 、 動漫模型效果測評

模型下載下傳(百度網盤):

已訓練好的動漫頭像模型(動漫頭像為主,512x512)
連結: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      

生成的動漫女生頭像如下:

???????? 墨理學AI

  • ???? 作為全網 AI 領域 幹貨最多的部落客之一,❤️ 不負光陰不負卿 ❤️
  • ❤️ 如果文章對你有幫助、點贊、評論鼓勵部落客的每一分認真創作
  • ???? 目标檢測、超分重建、圖像修複、資料集整理 四大專欄持續更新、敬請關注

繼續閱讀