data_augmentation.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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, p, factor)
  13. factor = factor or 0.1
  14. if torch.uniform() < p then
  15. local src, conversion = iproc.byte2float(src)
  16. local src_t = src:reshape(src:size(1), src:nElement() / src:size(1)):t():contiguous()
  17. local ce, cv = pcacov(src_t)
  18. local color_scale = torch.Tensor(3):uniform(1 / (1 + factor), 1 + factor)
  19. pca_space = torch.mm(src_t, cv):t():contiguous()
  20. for i = 1, 3 do
  21. pca_space[i]:mul(color_scale[i])
  22. end
  23. local dest = torch.mm(pca_space:t(), cv:t()):t():contiguous():resizeAs(src)
  24. dest[torch.lt(dest, 0.0)] = 0.0
  25. dest[torch.gt(dest, 1.0)] = 1.0
  26. if conversion then
  27. dest = iproc.float2byte(dest)
  28. end
  29. return dest
  30. else
  31. return src
  32. end
  33. end
  34. function data_augmentation.overlay(src, p)
  35. if torch.uniform() < p then
  36. local r = torch.uniform()
  37. local src, conversion = iproc.byte2float(src)
  38. src = src:contiguous()
  39. local flip = data_augmentation.flip(src)
  40. flip:mul(r):add(src * (1.0 - r))
  41. if conversion then
  42. flip = iproc.float2byte(flip)
  43. end
  44. return flip
  45. else
  46. return src
  47. end
  48. end
  49. function data_augmentation.shift_1px(src)
  50. -- reducing the even/odd issue in nearest neighbor scaler.
  51. local direction = torch.random(1, 4)
  52. local x_shift = 0
  53. local y_shift = 0
  54. if direction == 1 then
  55. x_shift = 1
  56. y_shift = 0
  57. elseif direction == 2 then
  58. x_shift = 0
  59. y_shift = 1
  60. elseif direction == 3 then
  61. x_shift = 1
  62. y_shift = 1
  63. elseif flip == 4 then
  64. x_shift = 0
  65. y_shift = 0
  66. end
  67. local w = src:size(3) - x_shift
  68. local h = src:size(2) - y_shift
  69. w = w - (w % 4)
  70. h = h - (h % 4)
  71. local dest = iproc.crop(src, x_shift, y_shift, x_shift + w, y_shift + h)
  72. return dest
  73. end
  74. function data_augmentation.flip(src)
  75. local flip = torch.random(1, 4)
  76. local tr = torch.random(1, 2)
  77. local src, conversion = iproc.byte2float(src)
  78. local dest
  79. src = src:contiguous()
  80. if tr == 1 then
  81. -- pass
  82. elseif tr == 2 then
  83. src = src:transpose(2, 3):contiguous()
  84. end
  85. if flip == 1 then
  86. dest = image.hflip(src)
  87. elseif flip == 2 then
  88. dest = image.vflip(src)
  89. elseif flip == 3 then
  90. dest = image.hflip(image.vflip(src))
  91. elseif flip == 4 then
  92. dest = src
  93. end
  94. if conversion then
  95. dest = iproc.float2byte(dest)
  96. end
  97. return dest
  98. end
  99. return data_augmentation