Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add readme, model download script, simple generate script
- Loading branch information
1 parent
6ece522
commit 161fb43
Showing
5 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Chainer implementation of "Perceptual Losses for Real-Time Style Transfer and Super-Resolution" | ||
|
||
### Note: | ||
This repository is currently in progress. | ||
|
||
## Requirement | ||
- [Chainer](https://github.com/pfnet/chainer) | ||
``` | ||
$ pip install chainer | ||
``` | ||
|
||
## Prerequisite | ||
Download VGG16 model and convert it into smaller file so that we use only the convolution layers which are 10% of entire model. | ||
``` | ||
sh setup_model.sh | ||
``` | ||
|
||
## Train | ||
Need to train one image transformation network model per one style target. | ||
According to the paper, the models are trained on the [Microsoft COCO dataset](http://mscoco.org/dataset/#download). | ||
``` | ||
python train.py -s <style_image_path> -d <training_dataset_path> -g 0 | ||
``` | ||
|
||
## Generate your own image | ||
``` | ||
python generate.py <input_image_path> -m <model_path> -o <output_image_path> | ||
``` | ||
|
||
## Reference | ||
- [Perceptual Losses for Real-Time Style Transfer and Super-Resolution](arxiv.org/abs/1603.08155) | ||
- [chainer-gogh](https://github.com/mattya/chainer-gogh.git) chainer implementation of neural-style. I heavily referenced it, super helpful. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import numpy as np | ||
import argparse | ||
from PIL import Image | ||
import time | ||
|
||
import chainer | ||
from chainer import cuda, Variable, serializers | ||
from net import * | ||
|
||
parser = argparse.ArgumentParser(description='Real-time style transfer image generator') | ||
parser.add_argument('input') | ||
parser.add_argument('--gpu', '-g', default=-1, type=int, | ||
help='GPU ID (negative value indicates CPU)') | ||
parser.add_argument('--model', '-m', default='models/style.model', type=str) | ||
parser.add_argument('--out', '-o', default='out.jpg', type=str) | ||
args = parser.parse_args() | ||
|
||
model = FastStyleNet() | ||
serializers.load_npz(args.model, model) | ||
if args.gpu >= 0: | ||
cuda.get_device(0).use() | ||
model.to_gpu() | ||
xp = np if args.gpu < 0 else cuda.cupy | ||
|
||
image = xp.asarray(Image.open(args.input).convert('RGB'), dtype=xp.float32).transpose(2, 0, 1) | ||
image = image.reshape((1,) + image.shape) | ||
image -= 120 | ||
x = Variable(image) | ||
|
||
start = time.time() | ||
y = model(x) | ||
result = cuda.to_cpu(y.data) | ||
|
||
result = result.transpose(0, 2, 3, 1) | ||
result = result.reshape((result.shape[1:])) | ||
result += 120 | ||
result = np.uint8(result) | ||
print time.time() - start, 'sec' | ||
|
||
Image.fromarray(result).save(args.out) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
if [ ! -f VGG_ILSVRC_16_layers.caffemodel ]; then | ||
wget http://www.robots.ox.ac.uk/~vgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel | ||
fi | ||
|
||
python create_chainer_model.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters