TensorFlow (Python API) implementation of Neural Style

Overview

neural-style-tf

This is a TensorFlow implementation of several techniques described in the papers:

Additionally, techniques are presented for semantic segmentation and multiple style transfer.

The Neural Style algorithm synthesizes a pastiche by separating and combining the content of one image with the style of another image using convolutional neural networks (CNN). Below is an example of transferring the artistic style of The Starry Night onto a photograph of an African lion:

Transferring the style of various artworks to the same content image produces qualitatively convincing results:

Here we reproduce Figure 3 from the first paper, which renders a photograph of the Neckarfront in Tübingen, Germany in the style of 5 different iconic paintings The Shipwreck of the Minotaur, The Starry Night, Composition VII, The Scream, Seated Nude:

Content / Style Tradeoff

The relative weight of the style and content can be controlled.

Here we render with an increasing style weight applied to Red Canna:

Multiple Style Images

More than one style image can be used to blend multiple artistic styles.

Top row (left to right): The Starry Night + The Scream, The Scream + Composition VII, Seated Nude + Composition VII
Bottom row (left to right): Seated Nude + The Starry Night, Oversoul + Freshness of Cold, David Bowie + Skull

Style Interpolation

When using multiple style images, the degree of blending between the images can be controlled.

Top row (left to right): content image, .2 The Starry Night + .8 The Scream, .8 The Starry Night + .2 The Scream
Bottom row (left to right): .2 Oversoul + .8 Freshness of Cold, .5 Oversoul + .5 Freshness of Cold, .8 Oversoul + .2 Freshness of Cold

Transfer style but not color

The color scheme of the original image can be preserved by including the flag --original_colors. Colors are transferred using either the YUV, YCrCb, CIE L*a*b*, or CIE L*u*v* color spaces.

Here we reproduce Figure 1 and Figure 2 in the third paper using luminance-only transfer:

Left to right: content image, stylized image, stylized image with the original colors of the content image

Textures

The algorithm is not constrained to artistic painting styles. It can also be applied to photographic textures to create pareidolic images.

Segmentation

Style can be transferred to semantic segmentations in the content image.

Multiple styles can be transferred to the foreground and background of the content image.

Left to right: content image, foreground style, background style, foreground mask, background mask, stylized image

Video

Animations can be rendered by applying the algorithm to each source frame. For the best results, the gradient descent is initialized with the previously stylized frame warped to the current frame according to the optical flow between the pair of frames. Loss functions for temporal consistency are used to penalize pixels excluding disoccluded regions and motion boundaries.


Top row (left to right): source frames, ground-truth optical flow visualized
Bottom row (left to right): disoccluded regions and motion boundaries, stylized frames

Big thanks to Mike Burakoff for finding a bug in the video rendering.

Gradient Descent Initialization

The initialization of the gradient descent is controlled using --init_img_type for single images and --init_frame_type or --first_frame_type for video frames. White noise allows an arbitrary number of distinct images to be generated. Whereas, initializing with a fixed image always converges to the same output.

Here we reproduce Figure 6 from the first paper:

Top row (left to right): Initialized with the content image, the style image, white noise (RNG seed 1)
Bottom row (left to right): Initialized with white noise (RNG seeds 2, 3, 4)

Layer Representations

The feature complexities and receptive field sizes increase down the CNN heirarchy.

Here we reproduce Figure 3 from the original paper:

1 x 10^-5 1 x 10^-4 1 x 10^-3 1 x 10^-2
conv1_1
conv2_1
conv3_1
conv4_1
conv5_1

Rows: increasing subsets of CNN layers; i.e. 'conv4_1' means using 'conv1_1', 'conv2_1', 'conv3_1', 'conv4_1'.
Columns: alpha/beta ratio of the the content and style reconstruction (see Content / Style Tradeoff).

Setup

Dependencies:

Optional (but recommended) dependencies:

After installing the dependencies:

  • Download the VGG-19 model weights (see the "VGG-VD models from the Very Deep Convolutional Networks for Large-Scale Visual Recognition project" section). More info about the VGG-19 network can be found here.
  • After downloading, copy the weights file imagenet-vgg-verydeep-19.mat to the project directory.

Usage

Basic Usage

Single Image

  1. Copy 1 content image to the default image content directory ./image_input
  2. Copy 1 or more style images to the default style directory ./styles
  3. Run the command:
bash stylize_image.sh <path_to_content_image> <path_to_style_image>

Example:

bash stylize_image.sh ./image_input/lion.jpg ./styles/kandinsky.jpg

Note: Supported image formats include: .png, .jpg, .ppm, .pgm

Note: Paths to images should not contain the ~ character to represent your home directory; you should instead use a relative path or the absolute path.

Video Frames

  1. Copy 1 content video to the default video content directory ./video_input
  2. Copy 1 or more style images to the default style directory ./styles
  3. Run the command:
bash stylize_video.sh <path_to_video> <path_to_style_image>

Example:

bash stylize_video.sh ./video_input/video.mp4 ./styles/kandinsky.jpg

Note: Supported video formats include: .mp4, .mov, .mkv

Advanced Usage

Single Image or Video Frames

  1. Copy content images to the default image content directory ./image_input or copy video frames to the default video content directory ./video_input
  2. Copy 1 or more style images to the default style directory ./styles
  3. Run the command with specific arguments:
python neural_style.py <arguments>

Example (Single Image):

python neural_style.py --content_img golden_gate.jpg \
                       --style_imgs starry-night.jpg \
                       --max_size 1000 \
                       --max_iterations 100 \
                       --original_colors \
                       --device /cpu:0 \
                       --verbose;

To use multiple style images, pass a space-separated list of the image names and image weights like this:

--style_imgs starry_night.jpg the_scream.jpg --style_imgs_weights 0.5 0.5

Example (Video Frames):

python neural_style.py --video \
                       --video_input_dir ./video_input/my_video_frames \
                       --style_imgs starry-night.jpg \
                       --content_weight 5 \
                       --style_weight 1000 \
                       --temporal_weight 1000 \
                       --start_frame 1 \
                       --end_frame 50 \
                       --max_size 1024 \
                       --first_frame_iterations 3000 \
                       --verbose;

Note: When using --init_frame_type prev_warp you must have previously computed the backward and forward optical flow between the frames. See ./video_input/make-opt-flow.sh and ./video_input/run-deepflow.sh

Arguments

  • --content_img: Filename of the content image. Example: lion.jpg
  • --content_img_dir: Relative or absolute directory path to the content image. Default: ./image_input
  • --style_imgs: Filenames of the style images. To use multiple style images, pass a space-separated list. Example: --style_imgs starry-night.jpg
  • --style_imgs_weights: The blending weights for each style image. Default: 1.0 (assumes only 1 style image)
  • --style_imgs_dir: Relative or absolute directory path to the style images. Default: ./styles
  • --init_img_type: Image used to initialize the network. Choices: content, random, style. Default: content
  • --max_size: Maximum width or height of the input images. Default: 512
  • --content_weight: Weight for the content loss function. Default: 5e0
  • --style_weight: Weight for the style loss function. Default: 1e4
  • --tv_weight: Weight for the total variational loss function. Default: 1e-3
  • --temporal_weight: Weight for the temporal loss function. Default: 2e2
  • --content_layers: Space-separated VGG-19 layer names used for the content image. Default: conv4_2
  • --style_layers: Space-separated VGG-19 layer names used for the style image. Default: relu1_1 relu2_1 relu3_1 relu4_1 relu5_1
  • --content_layer_weights: Space-separated weights of each content layer to the content loss. Default: 1.0
  • --style_layer_weights: Space-separated weights of each style layer to loss. Default: 0.2 0.2 0.2 0.2 0.2
  • --original_colors: Boolean flag indicating if the style is transferred but not the colors.
  • --color_convert_type: Color spaces (YUV, YCrCb, CIE L*u*v*, CIE L*a*b*) for luminance-matching conversion to original colors. Choices: yuv, ycrcb, luv, lab. Default: yuv
  • --style_mask: Boolean flag indicating if style is transferred to masked regions.
  • --style_mask_imgs: Filenames of the style mask images (example: face_mask.png). To use multiple style mask images, pass a space-separated list. Example: --style_mask_imgs face_mask.png face_mask_inv.png
  • --noise_ratio: Interpolation value between the content image and noise image if network is initialized with random. Default: 1.0
  • --seed: Seed for the random number generator. Default: 0
  • --model_weights: Weights and biases of the VGG-19 network. Download here. Default:imagenet-vgg-verydeep-19.mat
  • --pooling_type: Type of pooling in convolutional neural network. Choices: avg, max. Default: avg
  • --device: GPU or CPU device. GPU mode highly recommended but requires NVIDIA CUDA. Choices: /gpu:0 /cpu:0. Default: /gpu:0
  • --img_output_dir: Directory to write output to. Default: ./image_output
  • --img_name: Filename of the output image. Default: result
  • --verbose: Boolean flag indicating if statements should be printed to the console.

Optimization Arguments

  • --optimizer: Loss minimization optimizer. L-BFGS gives better results. Adam uses less memory. Choices: lbfgs, adam. Default: lbfgs
  • --learning_rate: Learning-rate parameter for the Adam optimizer. Default: 1e0

  • --max_iterations: Max number of iterations for the Adam or L-BFGS optimizer. Default: 1000
  • --print_iterations: Number of iterations between optimizer print statements. Default: 50
  • --content_loss_function: Different constants K in the content loss function. Choices: 1, 2, 3. Default: 1

Video Frame Arguments

  • --video: Boolean flag indicating if the user is creating a video.
  • --start_frame: First frame number. Default: 1
  • --end_frame: Last frame number. Default: 1
  • --first_frame_type: Image used to initialize the network during the rendering of the first frame. Choices: content, random, style. Default: random
  • --init_frame_type: Image used to initialize the network during the every rendering after the first frame. Choices: prev_warped, prev, content, random, style. Default: prev_warped
  • --video_input_dir: Relative or absolute directory path to input frames. Default: ./video_input
  • --video_output_dir: Relative or absolute directory path to write output frames to. Default: ./video_output
  • --content_frame_frmt: Format string of input frames. Default: frame_{}.png
  • --backward_optical_flow_frmt: Format string of backward optical flow files. Default: backward_{}_{}.flo
  • --forward_optical_flow_frmt: Format string of forward optical flow files. Default: forward_{}_{}.flo
  • --content_weights_frmt: Format string of optical flow consistency files. Default: reliable_{}_{}.txt
  • --prev_frame_indices: Previous frames to consider for longterm temporal consistency. Default: 1
  • --first_frame_iterations: Maximum number of optimizer iterations of the first frame. Default: 2000
  • --frame_iterations: Maximum number of optimizer iterations for each frame after the first frame. Default: 800

Questions and Errata

Send questions or issues:

Memory

By default, neural-style-tf uses the NVIDIA cuDNN GPU backend for convolutions and L-BFGS for optimization. These produce better and faster results, but can consume a lot of memory. You can reduce memory usage with the following:

  • Use Adam: Add the flag --optimizer adam to use Adam instead of L-BFGS. This should significantly reduce memory usage, but will require tuning of other parameters for good results; in particular you should experiment with different values of --learning_rate, --content_weight, --style_weight
  • Reduce image size: You can reduce the size of the generated image with the --max_size argument.

Implementation Details

All images were rendered on a machine with:

  • CPU: Intel Core i7-6800K @ 3.40GHz × 12
  • GPU: NVIDIA GeForce GTX 1080/PCIe/SSE2
  • OS: Linux Ubuntu 16.04.1 LTS 64-bit
  • CUDA: 8.0
  • python: 2.7.12
  • tensorflow: 0.10.0rc
  • opencv: 2.4.9.1

Acknowledgements

The implementation is based on the projects:

  • Torch (Lua) implementation 'neural-style' by jcjohnson
  • Torch (Lua) implementation 'artistic-videos' by manuelruder

Source video frames were obtained from:

Artistic images were created by the modern artists:

Artistic images were created by the popular historical artists:

Bash shell scripts for testing were created by my brother Sheldon Smith.

Citation

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

@misc{Smith2016,
  author = {Smith, Cameron},
  title = {neural-style-tf},
  year = {2016},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/cysmith/neural-style-tf}},
}
Comments
  • Error when trying to use GPU

    Error when trying to use GPU

    
    ---- RENDERING SINGLE IMAGE ----
    
    Traceback (most recent call last):
      File "neural_style.py", line 876, in <module>
        main()
      File "neural_style.py", line 873, in main
        else: render_single_image()
      File "neural_style.py", line 842, in render_single_image
        stylize(content_img, style_imgs, init_img)
      File "neural_style.py", line 574, in stylize
        L_style = sum_masked_style_losses(sess, net, style_imgs)
      File "neural_style.py", line 395, in sum_masked_style_losses
        sess.run(net['input'].assign(img))
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 717, in run
        run_metadata_ptr)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 915, in _run
        feed_dict_string, options, run_metadata)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 965, in _do_run
        target_list, options, run_metadata)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 985, in _do_call
        raise type(e)(node_def, op, message)
    tensorflow.python.framework.errors.InvalidArgumentError: Cannot assign a device to node 'Variable': Could not satisfy explicit device specification '/device:GPU:0' because no devices matching that specification are registered in this process; available devices: /job:localhost/replica:0/task:0/cpu:0
    Colocation Debug Info:
    Colocation group had the following types and devices:
    Identity: CPU
    Assign: CPU
    Variable: CPU
             [[Node: Variable = Variable[container="", dtype=DT_FLOAT, shape=[1,400,400,3], shared_name="", _device="/device:GPU:0"]()]]
    
    Caused by op u'Variable', defined at:
      File "neural_style.py", line 876, in <module>
        main()
      File "neural_style.py", line 873, in main
        else: render_single_image()
      File "neural_style.py", line 842, in render_single_image
        stylize(content_img, style_imgs, init_img)
      File "neural_style.py", line 570, in stylize
        net = build_vgg19(content_img)
      File "neural_style.py", line 245, in build_vgg19
        net['input']   = tf.Variable(np.zeros((1, h, w, d), dtype=np.float32))
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 215, in __init__
        dtype=dtype)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 300, in _init_from_args
        name=name)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/state_ops.py", line 146, in variable_op
        container=container, shared_name=shared_name)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_state_ops.py", line 490, in _variable
        name=name)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
        op_def=op_def)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
        original_op=self._default_original_op, op_def=op_def)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
        self._traceback = _extract_stack()
    
    InvalidArgumentError (see above for traceback): Cannot assign a device to node 'Variable': Could not satisfy explicit device specification '/device:GPU:0' because no devices matching that specification are registered in this process; available devices: /job:localhost/replica:0/task:0/cpu:0
    Colocation Debug Info:
    Colocation group had the following types and devices:
    Identity: CPU
    Assign: CPU
    Variable: CPU
             [[Node: Variable = Variable[container="", dtype=DT_FLOAT, shape=[1,400,400,3], shared_name="", _device="/device:GPU:0"]()]]
    
    ubuntu@ip-Address:~/neural-style-tf$
    
    
    opened by ProGamerGov 16
  • GPU Issue

    GPU Issue

    I have the script up and running with the CPU, but it crashes when I try to run it in GPU mode. CUDA and cuDNN are installed. Below is the error:

    `Traceback (most recent call last): File "neural_style.py", line 872, in main() File "neural_style.py", line 869, in main else: render_single_image() File "neural_style.py", line 838, in render_single_image stylize(content_img, style_imgs, init_img) File "neural_style.py", line 572, in stylize L_style = sum_style_losses(sess, net, style_imgs) File "neural_style.py", line 410, in sum_style_losses sess.run(net['input'].assign(img)) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run run_metadata_ptr) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _run feed_dict_string, options, run_metadata) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1015, in _do_run target_list, options, run_metadata) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1035, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device to node 'Variable': Could not satisfy explicit device specification '/device:GPU:0' because no devices matching that specification are registered in this process; available devices: /job:localhost/replica:0/task:0/cpu:0 Colocation Debug Info: Colocation group had the following types and devices: Assign: CPU Identity: CPU VariableV2: CPU [[Node: Variable = VariableV2container="", dtype=DT_FLOAT, shape=[1,512,512,3], shared_name="", _device="/device:GPU:0"]]

    Caused by op u'Variable', defined at: File "neural_style.py", line 872, in main() File "neural_style.py", line 869, in main else: render_single_image() File "neural_style.py", line 838, in render_single_image stylize(content_img, style_imgs, init_img) File "neural_style.py", line 566, in stylize net = build_model(content_img) File "neural_style.py", line 243, in build_model net['input'] = tf.Variable(np.zeros((1, h, w, d), dtype=np.float32)) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 197, in init expected_shape=expected_shape) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 293, in _init_from_args name=name) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/ops/state_ops.py", line 128, in variable_op_v2 shared_name=shared_name) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_state_ops.py", line 708, in _variable_v2 name=name) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op op_def=op_def) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op original_op=self._default_original_op, op_def=op_def) File "/home/simsies/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1226, in init self._traceback = _extract_stack()

    InvalidArgumentError (see above for traceback): Cannot assign a device to node 'Variable': Could not satisfy explicit device specification '/device:GPU:0' because no devices matching that specification are registered in this process; available devices: /job:localhost/replica:0/task:0/cpu:0 Colocation Debug Info: Colocation group had the following types and devices: Assign: CPU Identity: CPU VariableV2: CPU [[Node: Variable = VariableV2container="", dtype=DT_FLOAT, shape=[1,512,512,3], shared_name="", _device="/device:GPU:0"]]

    `

    opened by jcpsimmons 7
  • About Quick Neural style

    About Quick Neural style

    I have learned many things from your code! but I felt a little slowly when run your net in GPU gtx 1080(about 10 mins for one picture). I heard that there are some network that can implement The fast Neural Style and get the nearly result。Can you introduce some new and great github code for me ??I wanna learn more about the Neural Style!~ Really Thanks for you coding!~~

    opened by dovanchan 7
  • ValueError: Shape (1, 512, 512, 64) must have rank 1

    ValueError: Shape (1, 512, 512, 64) must have rank 1

    Hi, I got an error when running the example : "bash stylize_image.sh ./image_input/lion.jpg ./styles/starry-night.jpg" error: Traceback (most recent call last): File "neural_style.py", line 839, in main() File "neural_style.py", line 836, in main else: render_single_image() File "neural_style.py", line 805, in render_single_image stylize(content_img, style_imgs, init_img) File "neural_style.py", line 556, in stylize L_style = sum_style_losses(sess, net, style_imgs) File "neural_style.py", line 407, in sum_style_losses style_loss += style_layer_loss(a, x) * weight File "neural_style.py", line 355, in style_layer_loss A = gram_matrix(a, M, N) File "neural_style.py", line 361, in gram_matrix F = tf.reshape(x[0], (area, depth)) File "/home/dyc/tensorflow-0.10.0rc0/_python_build/tensorflow/python/ops/array_ops.py", line 338, in _SliceHelper sliced = slice(tensor, indices, sizes) File "/home/dyc/tensorflow-0.10.0rc0/_python_build/tensorflow/python/ops/array_ops.py", line 388, in slice return gen_array_ops.slice(input, begin, size, name=name) File "/home/dyc/tensorflow-0.10.0rc0/_python_build/tensorflow/python/ops/gen_array_ops.py", line 2001, in _slice name=name) File "/home/dyc/tensorflow-0.10.0rc0/_python_build/tensorflow/python/framework/op_def_library.py", line 703, in apply_op op_def=op_def) File "/home/dyc/tensorflow-0.10.0rc0/_python_build/tensorflow/python/framework/ops.py", line 2312, in create_op set_shapes_for_outputs(ret) File "/home/dyc/tensorflow-0.10.0rc0/_python_build/tensorflow/python/framework/ops.py", line 1704, in set_shapes_for_outputs shapes = shape_func(op) File "/home/dyc/tensorflow-0.10.0rc0/_python_build/tensorflow/python/ops/array_ops.py", line 1510, in _SliceShape input_shape.assert_has_rank(ndims) File "/home/dyc/tensorflow-0.10.0rc0/_python_build/tensorflow/python/framework/tensor_shape.py", line 621, in assert_has_rank raise ValueError("Shape %s must have rank %d" % (self, rank)) ValueError: Shape (1, 512, 512, 64) must have rank 1

    environment:cuda 8.0rc ,tensorflow 0.10rc ,python 2.7 something wrong with these versions? looking forward to your help! thanks

    opened by xinghedyc 7
  • TypeError: Input 'filter' of 'Conv2D' Op has type string that does not match type float32 of argument 'input'.

    TypeError: Input 'filter' of 'Conv2D' Op has type string that does not match type float32 of argument 'input'.

    I met a problem in running stylize_image.sh

    alex@alex-ubuntu1604:~/myprivate/github/neural-style-tf$ bash stylize_image.sh ./image_input/lion.jpg ./styles/kandinsky.jpg
    
    Did you install the required dependencies? [y/n] 
     > y
    
    Do you have a CUDA enabled GPU? [y/n] 
     > y
    Rendering stylized image. This may take a while...
    I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcublas.so locally
    I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcudnn.so locally
    I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcufft.so locally
    I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcuda.so.1 locally
    I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcurand.so locally
    
    ---- RENDERING SINGLE IMAGE ----
    
    I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties: 
    name: GeForce GTX 1080
    major: 6 minor: 1 memoryClockRate (GHz) 1.797
    pciBusID 0000:01:00.0
    Total memory: 7.92GiB
    Free memory: 7.40GiB
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0 
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0:   Y 
    I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0)
    
    BUILDING VGG-19 NETWORK
    loading model weights...
    constructing layers...
    LAYER GROUP 1
    Traceback (most recent call last):
      File "neural_style.py", line 876, in <module>
        main()
      File "neural_style.py", line 873, in main
        else: render_single_image()
      File "neural_style.py", line 842, in render_single_image
        stylize(content_img, style_imgs, init_img)
      File "neural_style.py", line 570, in stylize
        net = build_vgg19(content_img)
      File "neural_style.py", line 248, in build_vgg19
        net['conv1_1'] = conv_layer('conv1_1', net['input'], W=get_weights(vgg_layers, 0))
      File "neural_style.py", line 313, in conv_layer
        conv = tf.nn.conv2d(layer_input, W, strides=[1, 1, 1, 1], padding='SAME')
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 396, in conv2d
        data_format=data_format, name=name)
      File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 521, in apply_op
        inferred_from[input_arg.type_attr]))
    TypeError: Input 'filter' of 'Conv2D' Op has type string that does not match type float32 of argument 'input'.
    alex@alex-ubuntu1604:~/myprivate/github/neural-style-tf$ 
    
    
    opened by dgutyanghs 5
  • Attribute Error: 'NoneType' object has no attribute 'astype'

    Attribute Error: 'NoneType' object has no attribute 'astype'

    Hello,

    I think I have all the dependencies in place, I can launch python (version 2.7) and successfully

    import tensorflow as tf
    import numpy as np
    import scipy.io
    import argparse
    import struct
    import time
    import cv2
    import os
    

    However, when I try to launch the bash script, I get the following error:

    Rendering stylized image. This may take a while...
    Traceback (most recent call last):
      File "neural_style.py", line 839, in <module>
        main()
      File "neural_style.py", line 836, in main
        else: render_single_image()
      File "neural_style.py", line 799, in render_single_image
        content_img = get_content_image(args.content_img)
      File "neural_style.py", line 704, in get_content_image
        img = cv2.imread(path, cv2.IMREAD_COLOR).astype('float')
    AttributeError: 'NoneType' object has no attribute 'astype'
    

    Any ideas what could be going on?

    opened by neilpanchal 5
  • OpenCV function remap in Tensorflow

    OpenCV function remap in Tensorflow

    Hi, @cysmith

    I found you used the OpenCV function 'remap' to warp the current frame to the last frame. I just want to use the Tensorflow function for backpropagation. Do you know whether it have the similar function in Tensorflow or not?

    Thank you very much.

    opened by jackieyung 4
  • Script/params to recreate sample images?

    Script/params to recreate sample images?

    After installing and trying the basic image stylization example, as follows: bash stylize_image.sh ./image_input/lion.jpg ./styles/starry-night.jpg

    I obtained an output image that is "roughly" as expected, but seems less faithful to the style (e.g. less-accurate looking brushstrokes in the lion's mane), and with more artifacts (checkerboard-looking patches). Here was my output:

    result

    Question- Is this output what you expect from running the example command above?

    Suggestion- May I suggest you provide example parameters for users to recreate the output examples you display (whether some or all).

    This would help very much in validating that we have installed and executed the code correctly.

    Cheers

    opened by gmccaig 4
  • AttributeError: 'module' object has no attribute 'total_variation'

    AttributeError: 'module' object has no attribute 'total_variation'

    Attempting to run on floydhub. Uploaded the vgg weights as a dataset file on floydhub.

    Using the floyd-cli, ran in my terminal: floyd run --gpu --env tensorflow-0.12:py2 --data datasets/vggforstyle/1:vgg "python neural_style.py --content_img golden_gate.jpg --style_imgs starry-night.jpg --original_colors --model_weights /vgg/imagenet-vgg-verydeep-19.mat --img_output_dir /output --verbose;"

    And get the following error in the log: 2017-09-13 13:56:16,853 INFO - Traceback (most recent call last): 2017-09-13 13:56:16,855 INFO - File "neural_style.py", line 856, in 2017-09-13 13:56:16,856 INFO - main() 2017-09-13 13:56:16,856 INFO - File "neural_style.py", line 853, in main 2017-09-13 13:56:16,856 INFO - else: render_single_image() 2017-09-13 13:56:16,856 INFO - File "neural_style.py", line 822, in render_single_image 2017-09-13 13:56:16,856 INFO - stylize(content_img, style_imgs, init_img) 2017-09-13 13:56:16,856 INFO - File "neural_style.py", line 562, in stylize 2017-09-13 13:56:16,856 INFO - L_tv = tf.image.total_variation(net['input']) 2017-09-13 13:56:16,857 INFO - AttributeError: 'module' object has no attribute 'total_variation'

    Any suggestions?

    Thanks!

    opened by gregpaulos 3
  • core dump

    core dump

    Not sure why but I keep getting a core dump when running a basic style test:

    ~/neural-style-tf$ bash stylize_image.sh ../in/t1/dst.jpg ../in/t1/src.jpg
    
    Did you install the required dependencies? [y/n]
     > y
    
    Do you have a CUDA enabled GPU? [y/n]
     > y
    Rendering stylized image. This may take a while...
    I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcublas.so locally
    I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcudnn.so locally
    I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcufft.so locally
    I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcuda.so.1 locally
    I tensorflow/stream_executor/dso_loader.cc:108] successfully opened CUDA library libcurand.so locally
    stylize_image.sh: line 44: 21017 Segmentation fault      (core dumped) python neural_style.py --content_img "${content_filename}" --content_img_dir "${content_dir}" --style_imgs "${style_filename}" --style_imgs_dir "${style_dir}" --device "${device}" --verbose
    

    ;-( Could it be the version of Pythin I am running? Is there a specific python version required?

    opened by bmaltais 3
  • Content / Style Tradeoff. What are the weight values?

    Content / Style Tradeoff. What are the weight values?

    With respect to the content/ style tradeoff, What are the weight values used in the example Red Canna + Lion shown in the Readme? I would like to replicate the results. Any changes in other parameters different than style weight?

    Someone who experimented with the code, could give me some hints about the effect of changing the tv weight, temporal weight and content loss function? I am losing too much of the content even when I am only using style weight = 10.

    opened by fastlater 2
  • I, a wintard doesn't know how to use Bash // please help

    I, a wintard doesn't know how to use Bash // please help

    I'm a bit of an idiot who swears by Windows, and this is one of the only times I've dared use Bash (via GitBash). Upon entering test txt for single image generation (bash stylize_image.sh ./image_input/lion.jpg ./styles/kandinsky.jpg), I get the following error; bash: stylize_image.sh: No such file or directory. What did I fuck up? please let me know.

    Thanks, "RatMan"

    image

    opened by LeRatman 0
  • TF2 Migration, issue with L-BFGS-B

    TF2 Migration, issue with L-BFGS-B

    I was able to get this to work with Tensorflow 2 on Windows 10 by converting a couple things for compatibility. However I'm stumped on one problem regarding the optimizer portion of the code. The Adam optimizer works fine with optimizer = tf.compat.v1.train.AdamOptimizer(args.learning_rate)

    The L-BFGS-B optimizer does not seem to work with Tensorflow 2 as it's been removed. This is the part that needs to be migrated to work with Tensorflow 2

    optimizer = tf.contrib.opt.ScipyOptimizerInterface(
        loss, method='L-BFGS-B',
        options={'maxiter': args.max_iterations,
            'disp': print_iterations})
    

    I looked into tfp.optimizer.lbfgs_minimize but I have no idea how to get it working in the existing code. Do you have any ideas? I am using Nvidia Ampere architecture cards (RTX 3090) that do not support Tensorflow 1.x due to CUDA compute version. Would really like to use L-BFGS instead of Adam for better results.

    opened by splicerlabs 1
  • Consistency checker makefile not found.

    Consistency checker makefile not found.

    melon.mp4 is my filename when I run this:

    !python neural_style.py --video \
                --video_input_dir ./video_input/"melon.mp4" \
                --style_imgs starry-night.jpg \
                --content_weight 5 \
                --style_weight 1000 \
                --temporal_weight 1000 \
                --start_frame 1 \
                --end_frame 50 \
                --max_size 1024 \
                --first_frame_iterations 3000 \
                --verbose;
    

    error occurs:

    FileNotFoundError: [Errno 2] No such file: './video_input/melon.mp4/frame_0001.ppm'

    then I run !bash ./video_input/make-opt-flow.sh ./video_input/melon.mp4

    error occurs again

    Consistency checker makefile not found.

    How to address it please?

    opened by BingoGreen 2
  • Performance issue in neural_style.py (by P3)

    Performance issue in neural_style.py (by P3)

    Hello! I've found a performance issue in neural_style.py: with tf.Session() as sess(here) is defined in the function stylize(here) which is repeatedly called in the loop for frame in range(args.start_frame, args.end_frame+1)(here).

    tf.Session being defined repeatedly could lead to incremental overhead. If you define tf.Session out of the loop and pass tf.Session as a parameter to the loop, your program would be much more efficient. Here is the Stack Overflow post to support it.

    Looking forward to your reply. Btw, I am very glad to create a PR to fix it if you are too busy.

    opened by DLPerf 1
  • Issues running on Windows 10:

    Issues running on Windows 10:

    Hi. I am struggling to make this repo work somewhere outside Google Colab and asking for advice. Since my os of choice (mainly) is Windows, I would like to have an option to run the code on it.

    I've created a Conda env with tf 1.15, but still, can't figure out why the simplest command: python neural_style.py --content_img lion.jpg --style_imgs kandinsky.jpg

    Never gets past the following line: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll

    I am running on RTX A6000 and this should be a pretty fast job for it, but somehow I can't get to even start running the simple transfer. I also never get any error.

    Any advice on debugging and setting up the correct environment are much appreciated. Thank you

    opened by dokluch 1
Owner
Cameron
Cameron
Only a Matter of Style: Age Transformation Using a Style-Based Regression Model

Only a Matter of Style: Age Transformation Using a Style-Based Regression Model The task of age transformation illustrates the change of an individual

null 444 Dec 30, 2022
Simple Tensorflow implementation of "Adaptive Convolutions for Structure-Aware Style Transfer" (CVPR 2021)

AdaConv — Simple TensorFlow Implementation [Paper] : Adaptive Convolutions for Structure-Aware Style Transfer (CVPR 2021) Note This repository does no

Junho Kim 26 Nov 18, 2022
Fast Style Transfer in TensorFlow

Fast Style Transfer in TensorFlow Add styles from famous paintings to any photo in a fraction of a second! You can even style videos! It takes 100ms o

Jefferson 5 Oct 24, 2021
TensorFlow CNN for fast style transfer

Fast Style Transfer in TensorFlow Add styles from famous paintings to any photo in a fraction of a second! It takes 100ms on a 2015 Titan X to style t

null 1 Dec 14, 2021
PyTorch implementation of neural style transfer algorithm

neural-style-pt This is a PyTorch implementation of the paper A Neural Algorithm of Artistic Style by Leon A. Gatys, Alexander S. Ecker, and Matthias

null 770 Jan 2, 2023
Official PyTorch implementation of "ArtFlow: Unbiased Image Style Transfer via Reversible Neural Flows"

ArtFlow Official PyTorch implementation of the paper: ArtFlow: Unbiased Image Style Transfer via Reversible Neural Flows Jie An*, Siyu Huang*, Yibing

null 123 Dec 27, 2022
PyTorch implementation of neural style randomization for data augmentation

README Augment training images for deep neural networks by randomizing their visual style, as described in our paper: https://arxiv.org/abs/1809.05375

null 84 Nov 23, 2022
An implementation of the paper "A Neural Algorithm of Artistic Style"

A Neural Algorithm of Artistic Style implementation - Neural Style Transfer This is an implementation of the research paper "A Neural Algorithm of Art

Srijarko Roy 27 Sep 20, 2022
PyTorch implementation of paper: AdaAttN: Revisit Attention Mechanism in Arbitrary Neural Style Transfer, ICCV 2021.

AdaAttN: Revisit Attention Mechanism in Arbitrary Neural Style Transfer [Paper] [PyTorch Implementation] [Paddle Implementation] Overview This reposit

null 148 Dec 30, 2022
pytorch implementation of fast-neural-style

fast-neural-style ?? ?? NOTICE: This codebase is no longer maintained, please use the codebase from pytorch examples repository available at pytorch/e

Abhishek Kadian 405 Dec 15, 2022
Implementation of Neural Style Transfer in Pytorch

PytorchNeuralStyleTransfer Code to run Neural Style Transfer from our paper Image Style Transfer Using Convolutional Neural Networks. Also includes co

Leon Gatys 396 Dec 1, 2022
This repository contains notebook implementations of the following Neural Process variants: Conditional Neural Processes (CNPs), Neural Processes (NPs), Attentive Neural Processes (ANPs).

The Neural Process Family This repository contains notebook implementations of the following Neural Process variants: Conditional Neural Processes (CN

DeepMind 892 Dec 28, 2022
Neural style transfer in PyTorch.

style-transfer-pytorch An implementation of neural style transfer (A Neural Algorithm of Artistic Style) in PyTorch, supporting CPUs and Nvidia GPUs.

Katherine Crowson 395 Jan 6, 2023
Towards Ultra-Resolution Neural Style Transfer via Thumbnail Instance Normalization

Towards Ultra-Resolution Neural Style Transfer via Thumbnail Instance Normalization Official PyTorch implementation for our URST (Ultra-Resolution Sty

czczup 148 Dec 27, 2022
STYLER: Style Factor Modeling with Rapidity and Robustness via Speech Decomposition for Expressive and Controllable Neural Text to Speech

STYLER: Style Factor Modeling with Rapidity and Robustness via Speech Decomposition for Expressive and Controllable Neural Text to Speech Keon Lee, Ky

Keon Lee 114 Dec 12, 2022
Neural style transfer as a class in PyTorch

pt-styletransfer Neural style transfer as a class in PyTorch Based on: https://github.com/alexis-jacq/Pytorch-Tutorials Adds: StyleTransferNet as a cl

Tyler Kvochick 31 Jun 27, 2022
CVPR '21: In the light of feature distributions: Moment matching for Neural Style Transfer

In the light of feature distributions: Moment matching for Neural Style Transfer (CVPR 2021) This repository provides code to recreate results present

Nikolai Kalischek 49 Oct 13, 2022
A "gym" style toolkit for building lightweight Neural Architecture Search systems

A "gym" style toolkit for building lightweight Neural Architecture Search systems

Jack Turner 12 Nov 5, 2022