Merge pull request #10 from nagadomi/fix_chroma_scale
fix ugly chroma scaling
This commit is contained in:
commit
badfcec987
8 changed files with 43 additions and 18 deletions
0
images/gen.sh
Normal file → Executable file
0
images/gen.sh
Normal file → Executable file
Binary file not shown.
Before Width: | Height: | Size: 554 KiB After Width: | Height: | Size: 650 KiB |
Binary file not shown.
Before Width: | Height: | Size: 130 KiB After Width: | Height: | Size: 147 KiB |
Binary file not shown.
Before Width: | Height: | Size: 135 KiB After Width: | Height: | Size: 150 KiB |
|
@ -26,7 +26,8 @@ local function reconstruct_layer(model, x, block_size, offset)
|
||||||
end
|
end
|
||||||
return new_x
|
return new_x
|
||||||
end
|
end
|
||||||
local function reconstruct(model, x, offset, block_size)
|
local reconstruct = {}
|
||||||
|
function reconstruct.image(model, x, offset, block_size)
|
||||||
block_size = block_size or 128
|
block_size = block_size or 128
|
||||||
local output_size = block_size - offset * 2
|
local output_size = block_size - offset * 2
|
||||||
local h_blocks = math.floor(x:size(2) / output_size) +
|
local h_blocks = math.floor(x:size(2) / output_size) +
|
||||||
|
@ -54,5 +55,37 @@ local function reconstruct(model, x, offset, block_size)
|
||||||
|
|
||||||
return output
|
return output
|
||||||
end
|
end
|
||||||
|
function reconstruct.scale(model, scale, x, offset, block_size)
|
||||||
|
block_size = block_size or 128
|
||||||
|
local x_jinc = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Jinc")
|
||||||
|
x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Box")
|
||||||
|
|
||||||
|
local output_size = block_size - offset * 2
|
||||||
|
local h_blocks = math.floor(x:size(2) / output_size) +
|
||||||
|
((x:size(2) % output_size == 0 and 0) or 1)
|
||||||
|
local w_blocks = math.floor(x:size(3) / output_size) +
|
||||||
|
((x:size(3) % output_size == 0 and 0) or 1)
|
||||||
|
|
||||||
|
local h = offset + h_blocks * output_size + offset
|
||||||
|
local w = offset + w_blocks * output_size + offset
|
||||||
|
local pad_h1 = offset
|
||||||
|
local pad_w1 = offset
|
||||||
|
local pad_h2 = (h - offset) - x:size(2)
|
||||||
|
local pad_w2 = (w - offset) - x:size(3)
|
||||||
|
local yuv_nn = image.rgb2yuv(iproc.padding(x, pad_w1, pad_w2, pad_h1, pad_h2))
|
||||||
|
local yuv_jinc = image.rgb2yuv(iproc.padding(x_jinc, pad_w1, pad_w2, pad_h1, pad_h2))
|
||||||
|
local y = reconstruct_layer(model, yuv_nn[1], block_size, offset)
|
||||||
|
y[torch.lt(y, 0)] = 0
|
||||||
|
y[torch.gt(y, 1)] = 1
|
||||||
|
yuv_jinc[1]:copy(y)
|
||||||
|
local output = image.yuv2rgb(image.crop(yuv_jinc,
|
||||||
|
pad_w1, pad_h1,
|
||||||
|
yuv_jinc:size(3) - pad_w2, yuv_jinc:size(2) - pad_h2))
|
||||||
|
output[torch.lt(output, 0)] = 0
|
||||||
|
output[torch.gt(output, 1)] = 1
|
||||||
|
collectgarbage()
|
||||||
|
|
||||||
|
return output
|
||||||
|
end
|
||||||
|
|
||||||
return reconstruct
|
return reconstruct
|
||||||
|
|
|
@ -13,15 +13,11 @@ local pairwise_transform = require './lib/pairwise_transform'
|
||||||
local image_loader = require './lib/image_loader'
|
local image_loader = require './lib/image_loader'
|
||||||
|
|
||||||
local function save_test_scale(model, rgb, file)
|
local function save_test_scale(model, rgb, file)
|
||||||
local input = iproc.scale(rgb,
|
local up = reconstruct.scale(model, settings.scale, rgb, settings.block_offset)
|
||||||
rgb:size(3) * settings.scale,
|
|
||||||
rgb:size(2) * settings.scale)
|
|
||||||
local up = reconstruct(model, input, settings.block_offset)
|
|
||||||
|
|
||||||
image.save(file, up)
|
image.save(file, up)
|
||||||
end
|
end
|
||||||
local function save_test_jpeg(model, rgb, file)
|
local function save_test_jpeg(model, rgb, file)
|
||||||
local im, count = reconstruct(model, rgb, settings.block_offset)
|
local im, count = reconstruct.image(model, rgb, settings.block_offset)
|
||||||
image.save(file, im)
|
image.save(file, im)
|
||||||
end
|
end
|
||||||
local function split_data(x, test_size)
|
local function split_data(x, test_size)
|
||||||
|
|
10
waifu2x.lua
10
waifu2x.lua
|
@ -37,12 +37,11 @@ local function waifu2x()
|
||||||
local model = torch.load(path.join(opt.model_dir,
|
local model = torch.load(path.join(opt.model_dir,
|
||||||
("noise%d_model.t7"):format(opt.noise_level)), "ascii")
|
("noise%d_model.t7"):format(opt.noise_level)), "ascii")
|
||||||
model:evaluate()
|
model:evaluate()
|
||||||
new_x = reconstruct(model, x, BLOCK_OFFSET)
|
new_x = reconstruct.image(model, x, BLOCK_OFFSET)
|
||||||
elseif opt.m == "scale" then
|
elseif opt.m == "scale" then
|
||||||
local model = torch.load(path.join(opt.model_dir, "scale2.0x_model.t7"), "ascii")
|
local model = torch.load(path.join(opt.model_dir, "scale2.0x_model.t7"), "ascii")
|
||||||
model:evaluate()
|
model:evaluate()
|
||||||
x = iproc.scale(x, x:size(3) * 2.0, x:size(2) * 2.0)
|
new_x = reconstruct.scale(model, 2.0, x, BLOCK_OFFSET)
|
||||||
new_x = reconstruct(model, x, BLOCK_OFFSET)
|
|
||||||
elseif opt.m == "noise_scale" then
|
elseif opt.m == "noise_scale" then
|
||||||
local noise_model = torch.load(path.join(opt.model_dir,
|
local noise_model = torch.load(path.join(opt.model_dir,
|
||||||
("noise%d_model.t7"):format(opt.noise_level)), "ascii")
|
("noise%d_model.t7"):format(opt.noise_level)), "ascii")
|
||||||
|
@ -50,9 +49,8 @@ local function waifu2x()
|
||||||
|
|
||||||
noise_model:evaluate()
|
noise_model:evaluate()
|
||||||
scale_model:evaluate()
|
scale_model:evaluate()
|
||||||
x = reconstruct(noise_model, x, BLOCK_OFFSET)
|
x = reconstruct.image(noise_model, x, BLOCK_OFFSET)
|
||||||
x = iproc.scale(x, x:size(3) * 2.0, x:size(2) * 2.0)
|
new_x = reconstruct.scale(scale_model, 2.0, x, BLOCK_OFFSET)
|
||||||
new_x = reconstruct(scale_model, x, BLOCK_OFFSET)
|
|
||||||
else
|
else
|
||||||
error("undefined method:" .. opt.method)
|
error("undefined method:" .. opt.method)
|
||||||
end
|
end
|
||||||
|
|
8
web.lua
8
web.lua
|
@ -74,15 +74,13 @@ local function get_image(req)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function apply_denoise1(x)
|
local function apply_denoise1(x)
|
||||||
return reconstruct(noise1_model, x, BLOCK_OFFSET)
|
return reconstruct.image(noise1_model, x, BLOCK_OFFSET)
|
||||||
end
|
end
|
||||||
local function apply_denoise2(x)
|
local function apply_denoise2(x)
|
||||||
return reconstruct(noise2_model, x, BLOCK_OFFSET)
|
return reconstruct.image(noise2_model, x, BLOCK_OFFSET)
|
||||||
end
|
end
|
||||||
local function apply_scale2x(x)
|
local function apply_scale2x(x)
|
||||||
return reconstruct(scale20_model,
|
return reconstruct.scale(scale20_model, 2.0, x, BLOCK_OFFSET)
|
||||||
iproc.scale(x, x:size(3) * 2.0, x:size(2) * 2.0),
|
|
||||||
BLOCK_OFFSET)
|
|
||||||
end
|
end
|
||||||
local function cache_do(cache, x, func)
|
local function cache_do(cache, x, func)
|
||||||
if path.exists(cache) then
|
if path.exists(cache) then
|
||||||
|
|
Loading…
Reference in a new issue