data_augmentation.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. require 'image'
  2. local iproc = require 'iproc'
  3. local data_augmentation = {}
  4. local function pcacov(x)
  5. local mean = torch.mean(x, 1)
  6. local xm = x - torch.ger(torch.ones(x:size(1)), mean:squeeze())
  7. local c = torch.mm(xm:t(), xm)
  8. c:div(x:size(1) - 1)
  9. local ce, cv = torch.symeig(c, 'V')
  10. return ce, cv
  11. end
  12. function data_augmentation.color_noise(src, factor)
  13. factor = factor or 0.1
  14. local src, conversion = iproc.byte2float(src)
  15. local src_t = src:reshape(src:size(1), src:nElement() / src:size(1)):t():contiguous()
  16. local ce, cv = pcacov(src_t)
  17. local color_scale = torch.Tensor(3):uniform(1 / (1 + factor), 1 + factor)
  18. pca_space = torch.mm(src_t, cv):t():contiguous()
  19. for i = 1, 3 do
  20. pca_space[i]:mul(color_scale[i])
  21. end
  22. local dest = torch.mm(pca_space:t(), cv:t()):t():contiguous():resizeAs(src)
  23. dest[torch.lt(dest, 0.0)] = 0.0
  24. dest[torch.gt(dest, 1.0)] = 1.0
  25. if conversion then
  26. dest = iproc.float2byte(dest)
  27. end
  28. return dest
  29. end
  30. function data_augmentation.shift_1px(src)
  31. -- reducing the even/odd issue in nearest neighbor scaler.
  32. local direction = torch.random(1, 4)
  33. local x_shift = 0
  34. local y_shift = 0
  35. if direction == 1 then
  36. x_shift = 1
  37. y_shift = 0
  38. elseif direction == 2 then
  39. x_shift = 0
  40. y_shift = 1
  41. elseif direction == 3 then
  42. x_shift = 1
  43. y_shift = 1
  44. elseif flip == 4 then
  45. x_shift = 0
  46. y_shift = 0
  47. end
  48. local w = src:size(3) - x_shift
  49. local h = src:size(2) - y_shift
  50. w = w - (w % 4)
  51. h = h - (h % 4)
  52. local dest = iproc.crop(src, x_shift, y_shift, x_shift + w, y_shift + h)
  53. return dest
  54. end
  55. function data_augmentation.flip(src)
  56. local flip = torch.random(1, 4)
  57. local src, conversion = iproc.byte2float(src)
  58. local dest
  59. src = src:contiguous()
  60. if flip == 1 then
  61. dest = image.hflip(src)
  62. elseif flip == 2 then
  63. dest = image.vflip(src)
  64. elseif flip == 3 then
  65. dest = image.hflip(image.vflip(src))
  66. elseif flip == 4 then
  67. dest = src
  68. end
  69. if conversion then
  70. dest = iproc.float2byte(dest)
  71. end
  72. return dest
  73. end
  74. function data_augmentation.overlay(src, p)
  75. p = p or 0.25
  76. if torch.uniform() < p then
  77. local r = torch.uniform(0.2, 0.8)
  78. local src, conversion = iproc.byte2float(src)
  79. src = src:contiguous()
  80. local flip = data_augmentation.flip(src)
  81. flip:mul(r):add(src * (1.0 - r))
  82. if conversion then
  83. flip = iproc.float2byte(flip)
  84. end
  85. return flip
  86. else
  87. return src
  88. end
  89. end
  90. return data_augmentation