Skip to content

Instantly share code, notes, and snippets.

@htoyryla
Last active June 18, 2018 19:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save htoyryla/147f641f2203ad01b040f4b568e98260 to your computer and use it in GitHub Desktop.
Save htoyryla/147f641f2203ad01b040f4b568e98260 to your computer and use it in GitHub Desktop.
Use original colors in an image generated by fast-neural-style
-- this program takes an original image, such as a photo,
-- and a generated image, such as generated by jcjohnson/fast-neural-style
-- and copies the original colors to the generated image
-- like when using the original_colors param in jcjohnson/neural-style
--
-- by hannu töyrylä @htoyryla 30 oct 2016
--
require 'torch'
require 'image'
local cmd = torch.CmdLine()
cmd:option('-generated_image', '',
'Image to copy colors to')
cmd:option('-original_image', '',
'Image to copy colors from')
cmd:option('-output_image', 'out.png')
local function main(params)
local generated_image = image.load(params.generated_image, 3)
local oimg = image.load(params.original_image, 3)
local original_image = image.scale(oimg, generated_image:size(3), generated_image:size(2))
disp = original_colors(original_image, generated_image)
image.save(params.output_image, disp)
end
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
local params = cmd:parse(arg)
main(params)
@tkoham
Copy link

tkoham commented Dec 2, 2016

how might one go about adding GPU acceleration to this script, if such a thing is feasible?

@crypdick
Copy link

crypdick commented Jan 5, 2017

I love the results you posted with this. You mentioned you could potentially get better results by tuning the parameters for blending channels (jcjohnson/fast-neural-style#58 (comment)). I'm not fluent in LUA-- where do I finetune? Would you recommend transferring more than just the Y channel?

@htoyryla
Copy link
Author

htoyryla commented Jan 19, 2017

@tkoham, what this script does is so simple that I don't see much benefit adding gpu acceleration. Taking slices of two tensors and concatenating them. No actual computation at all, just copying.

@htoyryla
Copy link
Author

Maybe I should experiment a bit with blending the channels. I'll keep this in mind.

@acenter2507
Copy link

Do you have this code in python?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment