Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For easy setup #12

Open
wants to merge 15 commits into
base: chainer-1.20
Choose a base branch
from
96 changes: 96 additions & 0 deletions README-ja.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# chainer-gogh

## Implementation of "A neural algorithm of Artistic style" (http://arxiv.org/abs/1508.06576)
## 解説記事: https://research.preferred.jp/2015/09/chainer-gogh/

<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/cat.png" height="150px">


<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/style_0.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/im0.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/style_1.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/im1.png" height="150px">

<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/style_2.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/im2.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/style_3.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/im3.png" height="150px">

<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/style_4.jpg" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/im4.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/style_5.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/im5.png" height="150px">

<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/style_6.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/im6.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/style_7.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/im7.png" height="150px">

(VGG, lam=0.0075, after 5000 iteration)

## Usage:
### Chainerをインストール
```
pip install -r requirements.txt
```
詳しくはhttps://github.com/pfnet/chainer

### モデルをダウンロード
* NIN https://gist.github.com/mavenlin/d802a5849de39225bcc6

お手軽。(`-m nin`)
* VGG https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-md

きれいな絵がかけるがとても重い。(`-m vgg`, `-m vgg_chainer`)
vgg_chainerではモデルのダウンロードの必要はなく、初回を除いて非常に高速でロードできるようになります(chainer 1.19以降で動作)。

* GoogLeNet https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet

NIN並に軽く、ポテンシャルもあるはずだが、最適なパラメタがわかっていない。(`-m googlenet`)

* illustration2vec http://illustration2vec.net/ (pre-trained model for tag prediction, version 2.0)

VGGより軽く、二次元画像にとても強いはずだが、最適なパラメタがわかってない。(`-m i2v`)

### CPU実行
```
python chainer-gogh.py -m nin -i input.png -s style.png -o output_dir -g -1
```

### GPU実行
```
python chainer-gogh.py -m nin -i input.png -s style.png -o output_dir -g GPU番号
```

### VGG実行サンプル
```
python chainer-gogh.py -m vgg_chainer -i input.png -s style.png -o output_dir -g 0 --width 256
```

### モデルの指定方法
```
-m nin
```
のninを、vgg, vgg_chainer, googlenet, i2vに切り替えることが可能。
モデルファイルはディレクトリ直下に置いて、デフォルトの名前のまま変えないこと。

### 複数枚同時生成
* まず、input.txtというファイル名で、以下の様なファイルを作る。
```
input0.png style0.png
input1.png style1.png
...
```
そして、chainer-gogh-multi.pyの方を実行
```
python chainer-gogh-multi.py -i input.txt
```
VGGを使うときはGPUのメモリ不足に注意

## パラメタについて
* `--lr`: 学習速度。生成の進捗が遅い時は大きめにする
* `--lam`: これを上げるとinput画像に近くなり、下げるとstyle画像に近くなる
* alpha, beta: 各層から伝播させる誤差にかかる係数。models.pyの中でハードコードされている。

## 注意
* 現在のところ画像は正方形に近いほうがいいです
61 changes: 31 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# chainer-gogh

## Implementation of "A neural algorithm of Artistic style" (http://arxiv.org/abs/1508.06576)
## 解説記事: https://research.preferred.jp/2015/09/chainer-gogh/
Implementation of "A neural algorithm of Artistic style" (http://arxiv.org/abs/1508.06576) in Chainer. The Japanese readme can be found [here](README-ja.md).
## Accompanying article: https://research.preferred.jp/2015/09/chainer-gogh/

<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/cat.png" height="150px">

Expand All @@ -26,70 +25,72 @@
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/style_7.png" height="150px">
<img src="https://raw.githubusercontent.com/mattya/chainer-gogh/master/sample_images/im7.png" height="150px">

(VGG, lam=0.0075, after 5000 iteration)
(VGG, lam=0.0075, after 5000 iterations)

## Usage:
### Chainerをインストール
### Install Chainer
```
pip install chainer
pip install -r requirements.txt
```
詳しくはhttps://github.com/pfnet/chainer
See https://github.com/pfnet/chainer for details.

### モデルをダウンロード
### Download the model(s)
There are multiple models to chose from:
* NIN https://gist.github.com/mavenlin/d802a5849de39225bcc6

お手軽。(`-m nin`)
Simply specify: (`-m nin`)
* VGG https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-md

きれいな絵がかけるがとても重い。(`-m vgg`)
With VGG, it takes a long time to make good looking images. (`-m vgg`, `-m vgg_chainer`)

After downloading and using the vgg_chainer model for the first time, all subsequent uses will load the model very fast.(functionality available in chainer 1.19 and above).

* GoogLeNet https://github.com/BVLC/caffe/tree/master/models/bvlc_googlenet

NIN並に軽く、ポテンシャルもあるはずだが、最適なパラメタがわかっていない。(`-m googlenet`)
About the same as NIN, but there should be potential for good images. The optimum parameters are unknown. (`-m googlenet`)

* illustration2vec http://illustration2vec.net/ (pre-trained model for tag prediction, version 2.0)

VGGより軽く、二次元画像にとても強いはずだが、最適なパラメタがわかってない。(`-m i2v`)
Lightweight compared to VGG, should be good for illustrations/anime drawings. Optimal parameters are unknown. (`-m i2v`)

### CPU実行
### Run on CPU
```
python chainer-gogh.py -m nin -i input.png -s style.png -o output_dir -g -1
```

### GPU実行
### Run on GPU
```
python chainer-gogh.py -m nin -i input.png -s style.png -o output_dir -g GPU番号
python chainer-gogh.py -m nin -i input.png -s style.png -o output_dir -g <GPU number>
```

### VGG実行サンプル
### Stylize an image with VGG
```
python chainer-gogh.py -m vgg -i input.png -s style.png -o output_dir -g 0 --width 256
python chainer-gogh.py -m vgg_chainer -i input.png -s style.png -o output_dir -g 0 --width 256
```

### モデルの指定方法
### How to specify the model
```
-m nin
```
のninを、vgg, googlenet, i2vに切り替えることが可能。
モデルファイルはディレクトリ直下に置いて、デフォルトの名前のまま変えないこと。
It is possible to change from nin to vgg, vgg_chainer, googlenet or i2v. To do this, put the model file in the working directory, keeping the default file name.

### 複数枚同時生成
* まず、input.txtというファイル名で、以下の様なファイルを作る。
### Generate multiple images simultaneously
* First, create a file called input.txt and list the input and output file names:
```
input0.png style0.png
input1.png style1.png
...
```
そして、chainer-gogh-multi.pyの方を実行
then, run chainer-gogh-multi.py:
```
python chainer-gogh-multi.py -i input.txt
```
VGGを使うときはGPUのメモリ不足に注意
The VGG model uses a lot of GPU memory, be careful!

## パラメタについて
* `--lr`: 学習速度。生成の進捗が遅い時は大きめにする
* `--lam`: これを上げるとinput画像に近くなり、下げるとstyle画像に近くなる
* alpha, beta: 各層から伝播させる誤差にかかる係数。models.pyの中でハードコードされている。
## About the parameters
* `--lr`: learning rate. Increase this when the generation progress is slow.
* `--lam`: increase to make the output image similar to the input, decrease to add more style.
* alpha, beta: coefficients relating to the error propagated from each layer. They are hard coded for each model.

## 注意
* 現在のところ画像は正方形に近いほうがいいです
## Advice
* At the moment, using square images (e.g. 256x256) is best.
2 changes: 1 addition & 1 deletion chainer-gogh-multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def generate_image(img_orig, img_style, width, nw, nh, max_iter, lr, img_gen=Non
img_gen = xp.random.uniform(-20,20,(batch_size,3,width,width),dtype=np.float32)
img_gen[:,:,:,:] = img_gen_
else:
img_gen = np.random.uniform(-20,20,(batch_size,3,width,width)).astype(np.float32)
img_gen_ = np.random.uniform(-20,20,(3,width,width)).astype(np.float32)
img_gen = np.random.uniform(-20,20,(batch_size,3,width,width)).astype(np.float32)
img_gen[:,:,:,:] = img_gen_
x = Variable(img_gen)
Expand Down
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
chainer==1.20.0
filelock==3.0.4
nose==1.3.7
numpy==1.15.0
Pillow==5.2.0
protobuf==3.6.1
six==1.11.0