LoginSignup
18
10

More than 5 years have passed since last update.

FastPhotoStyleは簡単、便利、綺麗

Posted at

FastPhotoStyle

deep-photo-styletransferはMatlabなどが必要でセットアップがかなり面倒で、かつ、手順が複雑でしたが、nvidiaが公開したライブラリのFastPhotoStyleはpythonのみで完結し、nvidia謹製の機能群のみの依存ですむので、Matlab依存よりはマシで簡単でした

この実装系の優れたところは輪郭や、絵柄などを変化させず、色やコントラストを大きく変化させることに限定されているので、実際のアート領域と相性が良さそうな点があります(すごい)

幾つかの例を載せつつ、簡単に実行してみたいと思います。

公式サイトのUsageを見ながらセットアップしてみてください。ハマリポイントは注意点に書きました

nvidiaからのフォークの変更点

  • 不完全ながらpython3対応(nvidiaのpythonのモジュールの対応待ち)
  • 自動で様々な組み合わせを試すスクリプトを追加

注意点

  • nvidiaのcuda 9.0以降であること
  • python2のライブラリでないとうまく動作しないことがある
  • gcc, g++のversionが2018/2時点で5までの対応となっている
  • cudaのtensorflowなどで普段参照しないcudaのlibを参照するのでLD_LIBRARY_PATHを確認する必要があるかもしれない

adhocな対応で動かす

$ sudo rm /usr/bin/g++
$ sudo ln -s /usr/bin/g++-5 /usr/bin/g++
$ sudo rm /usr/bin/gcc
$ sudo ln -s /usr/bin/gcc-5 /usr/bin/gcc

datasourceを集めたら総当りで、FastPhotoStyleを適応する

このようなスクリプトを書くと早い

import os

from pathlib import Path

import random

import shutil
names = [name for name in Path('./kaga/nvme0n1/pixabay-scraper/imgs').glob('*')]

PWD = os.environ['PWD']
for i in range(5000):
  style, content = [str(x) for x in random.sample(names, 2)]
  print(style, content)
  out = f'{PWD}/images/{i:09d}_output.jpg'
  to_style = f'{PWD}/images/{i:09d}_style.jpg'
  to_content = f'{PWD}/images/{i:09d}_content.jpg'
  shutil.copy(style, to_style)
  shutil.copy(content, to_content)
  os.system(f'python ../demo.py --content_image_path={content} --style_image_path={style} --output_image_path={out}')  
  print(to_style)

Examples

PyTorchとcudaだけでStyle Transferができるので便利ですね

余談

約一年前に、DeepPhotoStyleTransferの方を苦労しながらやった感じでして、きれいな絵同士を使うとうまくいことがわかっています  

ただ、面倒な環境依存が多く、運用プロセスまで載せたくなかったのでサーベイで止まっていましたがnvidia版は良さそうですね。

一年前の奮闘日記です
https://github.com/GINK03/gink03.github.io/blob/master/_posts/posts/2017-04-07-deep-photo-style-transfer.md

フォークしたレポジトリ

今回、上記の例を作成するためにフォークしたレポジトリです

License

Copyright (C) 2018 NVIDIA Corporation. All rights reserved.
Licensed under the CC BY-NC-SA 4.0 license (https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode).

About

This code repository contains an implementation of our fast photorealistic style transfer algorithm. Given a content photo and a style photo, the code can transfer the style of the style photo to the content photo. The details of the algorithm behind the code is documented in our arxiv paper. Please cite the paper if this code repository is used in your publications.

Yijun Li, Ming-Yu Liu, Xueting Li, Ming-Hsuan Yang, Jan Kautz "A Closed-form Solution to Photorealistic Image Stylization" arXiv preprint arXiv:1802.06474

18
10
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
18
10