data_augmentation.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.pairwise_scale(x, y, p, scale_min, scale_max)
  96. if torch.uniform() < p then
  97. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  98. local scale = torch.uniform(scale_min, scale_max)
  99. local h = math.floor(x:size(2) * scale)
  100. local w = math.floor(x:size(3) * scale)
  101. x = iproc.scale(x, w, h, "Triangle")
  102. y = iproc.scale(y, w, h, "Triangle")
  103. return x, y
  104. else
  105. return x, y
  106. end
  107. end
  108. function data_augmentation.pairwise_rotate(x, y, p, r_min, r_max)
  109. if torch.uniform() < p then
  110. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  111. local r = torch.uniform(r_min, r_max) / 360.0 * math.pi
  112. x = iproc.rotate(x, r)
  113. y = iproc.rotate(y, r)
  114. return x, y
  115. else
  116. return x, y
  117. end
  118. end
  119. function data_augmentation.pairwise_negate(x, y, p)
  120. if torch.uniform() < p then
  121. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  122. x = iproc.negate(x, r)
  123. y = iproc.rotate(y, r)
  124. return x, y
  125. else
  126. return x, y
  127. end
  128. end
  129. function data_augmentation.pairwise_negate_x(x, y, p)
  130. if torch.uniform() < p then
  131. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  132. x = iproc.negate(x, r)
  133. return x, y
  134. else
  135. return x, y
  136. end
  137. end
  138. function data_augmentation.shift_1px(src)
  139. -- reducing the even/odd issue in nearest neighbor scaler.
  140. local direction = torch.random(1, 4)
  141. local x_shift = 0
  142. local y_shift = 0
  143. if direction == 1 then
  144. x_shift = 1
  145. y_shift = 0
  146. elseif direction == 2 then
  147. x_shift = 0
  148. y_shift = 1
  149. elseif direction == 3 then
  150. x_shift = 1
  151. y_shift = 1
  152. elseif flip == 4 then
  153. x_shift = 0
  154. y_shift = 0
  155. end
  156. local w = src:size(3) - x_shift
  157. local h = src:size(2) - y_shift
  158. w = w - (w % 4)
  159. h = h - (h % 4)
  160. local dest = iproc.crop(src, x_shift, y_shift, x_shift + w, y_shift + h)
  161. return dest
  162. end
  163. function data_augmentation.flip(src)
  164. local flip = torch.random(1, 4)
  165. local tr = torch.random(1, 2)
  166. local src, conversion = iproc.byte2float(src)
  167. local dest
  168. src = src:contiguous()
  169. if tr == 1 then
  170. -- pass
  171. elseif tr == 2 then
  172. src = src:transpose(2, 3):contiguous()
  173. end
  174. if flip == 1 then
  175. dest = iproc.hflip(src)
  176. elseif flip == 2 then
  177. dest = iproc.vflip(src)
  178. elseif flip == 3 then
  179. dest = iproc.hflip(iproc.vflip(src))
  180. elseif flip == 4 then
  181. dest = src
  182. end
  183. if conversion then
  184. dest = iproc.float2byte(dest)
  185. end
  186. return dest
  187. end
  188. local function test_blur()
  189. torch.setdefaulttensortype("torch.FloatTensor")
  190. local image =require 'image'
  191. local src = image.lena()
  192. image.display({image = src, min=0, max=1})
  193. local dest = data_augmentation.blur(src, 1.0, "3,5", 0.5, 0.6)
  194. image.display({image = dest, min=0, max=1})
  195. dest = data_augmentation.blur(src, 1.0, "3", 1.0, 1.0)
  196. image.display({image = dest, min=0, max=1})
  197. dest = data_augmentation.blur(src, 1.0, "5", 0.75, 0.75)
  198. image.display({image = dest, min=0, max=1})
  199. end
  200. --test_blur()
  201. return data_augmentation