Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Torch implementation of neural style algorithm

License

NotificationsYou must be signed in to change notification settings

Azureyjt/neural-style

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is a torch implementation of the paperA Neural Algorithm of Artistic Styleby Leon A. Gatys, Alexander S. Ecker, and Matthias Bethge.

The paper presents an algorithm for combining the content of one image with the style of another image usingconvolutional neural networks. Here's an example that maps the artistic style ofThe Starry Nightonto a night-time photograph of the Stanford campus:

Applying the style of different images to the same content image gives interesting results.Here we reproduce Figure 2 from the paper, which renders a photograph of the Tubingen in Germany in avariety of styles:

Here are the results of applying the style of various pieces of artwork to this photograph of thegolden gate bridge:

Content / Style Tradeoff

The algorithm allows the user to trade-off the relative weight of the style and content reconstruction terms,as shown in this example where we port the style ofPicasso's 1907 self-portrait onto Brad Pitt:

Style Scale

By resizing the style image before extracting style features, we can control the types of artisticfeatures that are transfered from the style image; you can control this behavior with the-style_scale flag.Below we see three examples of rendering the Golden Gate Bridge in the style of The Starry Night.From left to right,-style_scale is 2.0, 1.0, and 0.5.

<img src="https://raw.githubusercontent.com/jcjohnson/neural-style/master/examples/outputs/golden_gate_starry_scale2.png" height=175px"><img src="https://raw.githubusercontent.com/jcjohnson/neural-style/master/examples/outputs/golden_gate_starry_scale1.png" height=175px"><img src="https://raw.githubusercontent.com/jcjohnson/neural-style/master/examples/outputs/golden_gate_starry_scale05.png" height=175px">

Multiple Style Images

You can use more than one style image to blend multiple artistic styles.

Clockwise from upper left: "The Starry Night" + "The Scream", "The Scream" + "Composition VII","Seated Nude" + "Composition VII", and "Seated Nude" + "The Starry Night"

Style Interpolation

When using multiple style images, you can control the degree to which they are blended:

Transfer style but not color

If you add the flag-original_colors 1 then the output image will retain the colors of the original image;this is similar tothe recent blog post by deepart.io.

Setup:

Dependencies:

Optional dependencies:

After installing dependencies, you'll need to run the following script to download the VGG model:

sh models/download_models.sh

This will download the originalVGG-19 model.Leon Gatys has graciously provided the modified version of the VGG-19 model that was used in their paper;this will also be downloaded. By default the original VGG-19 model is used.

If you have a smaller memory GPU then using NIN Imagenet model will be better and gives slightly worse yet comparable results. You can get the details on the model fromBVLC Caffe ModelZoo and can download the files fromNIN-Imagenet Download Link

You can find detailed installation instructions for Ubuntu in theinstallation guide.

Usage

Basic usage:

th neural_style.lua -style_image <image.jpg> -content_image <image.jpg>

OpenCL usage with NIN Model (This requires you download the NIN Imagenet model files as described above):

th neural_style.lua -style_image examples/inputs/picasso_selfport1907.jpg -content_image examples/inputs/brad_pitt.jpg -output_image profile.png -model_file models/nin_imagenet_conv.caffemodel -proto_file models/train_val.prototxt -gpu 0 -backend clnn -num_iterations 1000 -seed 123 -content_layers relu0,relu3,relu7,relu12 -style_layers relu0,relu3,relu7,relu12 -content_weight 10 -style_weight 1000 -image_size 512 -optimizer adam

OpenCL NIN Model Picasso Brad Pitt

To use multiple style images, pass a comma-separated list like this:

-style_image starry_night.jpg,the_scream.jpg.

Note that paths to images should not contain the~ character to represent your home directory; you should instead use a relativepath or a full absolute path.

Options:

  • -image_size: Maximum side length (in pixels) of of the generated image. Default is 512.
  • -style_blend_weights: The weight for blending the style of multiple style images, as acomma-separated list, such as-style_blend_weights 3,7. By default all style imagesare equally weighted.
  • -gpu: Zero-indexed ID of the GPU to use; for CPU mode set-gpu to -1.

Optimization options:

  • -content_weight: How much to weight the content reconstruction term. Default is 5e0.
  • -style_weight: How much to weight the style reconstruction term. Default is 1e2.
  • -tv_weight: Weight of total-variation (TV) regularization; this helps to smooth the image.Default is 1e-3. Set to 0 to disable TV regularization.
  • -num_iterations: Default is 1000.
  • -init: Method for generating the generated image; one ofrandom orimage.Default israndom which uses a noise initialization as in the paper;imageinitializes with the content image.
  • -optimizer: The optimization algorithm to use; eitherlbfgs oradam; default islbfgs.L-BFGS tends to give better results, but uses more memory. Switching to ADAM will reduce memory usage;when using ADAM you will probably need to play with other parameters to get good results, especiallythe style weight, content weight, and learning rate; you may also want to normalize gradients whenusing ADAM.
  • -learning_rate: Learning rate to use with the ADAM optimizer. Default is 1e1.
  • -normalize_gradients: If this flag is present, style and content gradients from each layer will beL1 normalized. Idea fromandersbll/neural_artistic_style.

Output options:

  • -output_image: Name of the output image. Default isout.png.
  • -print_iter: Print progress everyprint_iter iterations. Set to 0 to disable printing.
  • -save_iter: Save the image everysave_iter iterations. Set to 0 to disable saving intermediate results.

Layer options:

  • -content_layers: Comma-separated list of layer names to use for content reconstruction.Default isrelu4_2.
  • -style_layers: Comma-separated list of layer names to use for style reconstruction.Default isrelu1_1,relu2_1,relu3_1,relu4_1,relu5_1.

Other options:

  • -style_scale: Scale at which to extract features from the style image. Default is 1.0.
  • -original_colors: If you set this to 1, then the output image will keep the colors of the content image.
  • -proto_file: Path to thedeploy.txt file for the VGG Caffe model.
  • -model_file: Path to the.caffemodel file for the VGG Caffe model.Default is the original VGG-19 model; you can also try the normalized VGG-19 model used in the paper.
  • -pooling: The type of pooling layers to use; one ofmax oravg. Default ismax.The VGG-19 models uses max pooling layers, but the paper mentions that replacing these layers with averagepooling layers can improve the results. I haven't been able to get good results using average pooling, butthe option is here.
  • -backend:nn,cudnn, orclnn. Default isnn.cudnn requirescudnn.torch and may reduce memory usage.clnn requirescltorch andclnn
  • -cudnn_autotune: When using the cuDNN backend, pass this flag to use the built-in cuDNN autotuner to selectthe best convolution algorithms for your architecture. This will make the first iteration a bit slower and cantake a bit more memory, but may significantly speed up the cuDNN backend.

Frequently Asked Questions

Problem: Generated image has saturation artifacts:

Solution: Update theimage packge to the latest version:luarocks install image

Problem: Running without a GPU gives an error message complaining aboutcutorch not found

Solution:Pass the flag-gpu -1 when running in CPU-only mode

Problem: The program runs out of memory and dies

Solution: Try reducing the image size:-image_size 256 (or lower). Note that different image sizes will likelyrequire non-default values for-style_weight and-content_weight for optimal results.If you are running on a GPU, you can also try running with-backend cudnn to reduce memory usage.

Problem: Get the following error message:

models/VGG_ILSVRC_19_layers_deploy.prototxt.cpu.lua:7: attempt to call method 'ceil' (a nil value)

Solution: Updatenn package to the latest version:luarocks install nn

Problem: Get an error message complaining aboutpaths.extname

Solution: Updatetorch.paths package to the latest version:luarocks install paths

Problem: NIN Imagenet model is not giving good results.

Solution: Make sure the correct-proto_file is selected. Also make sure the correct parameters for-content_layers and-style_layers are set. (See OpenCL usage example above.)

Problem:-backend cudnn is slower than default NN backend

Solution: Add the flag-cudnn_autotune; this will use the built-in cuDNN autotuner to select the best convolution algorithms.

Memory Usage

By default,neural-style uses thenn backend for convolutions and L-BFGS for optimization.These give good results, but can both use a lot of memory. You can reduce memory usage with the following:

  • Use cuDNN: Add the flag-backend cudnn to use the cuDNN backend. This will only work in GPU mode.
  • Use ADAM: Add the flag-optimizer adam to use ADAM instead of L-BFGS. This should significantlyreduce memory usage, but may require tuning of other parameters for good results; in particular you shouldplay with the learning rate, content weight, style weight, and also consider using gradient normalization.This should work in both CPU and GPU modes.
  • Reduce image size: If the above tricks are not enough, you can reduce the size of the generated image;pass the flag-image_size 256 to generate an image at half the default size.

With the default settings,neural-style uses about 3.5GB of GPU memory on my system;switching to ADAM and cuDNN reduces the GPU memory footprint to about 1GB.

Speed

Speed can vary a lot depending on the backend and the optimizer.Here are some times for running 500 iterations with-image_size=512 on a Maxwell Titan X with different settings:

  • -backend nn -optimizer lbfgs: 62 seconds
  • -backend nn -optimizer adam: 49 seconds
  • -backend cudnn -optimizer lbfgs: 79 seconds
  • -backend cudnn -cudnn_autotune -optimizer lbfgs: 58 seconds
  • -backend cudnn -cudnn_autotune -optimizer adam: 44 seconds
  • -backend clnn -optimizer lbfgs: 169 seconds
  • -backend clnn -optimizer adam: 106 seconds

Here are the same benchmarks on a Pascal Titan X with cuDNN 5.0 on CUDA 8.0 RC:

  • -backend nn -optimizer lbfgs: 43 seconds
  • -backend nn -optimizer adam: 36 seconds
  • -backend cudnn -optimizer lbfgs: 45 seconds
  • -backend cudnn -cudnn_autotune -optimizer lbfgs: 30 seconds
  • -backend cudnn -cudnn_autotune -optimizer adam: 22 seconds

Implementation details

Images are initialized with white noise and optimized using L-BFGS.

We perform style reconstructions using theconv1_1,conv2_1,conv3_1,conv4_1, andconv5_1 layersand content reconstructions using theconv4_2 layer. As in the paper, the five style reconstruction losses haveequal weights.

Citation

If you find this code useful for your research, please cite:

@misc{Johnson2015,  author = {Johnson, Justin},  title = {neural-style},  year = {2015},  publisher = {GitHub},  journal = {GitHub repository},  howpublished = {\url{https://github.com/jcjohnson/neural-style}},}

About

Torch implementation of neural style algorithm

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua97.8%
  • Shell2.2%

[8]ページ先頭

©2009-2025 Movatter.jp