data_augmentation.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. local filters = {"Lanczos", "Catrom"}
  102. local x_filter = filters[torch.random(1, 2)]
  103. x = iproc.scale(x, w, h, x_filter)
  104. y = iproc.scale(y, w, h, "Triangle")
  105. return x, y
  106. else
  107. return x, y
  108. end
  109. end
  110. function data_augmentation.pairwise_rotate(x, y, p, r_min, r_max)
  111. if torch.uniform() < p then
  112. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  113. local r = torch.uniform(r_min, r_max) / 360.0 * math.pi
  114. x = iproc.rotate(x, r)
  115. y = iproc.rotate(y, r)
  116. return x, y
  117. else
  118. return x, y
  119. end
  120. end
  121. function data_augmentation.pairwise_negate(x, y, p)
  122. if torch.uniform() < p then
  123. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  124. x = iproc.negate(x)
  125. y = iproc.negate(y)
  126. return x, y
  127. else
  128. return x, y
  129. end
  130. end
  131. function data_augmentation.pairwise_negate_x(x, y, p)
  132. if torch.uniform() < p then
  133. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  134. x = iproc.negate(x)
  135. return x, y
  136. else
  137. return x, y
  138. end
  139. end
  140. function data_augmentation.pairwise_flip(x, y)
  141. local flip = torch.random(1, 4)
  142. local tr = torch.random(1, 2)
  143. local x, conversion = iproc.byte2float(x)
  144. y = iproc.byte2float(y)
  145. x = x:contiguous()
  146. y = y:contiguous()
  147. if tr == 1 then
  148. -- pass
  149. elseif tr == 2 then
  150. x = x:transpose(2, 3):contiguous()
  151. y = y:transpose(2, 3):contiguous()
  152. end
  153. if flip == 1 then
  154. x = iproc.hflip(x)
  155. y = iproc.hflip(y)
  156. elseif flip == 2 then
  157. x = iproc.vflip(x)
  158. y = iproc.vflip(y)
  159. elseif flip == 3 then
  160. x = iproc.hflip(iproc.vflip(x))
  161. y = iproc.hflip(iproc.vflip(y))
  162. elseif flip == 4 then
  163. end
  164. if conversion then
  165. x = iproc.float2byte(x)
  166. y = iproc.float2byte(y)
  167. end
  168. return x, y
  169. end
  170. function data_augmentation.shift_1px(src)
  171. -- reducing the even/odd issue in nearest neighbor scaler.
  172. local direction = torch.random(1, 4)
  173. local x_shift = 0
  174. local y_shift = 0
  175. if direction == 1 then
  176. x_shift = 1
  177. y_shift = 0
  178. elseif direction == 2 then
  179. x_shift = 0
  180. y_shift = 1
  181. elseif direction == 3 then
  182. x_shift = 1
  183. y_shift = 1
  184. elseif flip == 4 then
  185. x_shift = 0
  186. y_shift = 0
  187. end
  188. local w = src:size(3) - x_shift
  189. local h = src:size(2) - y_shift
  190. w = w - (w % 4)
  191. h = h - (h % 4)
  192. local dest = iproc.crop(src, x_shift, y_shift, x_shift + w, y_shift + h)
  193. return dest
  194. end
  195. function data_augmentation.flip(src)
  196. local flip = torch.random(1, 4)
  197. local tr = torch.random(1, 2)
  198. local src, conversion = iproc.byte2float(src)
  199. local dest
  200. src = src:contiguous()
  201. if tr == 1 then
  202. -- pass
  203. elseif tr == 2 then
  204. src = src:transpose(2, 3):contiguous()
  205. end
  206. if flip == 1 then
  207. dest = iproc.hflip(src)
  208. elseif flip == 2 then
  209. dest = iproc.vflip(src)
  210. elseif flip == 3 then
  211. dest = iproc.hflip(iproc.vflip(src))
  212. elseif flip == 4 then
  213. dest = src
  214. end
  215. if conversion then
  216. dest = iproc.float2byte(dest)
  217. end
  218. return dest
  219. end
  220. local function test_blur()
  221. torch.setdefaulttensortype("torch.FloatTensor")
  222. local image =require 'image'
  223. local src = image.lena()
  224. image.display({image = src, min=0, max=1})
  225. local dest = data_augmentation.blur(src, 1.0, "3,5", 0.5, 0.6)
  226. image.display({image = dest, min=0, max=1})
  227. dest = data_augmentation.blur(src, 1.0, "3", 1.0, 1.0)
  228. image.display({image = dest, min=0, max=1})
  229. dest = data_augmentation.blur(src, 1.0, "5", 0.75, 0.75)
  230. image.display({image = dest, min=0, max=1})
  231. end
  232. --test_blur()
  233. return data_augmentation