2016-09-24 08:32:33 +12:00
|
|
|
require 'pl'
|
|
|
|
require 'cunn'
|
2015-10-30 02:44:15 +13:00
|
|
|
local iproc = require 'iproc'
|
2016-09-11 08:07:42 +12:00
|
|
|
local gm = {}
|
|
|
|
gm.Image = require 'graphicsmagick.Image'
|
2015-10-30 02:44:15 +13:00
|
|
|
local data_augmentation = {}
|
|
|
|
|
|
|
|
local function pcacov(x)
|
|
|
|
local mean = torch.mean(x, 1)
|
|
|
|
local xm = x - torch.ger(torch.ones(x:size(1)), mean:squeeze())
|
|
|
|
local c = torch.mm(xm:t(), xm)
|
|
|
|
c:div(x:size(1) - 1)
|
|
|
|
local ce, cv = torch.symeig(c, 'V')
|
|
|
|
return ce, cv
|
|
|
|
end
|
2015-11-07 11:18:22 +13:00
|
|
|
function data_augmentation.color_noise(src, p, factor)
|
2015-10-30 02:44:15 +13:00
|
|
|
factor = factor or 0.1
|
2015-11-07 11:18:22 +13:00
|
|
|
if torch.uniform() < p then
|
|
|
|
local src, conversion = iproc.byte2float(src)
|
|
|
|
local src_t = src:reshape(src:size(1), src:nElement() / src:size(1)):t():contiguous()
|
|
|
|
local ce, cv = pcacov(src_t)
|
|
|
|
local color_scale = torch.Tensor(3):uniform(1 / (1 + factor), 1 + factor)
|
|
|
|
|
|
|
|
pca_space = torch.mm(src_t, cv):t():contiguous()
|
|
|
|
for i = 1, 3 do
|
|
|
|
pca_space[i]:mul(color_scale[i])
|
|
|
|
end
|
|
|
|
local dest = torch.mm(pca_space:t(), cv:t()):t():contiguous():resizeAs(src)
|
2016-09-24 11:17:37 +12:00
|
|
|
dest:clamp(0.0, 1.0)
|
2015-10-30 02:44:15 +13:00
|
|
|
|
2015-11-07 11:18:22 +13:00
|
|
|
if conversion then
|
|
|
|
dest = iproc.float2byte(dest)
|
|
|
|
end
|
|
|
|
return dest
|
|
|
|
else
|
|
|
|
return src
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function data_augmentation.overlay(src, p)
|
|
|
|
if torch.uniform() < p then
|
|
|
|
local r = torch.uniform()
|
|
|
|
local src, conversion = iproc.byte2float(src)
|
|
|
|
src = src:contiguous()
|
|
|
|
local flip = data_augmentation.flip(src)
|
|
|
|
flip:mul(r):add(src * (1.0 - r))
|
|
|
|
if conversion then
|
|
|
|
flip = iproc.float2byte(flip)
|
|
|
|
end
|
|
|
|
return flip
|
|
|
|
else
|
|
|
|
return src
|
2015-10-30 02:44:15 +13:00
|
|
|
end
|
|
|
|
end
|
2015-11-27 22:36:36 +13:00
|
|
|
function data_augmentation.unsharp_mask(src, p)
|
|
|
|
if torch.uniform() < p then
|
|
|
|
local radius = 0 -- auto
|
2015-12-04 22:47:33 +13:00
|
|
|
local sigma = torch.uniform(0.5, 1.5)
|
|
|
|
local amount = torch.uniform(0.1, 0.9)
|
2015-11-27 22:36:36 +13:00
|
|
|
local threshold = torch.uniform(0.0, 0.05)
|
|
|
|
local unsharp = gm.Image(src, "RGB", "DHW"):
|
|
|
|
unsharpMask(radius, sigma, amount, threshold):
|
|
|
|
toTensor("float", "RGB", "DHW")
|
|
|
|
|
|
|
|
if src:type() == "torch.ByteTensor" then
|
|
|
|
return iproc.float2byte(unsharp)
|
|
|
|
else
|
|
|
|
return unsharp
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return src
|
|
|
|
end
|
|
|
|
end
|
2016-09-24 08:32:33 +12:00
|
|
|
function data_augmentation.blur(src, p, size, sigma_min, sigma_max)
|
|
|
|
size = size or "3"
|
|
|
|
filters = utils.split(size, ",")
|
|
|
|
for i = 1, #filters do
|
|
|
|
local s = tonumber(filters[i])
|
|
|
|
filters[i] = s
|
|
|
|
end
|
|
|
|
if torch.uniform() < p then
|
|
|
|
local src, conversion = iproc.byte2float(src)
|
|
|
|
local kernel_size = filters[torch.random(1, #filters)]
|
|
|
|
local sigma
|
|
|
|
if sigma_min == sigma_max then
|
|
|
|
sigma = sigma_min
|
|
|
|
else
|
|
|
|
sigma = torch.uniform(sigma_min, sigma_max)
|
|
|
|
end
|
|
|
|
local kernel = iproc.gaussian2d(kernel_size, sigma)
|
2016-10-21 05:21:42 +13:00
|
|
|
local dest = image.convolve(src, kernel, 'same')
|
2016-09-24 08:32:33 +12:00
|
|
|
if conversion then
|
|
|
|
dest = iproc.float2byte(dest)
|
|
|
|
end
|
|
|
|
return dest
|
|
|
|
else
|
|
|
|
return src
|
|
|
|
end
|
|
|
|
end
|
2016-10-21 19:43:28 +13:00
|
|
|
function data_augmentation.pairwise_scale(x, y, p, scale_min, scale_max)
|
|
|
|
if torch.uniform() < p then
|
|
|
|
assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
|
|
|
|
local scale = torch.uniform(scale_min, scale_max)
|
|
|
|
local h = math.floor(x:size(2) * scale)
|
|
|
|
local w = math.floor(x:size(3) * scale)
|
2017-02-26 13:01:26 +13:00
|
|
|
local filters = {"Lanczos", "Catrom"}
|
|
|
|
local x_filter = filters[torch.random(1, 2)]
|
|
|
|
x = iproc.scale(x, w, h, x_filter)
|
2016-10-21 19:43:28 +13:00
|
|
|
y = iproc.scale(y, w, h, "Triangle")
|
|
|
|
return x, y
|
|
|
|
else
|
|
|
|
return x, y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function data_augmentation.pairwise_rotate(x, y, p, r_min, r_max)
|
|
|
|
if torch.uniform() < p then
|
|
|
|
assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
|
|
|
|
local r = torch.uniform(r_min, r_max) / 360.0 * math.pi
|
|
|
|
x = iproc.rotate(x, r)
|
|
|
|
y = iproc.rotate(y, r)
|
|
|
|
return x, y
|
|
|
|
else
|
|
|
|
return x, y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function data_augmentation.pairwise_negate(x, y, p)
|
|
|
|
if torch.uniform() < p then
|
|
|
|
assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
|
2016-10-24 21:23:09 +13:00
|
|
|
x = iproc.negate(x)
|
|
|
|
y = iproc.negate(y)
|
2016-10-21 19:43:28 +13:00
|
|
|
return x, y
|
|
|
|
else
|
|
|
|
return x, y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
function data_augmentation.pairwise_negate_x(x, y, p)
|
|
|
|
if torch.uniform() < p then
|
|
|
|
assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
|
2016-10-24 21:23:09 +13:00
|
|
|
x = iproc.negate(x)
|
2016-10-21 19:43:28 +13:00
|
|
|
return x, y
|
|
|
|
else
|
|
|
|
return x, y
|
|
|
|
end
|
|
|
|
end
|
2017-02-12 21:48:21 +13:00
|
|
|
function data_augmentation.pairwise_flip(x, y)
|
|
|
|
local flip = torch.random(1, 4)
|
|
|
|
local tr = torch.random(1, 2)
|
|
|
|
local x, conversion = iproc.byte2float(x)
|
|
|
|
y = iproc.byte2float(y)
|
|
|
|
x = x:contiguous()
|
|
|
|
y = y:contiguous()
|
|
|
|
if tr == 1 then
|
|
|
|
-- pass
|
|
|
|
elseif tr == 2 then
|
|
|
|
x = x:transpose(2, 3):contiguous()
|
|
|
|
y = y:transpose(2, 3):contiguous()
|
|
|
|
end
|
|
|
|
if flip == 1 then
|
|
|
|
x = iproc.hflip(x)
|
|
|
|
y = iproc.hflip(y)
|
|
|
|
elseif flip == 2 then
|
|
|
|
x = iproc.vflip(x)
|
|
|
|
y = iproc.vflip(y)
|
|
|
|
elseif flip == 3 then
|
|
|
|
x = iproc.hflip(iproc.vflip(x))
|
|
|
|
y = iproc.hflip(iproc.vflip(y))
|
|
|
|
elseif flip == 4 then
|
|
|
|
end
|
|
|
|
if conversion then
|
|
|
|
x = iproc.float2byte(x)
|
|
|
|
y = iproc.float2byte(y)
|
|
|
|
end
|
|
|
|
return x, y
|
|
|
|
end
|
2015-10-30 02:44:15 +13:00
|
|
|
function data_augmentation.shift_1px(src)
|
|
|
|
-- reducing the even/odd issue in nearest neighbor scaler.
|
|
|
|
local direction = torch.random(1, 4)
|
|
|
|
local x_shift = 0
|
|
|
|
local y_shift = 0
|
|
|
|
if direction == 1 then
|
|
|
|
x_shift = 1
|
|
|
|
y_shift = 0
|
|
|
|
elseif direction == 2 then
|
|
|
|
x_shift = 0
|
|
|
|
y_shift = 1
|
|
|
|
elseif direction == 3 then
|
|
|
|
x_shift = 1
|
|
|
|
y_shift = 1
|
|
|
|
elseif flip == 4 then
|
|
|
|
x_shift = 0
|
|
|
|
y_shift = 0
|
|
|
|
end
|
|
|
|
local w = src:size(3) - x_shift
|
|
|
|
local h = src:size(2) - y_shift
|
|
|
|
w = w - (w % 4)
|
|
|
|
h = h - (h % 4)
|
|
|
|
local dest = iproc.crop(src, x_shift, y_shift, x_shift + w, y_shift + h)
|
|
|
|
return dest
|
|
|
|
end
|
|
|
|
function data_augmentation.flip(src)
|
|
|
|
local flip = torch.random(1, 4)
|
2015-11-10 03:44:43 +13:00
|
|
|
local tr = torch.random(1, 2)
|
2015-10-30 02:44:15 +13:00
|
|
|
local src, conversion = iproc.byte2float(src)
|
|
|
|
local dest
|
|
|
|
src = src:contiguous()
|
2015-11-10 03:44:43 +13:00
|
|
|
if tr == 1 then
|
|
|
|
-- pass
|
|
|
|
elseif tr == 2 then
|
|
|
|
src = src:transpose(2, 3):contiguous()
|
|
|
|
end
|
2015-10-30 02:44:15 +13:00
|
|
|
if flip == 1 then
|
2016-09-11 08:07:42 +12:00
|
|
|
dest = iproc.hflip(src)
|
2015-10-30 02:44:15 +13:00
|
|
|
elseif flip == 2 then
|
2016-09-11 08:07:42 +12:00
|
|
|
dest = iproc.vflip(src)
|
2015-10-30 02:44:15 +13:00
|
|
|
elseif flip == 3 then
|
2016-09-11 08:07:42 +12:00
|
|
|
dest = iproc.hflip(iproc.vflip(src))
|
2015-10-30 02:44:15 +13:00
|
|
|
elseif flip == 4 then
|
|
|
|
dest = src
|
|
|
|
end
|
|
|
|
if conversion then
|
|
|
|
dest = iproc.float2byte(dest)
|
|
|
|
end
|
|
|
|
return dest
|
|
|
|
end
|
2016-09-24 08:32:33 +12:00
|
|
|
|
|
|
|
local function test_blur()
|
|
|
|
torch.setdefaulttensortype("torch.FloatTensor")
|
|
|
|
local image =require 'image'
|
|
|
|
local src = image.lena()
|
|
|
|
|
|
|
|
image.display({image = src, min=0, max=1})
|
|
|
|
local dest = data_augmentation.blur(src, 1.0, "3,5", 0.5, 0.6)
|
|
|
|
image.display({image = dest, min=0, max=1})
|
|
|
|
dest = data_augmentation.blur(src, 1.0, "3", 1.0, 1.0)
|
|
|
|
image.display({image = dest, min=0, max=1})
|
|
|
|
dest = data_augmentation.blur(src, 1.0, "5", 0.75, 0.75)
|
|
|
|
image.display({image = dest, min=0, max=1})
|
|
|
|
end
|
|
|
|
--test_blur()
|
|
|
|
|
2015-10-30 02:44:15 +13:00
|
|
|
return data_augmentation
|