Skip to content

Commit dc10cd8

Browse files
abhiskksoumith
authored andcommitted
fast-neural-style example (pytorch#129)
* Add fast-neural-style implementation * Rename directory * Add option for stylizing using GPU * Use state_dict for saving and loading model * Update vgg-model download link * Add script for downloading models, remove saved-models folder * Use pytorch's pretrained vgg * Remove cloning of intermediate outputs * Add pytorch vgg results, update README.md * Update README.md * Change default learning rate * Update README.md * Add content scaling in stylize, edit docstring * Refactor code * Use inbuilt Instance-Normalization, refactor code * Fix typo in README.md * Update README.md * Update models, photos, README.md * Refactor * Change affine and momentum parameters for InstanceNorm * Change mode back to training, refactor transformer_net After checkpointing the model remained in evaluation mode and hence no updates were made, add code to put the model back in training mode after checkpointing. Also use Volatile variable when stylizing images during testing * Refactor * Update stylized images * Update candy style image
1 parent 5c41070 commit dc10cd8

File tree

17 files changed

+467
-0
lines changed

17 files changed

+467
-0
lines changed

fast_neural_style/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# fast-neural-style :city_sunrise: :rocket:
2+
This repository contains a pytorch implementation of an algorithm for artistic style transfer. The algorithm can be used to mix the content of an image with the style of another image. For example, here is a photograph of a door arch rendered in the style of a stained glass painting.
3+
4+
The model uses the method described in [Perceptual Losses for Real-Time Style Transfer and Super-Resolution](https://arxiv.org/abs/1603.08155) along with [Instance Normalization](https://arxiv.org/pdf/1607.08022.pdf). The saved-models for examples shown in the README can be downloaded from [here](https://www.dropbox.com/s/lrvwfehqdcxoza8/saved_models.zip?dl=0).
5+
6+
<p align="center">
7+
<img src="images/style-images/mosaic.jpg" height="200px">
8+
<img src="images/content-images/amber.jpg" height="200px">
9+
<img src="images/output-images/amber-mosaic.jpg" height="440px">
10+
</p>
11+
12+
## Requirements
13+
The program is written in Python, and uses [pytorch](http://pytorch.org/), [scipy](https://www.scipy.org). A GPU is not necessary, but can provide a significant speed up especially for training a new model. Regular sized images can be styled on a laptop or desktop using saved models.
14+
15+
## Usage
16+
Stylize image
17+
```
18+
python neural_style/neural_style.py eval --content-image </path/to/content/image> --model </path/to/saved/model> --output-image </path/to/output/image> --cuda 0
19+
```
20+
* `--content-image`: path to content image you want to stylize.
21+
* `--model`: saved model to be used for stylizing the image (eg: `mosaic.pth`)
22+
* `--output-image`: path for saving the output image.
23+
* `--content-scale`: factor for scaling down the content image if memory is an issue (eg: value of 2 will halve the height and width of content-image)
24+
* `--cuda`: set it to 1 for running on GPU, 0 for CPU.
25+
26+
Train model
27+
```bash
28+
python neural_style/neural_style.py train --dataset </path/to/train-dataset> --style-image </path/to/style/image> --save-model-dir </path/to/save-model/folder> --epochs 2 --cuda 1
29+
```
30+
31+
There are several command line arguments, the important ones are listed below
32+
* `--dataset`: path to training dataset, the path should point to a folder containing another folder with all the training images. I used COCO 2014 Training images dataset [80K/13GB] [(download)](http://mscoco.org/dataset/#download).
33+
* `--style-image`: path to style-image.
34+
* `--save-model-dir`: path to folder where trained model will be saved.
35+
* `--cuda`: set it to 1 for running on GPU, 0 for CPU.
36+
37+
Refer to ``neural_style/neural_style.py`` for other command line arguments. For training new models you might have to tune the values of `--content-weight` and `--style-weight`. The mosaic style model shown above was trained with `--content-weight 1e5` and `--style-weight 1e10`. The remaining 3 models were also trained with similar order of weight parameters with slight variation in the `--style-weight` (`5e10` or `1e11`).
38+
39+
## Models
40+
41+
Models for the examples shown below can be downloaded from [here](https://www.dropbox.com/s/lrvwfehqdcxoza8/saved_models.zip?dl=0) or by running the script ``download_saved_models.sh``.
42+
43+
<div align='center'>
44+
<img src='images/content-images/amber.jpg' height="174px">
45+
</div>
46+
47+
<div align='center'>
48+
<img src='images/style-images/mosaic.jpg' height="174px">
49+
<img src='images/output-images/amber-mosaic.jpg' height="174px">
50+
<img src='images/output-images/amber-candy.jpg' height="174px">
51+
<img src='images/style-images/candy.jpg' height="174px">
52+
<br>
53+
<img src='images/style-images/rain-princess-cropped.jpg' height="174px">
54+
<img src='images/output-images/amber-rain-princess.jpg' height="174px">
55+
<img src='images/output-images/amber-udnie.jpg' height="174px">
56+
<img src='images/style-images/udnie.jpg' height="174px">
57+
</div>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
wget https://www.dropbox.com/s/lrvwfehqdcxoza8/saved_models.zip?dl=1
2+
unzip saved_models.zip?dl=1
106 KB
Loading
319 KB
Loading
364 KB
Loading
299 KB
Loading
160 KB
Loading
367 KB
Loading
75.6 KB
Loading
137 KB
Loading

0 commit comments

Comments
 (0)