1
0
Fork 0
mirror of synced 2024-07-01 04:31:14 +12:00
waifu2x/lib/pairwise_transform_scale.lua
nagadomi a210090033 Convert model files; Add new pretrained model
- Add new pretrained model to ./models/upconv_7
- Move old models to ./models/vgg_7
- Use nn.LeakyReLU instead of w2nn.LeakyReLU
- Add useful attribute to .json

New JSON attribute:
The first layer has `model_config` attribute.
It contains:
  model_arch: architecture name of model. see `lib/srcnn.lua`
  scale_factor: if scale_factor > 1, model:forward() changes image resolution with scale_factor.
  channels: input/output channels. if channels == 3, model is RGB model.
  offset: pixel size that is to be removed from output.
          for example:
            (scale_factor=1, offset=7, input=100x100) => output=(100-7)x(100-7)
            (scale_factor=2, offset=12, input=100x100) => output=(100*2-12)x(100*2-12)
And each layer has `class_name` attribute.
2016-05-15 03:04:08 +09:00

87 lines
3.1 KiB
Lua

local pairwise_utils = require 'pairwise_transform_utils'
local iproc = require 'iproc'
local pairwise_transform = {}
function pairwise_transform.scale(src, scale, size, offset, n, options)
local filters = options.downsampling_filters
local unstable_region_offset = 8
local downsampling_filter = filters[torch.random(1, #filters)]
local y = pairwise_utils.preprocess(src, size, options)
assert(y:size(2) % 4 == 0 and y:size(3) % 4 == 0)
local down_scale = 1.0 / scale
local x
if options.gamma_correction then
local small = iproc.scale_with_gamma22(y, y:size(3) * down_scale,
y:size(2) * down_scale, downsampling_filter)
if options.x_upsampling then
x = iproc.scale(small, y:size(3), y:size(2), options.upsampling_filter)
else
x = small
end
else
local small = iproc.scale(y, y:size(3) * down_scale,
y:size(2) * down_scale, downsampling_filter)
if options.x_upsampling then
x = iproc.scale(small, y:size(3), y:size(2), options.upsampling_filter)
else
x = small
end
end
if options.x_upsampling then
x = iproc.crop(x, unstable_region_offset, unstable_region_offset,
x:size(3) - unstable_region_offset, x:size(2) - unstable_region_offset)
y = iproc.crop(y, unstable_region_offset, unstable_region_offset,
y:size(3) - unstable_region_offset, y:size(2) - unstable_region_offset)
assert(x:size(2) % 4 == 0 and x:size(3) % 4 == 0)
assert(x:size(1) == y:size(1) and x:size(2) == y:size(2) and x:size(3) == y:size(3))
else
assert(x:size(1) == y:size(1) and x:size(2) * scale == y:size(2) and x:size(3) * scale == y:size(3))
end
local scale_inner = scale
if options.x_upsampling then
scale_inner = 1
end
local batch = {}
for i = 1, n do
local xc, yc = pairwise_utils.active_cropping(x, y,
size,
scale_inner,
options.active_cropping_rate,
options.active_cropping_tries)
xc = iproc.byte2float(xc)
yc = iproc.byte2float(yc)
if options.rgb then
else
yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
end
table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
end
return batch
end
function pairwise_transform.test_scale(src)
torch.setdefaulttensortype("torch.FloatTensor")
local options = {random_color_noise_rate = 0.5,
random_half_rate = 0.5,
random_overlay_rate = 0.5,
random_unsharp_mask_rate = 0.5,
active_cropping_rate = 0.5,
active_cropping_tries = 10,
max_size = 256,
x_upsampling = false,
downsampling_filters = "Box",
rgb = true
}
local image = require 'image'
local src = image.lena()
for i = 1, 10 do
local xy = pairwise_transform.scale(src, 2.0, 128, 7, 1, options)
image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
end
end
return pairwise_transform