環境
二つのWindows環境で動作確認しました。
Windows10 Pro 64bit (GPUなし) Python 3.6.8
Windows10 Pro 64bit NVIDIA GeForce GTX1080 CUDA9.2 cudnn7.2.1 Python3.6.8
Pytorchの導入
今回は古いPytorchをpipで導入する
- GPUなし
pip install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-win_amd64.whl pip install torchvision==0.2.1
- GPUあり
pip install http://download.pytorch.org/whl/cu92/torch-0.4.1-cp36-cp36m-win_amd64.whl pip install torchvision==0.2.1
コードとモデルのダウンロード
上記GitHubページからすべてのコードをZIPファイルでダウンロードして解凍する。
モデルのダウンロードはLinuxが使える場合と使えない場合で方法が変わります。
- Linuxが使用可能な場合
sh pretrained_model/download_pth.sh
- Linuxが使用できない場合
PowerShellで以下の4行をそれぞれ1行ずつ実行して4つのモデルをダウンロードする。
ダウンロードしたものは「pretrained_model」フォルダに入れる。
wget "http://vllab1.ucmerced.edu/~yli62/CartoonGAN/pytorch_pth/Hayao_net_G_float.pth" -OutFile "Hayao_net_G_float.pth" wget "http://vllab1.ucmerced.edu/~yli62/CartoonGAN/pytorch_pth/Hosoda_net_G_float.pth" -OutFile "Hosoda_net_G_float.pth" wget "http://vllab1.ucmerced.edu/~yli62/CartoonGAN/pytorch_pth/Paprika_net_G_float.pth" -OutFile "Paprika_net_G_float.pth" wget "http://vllab1.ucmerced.edu/~yli62/CartoonGAN/pytorch_pth/Shinkai_net_G_float.pth" -OutFile "Shinkai_net_G_float.pth"
「test.py」の書き換え
60~63行目を以下のように書き換える
- 修正前
if opt.gpu > -1: input_image = Variable(input_image, volatile=True).cuda() else: input_image = Variable(input_image, volatile=True).float()
- 修正後
if opt.gpu > -1: with torch.no_grad(): input_image = Variable(input_image).cuda() else: with torch.no_grad(): input_image = Variable(input_image).float()
実行
my_imgフォルダを作成して画像を入れて、いよいよ実行。
styleは「Hayao」「Hosoda」「Paprika」「Shinkai」から選択する。
- GPUなし
python test.py --input_dir my_img --style Hosoda --gpu -1
- GPUあり
python test.py --input_dir my_img --style Hosoda --gpu 0
結果
- もと画像
- Hosoda
- Hayao
- Paprika
- Shinkai
警告
- 今回は無視した。
UserWarning: nn.functional.tanh is deprecated. Use torch.tanh instead.
2021年10月27日追記:動画バージョン
動画バージョンの記事を書きました。
touch-sp.hatenablog.com