LoginSignup
7
12

More than 3 years have passed since last update.

【StyleGAN2入門】10個のアニメ顔で独自学習♬

Posted at

StyleGANに引き続いて、StyleGAN2でもアニメ顔の独自学習をやってみた。
結論から言うと、config-aは学習できたが、config-bからconfig-fは環境が整っていない。
とはいえ、ほぼコードは見えてきたので、一度まとめておこうと思う。
特に今回は、10個のみのアニメ顔でどこまで学習するかをやってみた。
【参考】
NVlabs/stylegan2
stylegan2で独自モデルの学習方法

やったこと

・環境
・10個のアニメ顔で学習
・出力してみる
・出力画像とオリジナル画像

・環境

StyleGAN2の環境は以下のとおり

Requirements
・Both Linux and Windows are supported. 
・Linux is recommended for performance and compatibility reasons.
・64-bit Python 3.6 installation. We recommend Anaconda3 with numpy 1.14.3 or newer.
・TensorFlow 1.14 or 1.15 with GPU support. The code does not support TensorFlow 2.0.
・On Windows, you need to use TensorFlow 1.14 — TensorFlow 1.15 will not work.
・One or more high-end NVIDIA GPUs, NVIDIA drivers, CUDA 10.0 toolkit and cuDNN 7.5. ・To reproduce the results reported in the paper, 
     you need an NVIDIA GPU with at least 16 GB of DRAM.
・Docker users: use the provided Dockerfile to build an image with the required library dependencies.

・StyleGAN2 relies on custom TensorFlow ops that are compiled on the fly using NVCC. 
To test that your NVCC installation is working correctly, run:
nvcc test_nvcc.cu -o test_nvcc -run
| CPU says hello.
| GPU says hello.
・On Windows, the compilation requires Microsoft Visual Studio to be in PATH. 
We recommend installing Visual Studio Community Edition and adding into PATH 
using "C:\Program Files (x86)\Microsoft
VisualStudio\2019\Community\VC\Auxiliary\Build\vcvars64.bat".

ところが、一応整ったと思ったところでconfig-a以外は、以下のエラーを吐いて止まってしまう。

 File "C:\Users\user\Anaconda3\envs\keras-gpu\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 185, in __init__
    self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'

・10個のアニメ顔で学習

ということで、今回はconfig-aすなわちStyleGANで学習を試みた。
まず、準備として
⓪アニメ顔を10枚選んで、前回と同様にcustom_dataset-r02.tfrecordsを作成する

コードは以下のとおりで、1060で動くように変更する。
コード全体は以下においた。
StyleGAN2/run_training.py /
変更な主な部分は、以下のとおり
①ターゲット解像度=64とした
以下のコードで読み込むとサイズ(128,128)の画像も(64,64)で読み込め、StyleGANのネットワーク構造のものとなる。

run_training.py
    dataset_args = EasyDict(tfrecord_dir=dataset, resolution=64)  #, resolution=64

②初期解像度も64とした
これはStyleGANはpGANなので学習が進むとだんだん次元が大きくなるが、今回はどうもそれがうまく動かないので、最初からターゲットな解像度の64にしてやってみたら収束しだしたのでこれを採用した

run_training.py
sched.lod_initial_resolution = 64 #8

run_training.pyのそのほかの変更は上記のコード全体を見てほしい
③これだけだとメモリーエラーを吐いて止まってしまうのでdataset.pyの以下の部分を変更します。
StyleGAN2/training/dataset.py

training/dataset.py
# Load labels.
        assert max_label_size == 'full' or max_label_size >= 0
        #self._np_labels = np.zeros([1<<30, 0], dtype=np.float32)
        self._np_labels = np.zeros([1<<20, 0], dtype=np.float32)

④最後に学習が時間かかるのとHDDのメモリーが不足気味なので毎回出力に変更
StyleGAN2/training/training_loop.py

training/training_loop.py
    image_snapshot_ticks    = 1,  #50     # How often to save image snapshots? None = only save 'reals.png' and 'fakes-init.png'.
    network_snapshot_ticks  = 1, #50      # How often to save network snapshots? None = only save 'networks-final.pkl'.
...
            if network_snapshot_ticks is not None and (cur_tick % network_snapshot_ticks == 0 or done):
                #pkl = dnnlib.make_run_dir_path('network-snapshot-%06d.pkl' % (cur_nimg // 1000))
                pkl = dnnlib.make_run_dir_path('network-snapshot-.pkl')
                misc.save_pkl((G, D, Gs), pkl)

・出力してみる

出力例のミキシングやってみる

# Example of style mixing (matches the corresponding video clip)
python run_generator.py style-mixing-example --network=./results/00007-stylegan2-custom_dataset-1gpu-config-a/network-final.pkl --row-seeds=85,100,75,458,1500 --col-seeds=55,821,1789,293 --truncation-psi=1.0

以下のとおり、学習できている。
grid.png
また、前回のStyleGANのミキシングも以下のコードで実施できた。
example_256_4705_50_52x11.gif
そして、適当に100枚出力してみると以下のように出力した。
example256_1000_100.gif
StyleGAN2/pretrained_example.py

・出力画像とオリジナル画像

最後に一番の興味はこうして生成される画像はオリジナルとは異なるのかという疑問の解釈の一助として以下の画像を貼っておく
※これらの画像はシステマティックに乱数で100枚生成したものからの抜粋である

元画像 0.png 1.png 2.png 3.png 4.png
生成画像 example0_11.png example1_14.png example2_79.png example3_13.png example4_91.png
元画像 5.png 6.png 7.png 8.png 9.png
生成画像 example5_2.png example6_1.png example7_21.png example8_78.png example9_51.png

まとめ

・10枚のアニメ顔でStyleGANの学習が出来た
・生成画像にはオリジナル画像が含まれると解釈できる

・次回は1080マシンで高解像度に挑戦したいと思う

7
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
12