data_augmentation.lua 4.5 KB

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