data_augmentation.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. require 'pl'
  2. require 'cunn'
  3. local iproc = require 'iproc'
  4. local gm = {}
  5. gm.Image = require 'graphicsmagick.Image'
  6. local data_augmentation = {}
  7. local function pcacov(x)
  8. local mean = torch.mean(x, 1)
  9. local xm = x - torch.ger(torch.ones(x:size(1)), mean:squeeze())
  10. local c = torch.mm(xm:t(), xm)
  11. c:div(x:size(1) - 1)
  12. local ce, cv = torch.symeig(c, 'V')
  13. return ce, cv
  14. end
  15. function data_augmentation.color_noise(src, p, factor)
  16. factor = factor or 0.1
  17. if torch.uniform() < p then
  18. local src, conversion = iproc.byte2float(src)
  19. local src_t = src:reshape(src:size(1), src:nElement() / src:size(1)):t():contiguous()
  20. local ce, cv = pcacov(src_t)
  21. local color_scale = torch.Tensor(3):uniform(1 / (1 + factor), 1 + factor)
  22. pca_space = torch.mm(src_t, cv):t():contiguous()
  23. for i = 1, 3 do
  24. pca_space[i]:mul(color_scale[i])
  25. end
  26. local dest = torch.mm(pca_space:t(), cv:t()):t():contiguous():resizeAs(src)
  27. dest:clamp(0.0, 1.0)
  28. if conversion then
  29. dest = iproc.float2byte(dest)
  30. end
  31. return dest
  32. else
  33. return src
  34. end
  35. end
  36. function data_augmentation.overlay(src, p)
  37. if torch.uniform() < p then
  38. local r = torch.uniform()
  39. local src, conversion = iproc.byte2float(src)
  40. src = src:contiguous()
  41. local flip = data_augmentation.flip(src)
  42. flip:mul(r):add(src * (1.0 - r))
  43. if conversion then
  44. flip = iproc.float2byte(flip)
  45. end
  46. return flip
  47. else
  48. return src
  49. end
  50. end
  51. function data_augmentation.unsharp_mask(src, p)
  52. if torch.uniform() < p then
  53. local radius = 0 -- auto
  54. local sigma = torch.uniform(0.5, 1.5)
  55. local amount = torch.uniform(0.1, 0.9)
  56. local threshold = torch.uniform(0.0, 0.05)
  57. local unsharp = gm.Image(src, "RGB", "DHW"):
  58. unsharpMask(radius, sigma, amount, threshold):
  59. toTensor("float", "RGB", "DHW")
  60. if src:type() == "torch.ByteTensor" then
  61. return iproc.float2byte(unsharp)
  62. else
  63. return unsharp
  64. end
  65. else
  66. return src
  67. end
  68. end
  69. function data_augmentation.blur(src, p, size, sigma_min, sigma_max)
  70. size = size or "3"
  71. filters = utils.split(size, ",")
  72. for i = 1, #filters do
  73. local s = tonumber(filters[i])
  74. filters[i] = s
  75. end
  76. if torch.uniform() < p then
  77. local src, conversion = iproc.byte2float(src)
  78. local kernel_size = filters[torch.random(1, #filters)]
  79. local sigma
  80. if sigma_min == sigma_max then
  81. sigma = sigma_min
  82. else
  83. sigma = torch.uniform(sigma_min, sigma_max)
  84. end
  85. local kernel = iproc.gaussian2d(kernel_size, sigma)
  86. local dest = image.convolve(src, kernel, 'same')
  87. if conversion then
  88. dest = iproc.float2byte(dest)
  89. end
  90. return dest
  91. else
  92. return src
  93. end
  94. end
  95. function data_augmentation.shift_1px(src)
  96. -- reducing the even/odd issue in nearest neighbor scaler.
  97. local direction = torch.random(1, 4)
  98. local x_shift = 0
  99. local y_shift = 0
  100. if direction == 1 then
  101. x_shift = 1
  102. y_shift = 0
  103. elseif direction == 2 then
  104. x_shift = 0
  105. y_shift = 1
  106. elseif direction == 3 then
  107. x_shift = 1
  108. y_shift = 1
  109. elseif flip == 4 then
  110. x_shift = 0
  111. y_shift = 0
  112. end
  113. local w = src:size(3) - x_shift
  114. local h = src:size(2) - y_shift
  115. w = w - (w % 4)
  116. h = h - (h % 4)
  117. local dest = iproc.crop(src, x_shift, y_shift, x_shift + w, y_shift + h)
  118. return dest
  119. end
  120. function data_augmentation.flip(src)
  121. local flip = torch.random(1, 4)
  122. local tr = torch.random(1, 2)
  123. local src, conversion = iproc.byte2float(src)
  124. local dest
  125. src = src:contiguous()
  126. if tr == 1 then
  127. -- pass
  128. elseif tr == 2 then
  129. src = src:transpose(2, 3):contiguous()
  130. end
  131. if flip == 1 then
  132. dest = iproc.hflip(src)
  133. elseif flip == 2 then
  134. dest = iproc.vflip(src)
  135. elseif flip == 3 then
  136. dest = iproc.hflip(iproc.vflip(src))
  137. elseif flip == 4 then
  138. dest = src
  139. end
  140. if conversion then
  141. dest = iproc.float2byte(dest)
  142. end
  143. return dest
  144. end
  145. local function test_blur()
  146. torch.setdefaulttensortype("torch.FloatTensor")
  147. local image =require 'image'
  148. local src = image.lena()
  149. image.display({image = src, min=0, max=1})
  150. local dest = data_augmentation.blur(src, 1.0, "3,5", 0.5, 0.6)
  151. image.display({image = dest, min=0, max=1})
  152. dest = data_augmentation.blur(src, 1.0, "3", 1.0, 1.0)
  153. image.display({image = dest, min=0, max=1})
  154. dest = data_augmentation.blur(src, 1.0, "5", 0.75, 0.75)
  155. image.display({image = dest, min=0, max=1})
  156. end
  157. --test_blur()
  158. return data_augmentation