data_augmentation.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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)
  123. y = iproc.negate(y)
  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)
  133. return x, y
  134. else
  135. return x, y
  136. end
  137. end
  138. function data_augmentation.pairwise_flip(x, y)
  139. local flip = torch.random(1, 4)
  140. local tr = torch.random(1, 2)
  141. local x, conversion = iproc.byte2float(x)
  142. y = iproc.byte2float(y)
  143. x = x:contiguous()
  144. y = y:contiguous()
  145. if tr == 1 then
  146. -- pass
  147. elseif tr == 2 then
  148. x = x:transpose(2, 3):contiguous()
  149. y = y:transpose(2, 3):contiguous()
  150. end
  151. if flip == 1 then
  152. x = iproc.hflip(x)
  153. y = iproc.hflip(y)
  154. elseif flip == 2 then
  155. x = iproc.vflip(x)
  156. y = iproc.vflip(y)
  157. elseif flip == 3 then
  158. x = iproc.hflip(iproc.vflip(x))
  159. y = iproc.hflip(iproc.vflip(y))
  160. elseif flip == 4 then
  161. end
  162. if conversion then
  163. x = iproc.float2byte(x)
  164. y = iproc.float2byte(y)
  165. end
  166. return x, y
  167. end
  168. function data_augmentation.shift_1px(src)
  169. -- reducing the even/odd issue in nearest neighbor scaler.
  170. local direction = torch.random(1, 4)
  171. local x_shift = 0
  172. local y_shift = 0
  173. if direction == 1 then
  174. x_shift = 1
  175. y_shift = 0
  176. elseif direction == 2 then
  177. x_shift = 0
  178. y_shift = 1
  179. elseif direction == 3 then
  180. x_shift = 1
  181. y_shift = 1
  182. elseif flip == 4 then
  183. x_shift = 0
  184. y_shift = 0
  185. end
  186. local w = src:size(3) - x_shift
  187. local h = src:size(2) - y_shift
  188. w = w - (w % 4)
  189. h = h - (h % 4)
  190. local dest = iproc.crop(src, x_shift, y_shift, x_shift + w, y_shift + h)
  191. return dest
  192. end
  193. function data_augmentation.flip(src)
  194. local flip = torch.random(1, 4)
  195. local tr = torch.random(1, 2)
  196. local src, conversion = iproc.byte2float(src)
  197. local dest
  198. src = src:contiguous()
  199. if tr == 1 then
  200. -- pass
  201. elseif tr == 2 then
  202. src = src:transpose(2, 3):contiguous()
  203. end
  204. if flip == 1 then
  205. dest = iproc.hflip(src)
  206. elseif flip == 2 then
  207. dest = iproc.vflip(src)
  208. elseif flip == 3 then
  209. dest = iproc.hflip(iproc.vflip(src))
  210. elseif flip == 4 then
  211. dest = src
  212. end
  213. if conversion then
  214. dest = iproc.float2byte(dest)
  215. end
  216. return dest
  217. end
  218. local function test_blur()
  219. torch.setdefaulttensortype("torch.FloatTensor")
  220. local image =require 'image'
  221. local src = image.lena()
  222. image.display({image = src, min=0, max=1})
  223. local dest = data_augmentation.blur(src, 1.0, "3,5", 0.5, 0.6)
  224. image.display({image = dest, min=0, max=1})
  225. dest = data_augmentation.blur(src, 1.0, "3", 1.0, 1.0)
  226. image.display({image = dest, min=0, max=1})
  227. dest = data_augmentation.blur(src, 1.0, "5", 0.75, 0.75)
  228. image.display({image = dest, min=0, max=1})
  229. end
  230. --test_blur()
  231. return data_augmentation