data_augmentation.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. data_augmentation.blur_conv = {}
  71. function data_augmentation.blur(src, p, size, sigma_min, sigma_max)
  72. size = size or "3"
  73. filters = utils.split(size, ",")
  74. for i = 1, #filters do
  75. local s = tonumber(filters[i])
  76. filters[i] = s
  77. if not data_augmentation.blur_conv[s] then
  78. data_augmentation.blur_conv[s] = nn.SpatialConvolutionMM(1, 1, s, s, 1, 1, (s - 1) / 2, (s - 1) / 2):noBias():cuda()
  79. end
  80. end
  81. if torch.uniform() < p then
  82. local src, conversion = iproc.byte2float(src)
  83. local kernel_size = filters[torch.random(1, #filters)]
  84. local sigma
  85. if sigma_min == sigma_max then
  86. sigma = sigma_min
  87. else
  88. sigma = torch.uniform(sigma_min, sigma_max)
  89. end
  90. local kernel = iproc.gaussian2d(kernel_size, sigma)
  91. data_augmentation.blur_conv[kernel_size].weight:copy(kernel)
  92. local dest = torch.Tensor(3, src:size(2), src:size(3))
  93. dest[1]:copy(data_augmentation.blur_conv[kernel_size]:forward(src[1]:reshape(1, src:size(2), src:size(3)):cuda()))
  94. dest[2]:copy(data_augmentation.blur_conv[kernel_size]:forward(src[2]:reshape(1, src:size(2), src:size(3)):cuda()))
  95. dest[3]:copy(data_augmentation.blur_conv[kernel_size]:forward(src[3]:reshape(1, src:size(2), src:size(3)):cuda()))
  96. if conversion then
  97. dest = iproc.float2byte(dest)
  98. end
  99. return dest
  100. else
  101. return src
  102. end
  103. end
  104. function data_augmentation.shift_1px(src)
  105. -- reducing the even/odd issue in nearest neighbor scaler.
  106. local direction = torch.random(1, 4)
  107. local x_shift = 0
  108. local y_shift = 0
  109. if direction == 1 then
  110. x_shift = 1
  111. y_shift = 0
  112. elseif direction == 2 then
  113. x_shift = 0
  114. y_shift = 1
  115. elseif direction == 3 then
  116. x_shift = 1
  117. y_shift = 1
  118. elseif flip == 4 then
  119. x_shift = 0
  120. y_shift = 0
  121. end
  122. local w = src:size(3) - x_shift
  123. local h = src:size(2) - y_shift
  124. w = w - (w % 4)
  125. h = h - (h % 4)
  126. local dest = iproc.crop(src, x_shift, y_shift, x_shift + w, y_shift + h)
  127. return dest
  128. end
  129. function data_augmentation.flip(src)
  130. local flip = torch.random(1, 4)
  131. local tr = torch.random(1, 2)
  132. local src, conversion = iproc.byte2float(src)
  133. local dest
  134. src = src:contiguous()
  135. if tr == 1 then
  136. -- pass
  137. elseif tr == 2 then
  138. src = src:transpose(2, 3):contiguous()
  139. end
  140. if flip == 1 then
  141. dest = iproc.hflip(src)
  142. elseif flip == 2 then
  143. dest = iproc.vflip(src)
  144. elseif flip == 3 then
  145. dest = iproc.hflip(iproc.vflip(src))
  146. elseif flip == 4 then
  147. dest = src
  148. end
  149. if conversion then
  150. dest = iproc.float2byte(dest)
  151. end
  152. return dest
  153. end
  154. local function test_blur()
  155. torch.setdefaulttensortype("torch.FloatTensor")
  156. local image =require 'image'
  157. local src = image.lena()
  158. image.display({image = src, min=0, max=1})
  159. local dest = data_augmentation.blur(src, 1.0, "3,5", 0.5, 0.6)
  160. image.display({image = dest, min=0, max=1})
  161. dest = data_augmentation.blur(src, 1.0, "3", 1.0, 1.0)
  162. image.display({image = dest, min=0, max=1})
  163. dest = data_augmentation.blur(src, 1.0, "5", 0.75, 0.75)
  164. image.display({image = dest, min=0, max=1})
  165. end
  166. --test_blur()
  167. return data_augmentation