jcjohnson / neural-style

Torch implementation of neural style algorithm
MIT License
18.31k stars 2.7k forks source link

Is there any way just adding the style color without using style #316

Open markzhong88 opened 8 years ago

markzhong88 commented 8 years ago

I know there is a parameter to keep the original content image color. I wonder is there any parameter I can only transfer the color from style image, without changing too much the content image style?

jcjohnson commented 8 years ago

There is no existing parameter for this but there is an easy thing you can try.

To transfer style but not color, we run normal style transfer and convert both the original image and stylized image to the YUV colorspace; the "style but not color" output is then produced by combining the Y channel from the stylized image with the UV channels of the original image.

To transfer color but not style, you could try doing something similar, combining the Y channel from the original image and the UV channels from the stylized image; it would look similar to this function.

markzhong88 commented 8 years ago

@jcjohnson Thank you for the reply. I tried to change "content_image" into "style_image" in the following function parameter, which I think I am keep the style color. But I failed to execute the code:


  -- Maybe perform postprocessing for color-independent style transfer
  if params.original_colors == 1 then
    disp = original_colors(content_image, disp)
  end

error output:

/home/mark/torch/install/share/lua/5.1/image/init.lua:1545: in function 'rgb2yuv' neural_style.lua:391: in function 'original_colors' neural_style.lua:310: in function 'maybe_save' neural_style.lua:335: in function 'opfunc' /home/mark/torch/install/share/lua/5.1/optim/lbfgs.lua:211: in function 'lbfgs' neural_style.lua:345: in function 'main' neural_style.lua:515: in main chunk [C]: in function 'dofile' ...mark/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk


Also I tried to swap the content & generated parameter here and still got the original color:


function original_colors(content, generated) local generated_y = image.rgb2yuv(generated)[{{1, 1}}] local content_uv = image.rgb2yuv(content)[{{2, 3}}] return image.yuv2rgb(torch.cat(generated_y, content_uv, 1)) end


Please advise

htoyryla commented 8 years ago

To transfer the color, the two images must have the same dimensions. So to transfer the colors from the style image, we need to make a copy of the style image resized to dimensions of the generated image. Otherwise the torch cat operation will throw an error.

As to your second question, I am not sure how you swapped them and for what purpose.