data_augmentation.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. local iproc = require 'iproc'
  2. local gm = {}
  3. gm.Image = require 'graphicsmagick.Image'
  4. local data_augmentation = {}
  5. local function pcacov(x)
  6. local mean = torch.mean(x, 1)
  7. local xm = x - torch.ger(torch.ones(x:size(1)), mean:squeeze())
  8. local c = torch.mm(xm:t(), xm)
  9. c:div(x:size(1) - 1)
  10. local ce, cv = torch.symeig(c, 'V')
  11. return ce, cv
  12. end
  13. function data_augmentation.color_noise(src, p, factor)
  14. factor = factor or 0.1
  15. if torch.uniform() < p then
  16. local src, conversion = iproc.byte2float(src)
  17. local src_t = src:reshape(src:size(1), src:nElement() / src:size(1)):t():contiguous()
  18. local ce, cv = pcacov(src_t)
  19. local color_scale = torch.Tensor(3):uniform(1 / (1 + factor), 1 + factor)
  20. pca_space = torch.mm(src_t, cv):t():contiguous()
  21. for i = 1, 3 do
  22. pca_space[i]:mul(color_scale[i])
  23. end
  24. local dest = torch.mm(pca_space:t(), cv:t()):t():contiguous():resizeAs(src)
  25. dest[torch.lt(dest, 0.0)] = 0.0
  26. dest[torch.gt(dest, 1.0)] = 1.0
  27. if conversion then
  28. dest = iproc.float2byte(dest)
  29. end
  30. return dest
  31. else
  32. return src
  33. end
  34. end
  35. function data_augmentation.overlay(src, p)
  36. if torch.uniform() < p then
  37. local r = torch.uniform()
  38. local src, conversion = iproc.byte2float(src)
  39. src = src:contiguous()
  40. local flip = data_augmentation.flip(src)
  41. flip:mul(r):add(src * (1.0 - r))
  42. if conversion then
  43. flip = iproc.float2byte(flip)
  44. end
  45. return flip
  46. else
  47. return src
  48. end
  49. end
  50. function data_augmentation.unsharp_mask(src, p)
  51. if torch.uniform() < p then
  52. local radius = 0 -- auto
  53. local sigma = torch.uniform(0.5, 1.5)
  54. local amount = torch.uniform(0.1, 0.9)
  55. local threshold = torch.uniform(0.0, 0.05)
  56. local unsharp = gm.Image(src, "RGB", "DHW"):
  57. unsharpMask(radius, sigma, amount, threshold):
  58. toTensor("float", "RGB", "DHW")
  59. if src:type() == "torch.ByteTensor" then
  60. return iproc.float2byte(unsharp)
  61. else
  62. return unsharp
  63. end
  64. else
  65. return src
  66. end
  67. end
  68. function data_augmentation.shift_1px(src)
  69. -- reducing the even/odd issue in nearest neighbor scaler.
  70. local direction = torch.random(1, 4)
  71. local x_shift = 0
  72. local y_shift = 0
  73. if direction == 1 then
  74. x_shift = 1
  75. y_shift = 0
  76. elseif direction == 2 then
  77. x_shift = 0
  78. y_shift = 1
  79. elseif direction == 3 then
  80. x_shift = 1
  81. y_shift = 1
  82. elseif flip == 4 then
  83. x_shift = 0
  84. y_shift = 0
  85. end
  86. local w = src:size(3) - x_shift
  87. local h = src:size(2) - y_shift
  88. w = w - (w % 4)
  89. h = h - (h % 4)
  90. local dest = iproc.crop(src, x_shift, y_shift, x_shift + w, y_shift + h)
  91. return dest
  92. end
  93. function data_augmentation.flip(src)
  94. local flip = torch.random(1, 4)
  95. local tr = torch.random(1, 2)
  96. local src, conversion = iproc.byte2float(src)
  97. local dest
  98. src = src:contiguous()
  99. if tr == 1 then
  100. -- pass
  101. elseif tr == 2 then
  102. src = src:transpose(2, 3):contiguous()
  103. end
  104. if flip == 1 then
  105. dest = iproc.hflip(src)
  106. elseif flip == 2 then
  107. dest = iproc.vflip(src)
  108. elseif flip == 3 then
  109. dest = iproc.hflip(iproc.vflip(src))
  110. elseif flip == 4 then
  111. dest = src
  112. end
  113. if conversion then
  114. dest = iproc.float2byte(dest)
  115. end
  116. return dest
  117. end
  118. return data_augmentation