Code Monkey home page Code Monkey logo

stylenet's Introduction

Stylenet

This is a Tensorflow project of neural network with the style synthesis algorithm modified from 2 major methods Gram Matrix and Markov Random Fields. By given a content and style image, the style and pattern can be synthesised into the content. This project also support region mapping. We have added several modifications in the Markov Random Fields cost functions. See belows for the detail.

The visual network is make use of the Tensorflow VGG19 network (Original Caffe implementation is in here and here).

Here are some sample result generated by this algorithm.

Basic synthesis

Content Style Result
>See the intermediate results in [this video](https://youtu.be/4ssJyLivbBM)
Synthesis with region mapping
Content Style
Content Region Map Style Region Map Result


##Modification of Algorithm There are 2 modifications of the algorithm from the original Markov Random Field in the paper.

  • We added the blur filtering in the convariant matrix before calculate the max argument in order to reduce the different of the convariant score in a local neighbouthood. This can make the result image look more natural. (Yellow part in the diagram)

  • The second modification is to replace the cost function of the piecwise square difference between all patches and the slices of the image. We replaced the function to a single averaged patch tensor in order greatly improve the training process. (Red part in the diagram)



##Requirement



##Basic Usage

stylenet_patch.render_gen( <content image path> , <style image path>, height=<output height>)

See the smaple main function in stylenet_patch for more detail.

stylenet's People

Contributors

machrisaa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stylenet's Issues

Replicating with random_init

Hi,
Thank you for the great work.

I've been trying to replicate the results of this paper "https://arxiv.org/pdf/1508.06576.pdf" using your implementation, by setting "random_init=true, gram_ratio=0.5, content_ratio=0.5". However, all I get is noise.

Can you give any insight that clarifies the issue I'm having?

Thank you again.
Regards,

Where is ./train/husky_paint-husky_real-style_map_3_full.npy

patches map finished 0 3.54528403282
finished joining patches 5.4882171154
Traceback (most recent call last):
File "stylenet_patch.py", line 342, in
"./images/husky_paint_region.jpg", "./test_data/husky_real_region.jpg", height=500)
File "stylenet_patch.py", line 330, in render_gen
output_file=output_file)
File "stylenet_patch.py", line 141, in render
blur_mapping=blur_mapping)
File "/Users/hcai/work/ai/stylenet/stylenet_core.py", line 240, in get_style_cost_patch2
np.save(full_patch_file, full_patch_out)
File "/Library/Python/2.7/site-packages/numpy/lib/npyio.py", line 477, in save
fid = open(file, "wb")
IOError: [Errno 2] No such file or directory: './train/husky_paint-husky_real-style_map_3_full.npy'

Cannot create a tensor proto whose content is larger than 2GB

Looks like the following line of code in stylenet_core.py is causing an exception when using a large height value
filters = tf.constant(filters_out, tf.float32)

Is there any fix for this? I looked around on google for a bit and found something about using feed_dict but how to do this was not very clear to me.

Memory issue

Hey, so I started using your code and I have encountered quite high memory usage. I have GPU-enabled Tensorflow, CUDA works well and it stays around 2.5GB in GPU memory for the default settings. My issue is with RAM - it keeps rising up to around 9GB. Is this normal behaviour or is there something wrong with my Tensorflow installation? Do you have maybe some tips for other settings I might use in your code?

Thanks

A question about the MRF implementation in the code

I am trying to understand the MRF paper and after I read your code, one part confuses me a little bit, which is get_style_cost_patch2() in stylenet_core.py. I am wondering if you could help me out?

From my understanding, function get_style_cost_patch2() is trying to calculate MRF cost, defined as Eq. (2) in the paper. And within it, calculating Eq.(3) is done by this line of code:
max_arg_out, patches = _create_patch(sess, content_input, style_input, content_regions, style_regions, blur_mapping)
It seems that this line of code finds the maximized matching patches between the style image and the content image. However in the paper, Eq (3) aims to find the maximized matching patches between the style image and the synthesized image.

I am wondering if I miss something from your implementation, or you modify the algorithm on purpose?

I also tried to modify this part back according to the paper and write a simply modified function get_style_cost_patch(). However TF reports an error saying Attempting to use uninitialized value Variable. I am wondering if you have some insight about why the error happens? Thanks,
My modified code is below:

def get_style_cost_patch(sess, var_input, style_input, save_file,
                          content_regions=None, style_regions=None,
                          load_saved_mapping=True, blur_mapping=False):
    dim = var_input.get_shape().as_list()
    h, w, d = dim[1], dim[2], dim[3]
    ph, pw = h - 2, w - 2
    pn = ph * pw
    full_patch_file = save_file + "_full.npy"

    full_patch_out = None
    if load_saved_mapping:
        try:
            full_patch_out = np.load(full_patch_file)
        except:
            print("saved full patch not found")
    if full_patch_out is None:
        with tf.device("/cpu:0"):
            max_arg_out, patches = _create_patch(sess, var_input, style_input,
                                                 content_regions, style_regions, blur_mapping)
            assert patches.get_shape().as_list() == [3, 3, d, pn]

            max_arg_out = np.reshape(max_arg_out, [ph, pw])
            # print max_arg_out
            full_patch = _join_patches_np(sess, patches, max_arg_out)

            assert full_patch.get_shape() == var_input.get_shape()
            full_patch_out = sess.run(full_patch)
            np.save(full_patch_file, full_patch_out)

    full_patch = tf.constant(full_patch_out)

    cost = l2_norm_cost(var_input - full_patch)

    return cost

detailed description about stylenet

this work is amazing! But is there any arxiv paper for this stylenet? i want to know if there are some more details about the two modifications. Thanks.

The link for Tensorflow is broken

Hi,@machrisaa

Thank you for the released code.The link for Tensorflow in the "#Requirement" in readme is broken.And Ido not Know where the "tensoflow_vgg" in the file "custom_vgg19.py" is .How can I get it?

Looking forward to receiving your early reply.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.