data_augmentation.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.erase(src, p, n, rect_min, rect_max)
  16. if torch.uniform() < p then
  17. local src, conversion = iproc.byte2float(src)
  18. src = src:contiguous():clone()
  19. local ch = src:size(1)
  20. local height = src:size(2)
  21. local width = src:size(3)
  22. for i = 1, n do
  23. local r = torch.Tensor(4):uniform():cmul(torch.Tensor({height-1, width-1, rect_max - rect_min, rect_max - rect_min})):int()
  24. local rect_y1 = r[1] + 1
  25. local rect_x1 = r[2] + 1
  26. local rect_h = r[3] + rect_min
  27. local rect_w = r[4] + rect_min
  28. local rect_x2 = math.min(rect_x1 + rect_w, width)
  29. local rect_y2 = math.min(rect_y1 + rect_h, height)
  30. local sub_rect = src:sub(1, ch, rect_y1, rect_y2, rect_x1, rect_x2)
  31. for i = 1, ch do
  32. sub_rect[i]:fill(src[i][rect_y1][rect_x1])
  33. end
  34. end
  35. if conversion then
  36. src = iproc.float2byte(src)
  37. end
  38. return src
  39. else
  40. return src
  41. end
  42. end
  43. function data_augmentation.color_noise(src, p, factor)
  44. factor = factor or 0.1
  45. if torch.uniform() < p then
  46. local src, conversion = iproc.byte2float(src)
  47. local src_t = src:reshape(src:size(1), src:nElement() / src:size(1)):t():contiguous()
  48. local ce, cv = pcacov(src_t)
  49. local color_scale = torch.Tensor(3):uniform(1 / (1 + factor), 1 + factor)
  50. pca_space = torch.mm(src_t, cv):t():contiguous()
  51. for i = 1, 3 do
  52. pca_space[i]:mul(color_scale[i])
  53. end
  54. local dest = torch.mm(pca_space:t(), cv:t()):t():contiguous():resizeAs(src)
  55. dest:clamp(0.0, 1.0)
  56. if conversion then
  57. dest = iproc.float2byte(dest)
  58. end
  59. return dest
  60. else
  61. return src
  62. end
  63. end
  64. function data_augmentation.overlay(src, p)
  65. if torch.uniform() < p then
  66. local r = torch.uniform()
  67. local src, conversion = iproc.byte2float(src)
  68. src = src:contiguous()
  69. local flip = data_augmentation.flip(src)
  70. flip:mul(r):add(src * (1.0 - r))
  71. if conversion then
  72. flip = iproc.float2byte(flip)
  73. end
  74. return flip
  75. else
  76. return src
  77. end
  78. end
  79. function data_augmentation.unsharp_mask(src, p)
  80. if torch.uniform() < p then
  81. local radius = 0 -- auto
  82. local sigma = torch.uniform(0.5, 1.5)
  83. local amount = torch.uniform(0.1, 0.9)
  84. local threshold = torch.uniform(0.0, 0.05)
  85. local unsharp = gm.Image(src, "RGB", "DHW"):
  86. unsharpMask(radius, sigma, amount, threshold):
  87. toTensor("float", "RGB", "DHW")
  88. if src:type() == "torch.ByteTensor" then
  89. return iproc.float2byte(unsharp)
  90. else
  91. return unsharp
  92. end
  93. else
  94. return src
  95. end
  96. end
  97. function data_augmentation.blur(src, p, size, sigma_min, sigma_max)
  98. size = size or "3"
  99. filters = utils.split(size, ",")
  100. for i = 1, #filters do
  101. local s = tonumber(filters[i])
  102. filters[i] = s
  103. end
  104. if torch.uniform() < p then
  105. local src, conversion = iproc.byte2float(src)
  106. local kernel_size = filters[torch.random(1, #filters)]
  107. local sigma
  108. if sigma_min == sigma_max then
  109. sigma = sigma_min
  110. else
  111. sigma = torch.uniform(sigma_min, sigma_max)
  112. end
  113. local kernel = iproc.gaussian2d(kernel_size, sigma)
  114. local dest = image.convolve(src, kernel, 'same')
  115. if conversion then
  116. dest = iproc.float2byte(dest)
  117. end
  118. return dest
  119. else
  120. return src
  121. end
  122. end
  123. function data_augmentation.pairwise_scale(x, y, p, scale_min, scale_max)
  124. if torch.uniform() < p then
  125. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  126. local scale = torch.uniform(scale_min, scale_max)
  127. local h = math.floor(x:size(2) * scale)
  128. local w = math.floor(x:size(3) * scale)
  129. local filters = {"Lanczos", "Catrom"}
  130. local x_filter = filters[torch.random(1, 2)]
  131. x = iproc.scale(x, w, h, x_filter)
  132. y = iproc.scale(y, w, h, "Triangle")
  133. return x, y
  134. else
  135. return x, y
  136. end
  137. end
  138. function data_augmentation.pairwise_rotate(x, y, p, r_min, r_max)
  139. if torch.uniform() < p then
  140. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  141. local r = torch.uniform(r_min, r_max) / 360.0 * math.pi
  142. x = iproc.rotate(x, r)
  143. y = iproc.rotate(y, r)
  144. return x, y
  145. else
  146. return x, y
  147. end
  148. end
  149. function data_augmentation.pairwise_negate(x, y, p)
  150. if torch.uniform() < p then
  151. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  152. x = iproc.negate(x)
  153. y = iproc.negate(y)
  154. return x, y
  155. else
  156. return x, y
  157. end
  158. end
  159. function data_augmentation.pairwise_negate_x(x, y, p)
  160. if torch.uniform() < p then
  161. assert(x:size(2) == y:size(2) and x:size(3) == y:size(3))
  162. x = iproc.negate(x)
  163. return x, y
  164. else
  165. return x, y
  166. end
  167. end
  168. function data_augmentation.pairwise_flip(x, y)
  169. local flip = torch.random(1, 4)
  170. local tr = torch.random(1, 2)
  171. local x, conversion = iproc.byte2float(x)
  172. y = iproc.byte2float(y)
  173. x = x:contiguous()
  174. y = y:contiguous()
  175. if tr == 1 then
  176. -- pass
  177. elseif tr == 2 then
  178. x = x:transpose(2, 3):contiguous()
  179. y = y:transpose(2, 3):contiguous()
  180. end
  181. if flip == 1 then
  182. x = iproc.hflip(x)
  183. y = iproc.hflip(y)
  184. elseif flip == 2 then
  185. x = iproc.vflip(x)
  186. y = iproc.vflip(y)
  187. elseif flip == 3 then
  188. x = iproc.hflip(iproc.vflip(x))
  189. y = iproc.hflip(iproc.vflip(y))
  190. elseif flip == 4 then
  191. end
  192. if conversion then
  193. x = iproc.float2byte(x)
  194. y = iproc.float2byte(y)
  195. end
  196. return x, y
  197. end
  198. function data_augmentation.shift_1px(src)
  199. -- reducing the even/odd issue in nearest neighbor scaler.
  200. local direction = torch.random(1, 4)
  201. local x_shift = 0
  202. local y_shift = 0
  203. if direction == 1 then
  204. x_shift = 1
  205. y_shift = 0
  206. elseif direction == 2 then
  207. x_shift = 0
  208. y_shift = 1
  209. elseif direction == 3 then
  210. x_shift = 1
  211. y_shift = 1
  212. elseif flip == 4 then
  213. x_shift = 0
  214. y_shift = 0
  215. end
  216. local w = src:size(3) - x_shift
  217. local h = src:size(2) - y_shift
  218. w = w - (w % 4)
  219. h = h - (h % 4)
  220. local dest = iproc.crop(src, x_shift, y_shift, x_shift + w, y_shift + h)
  221. return dest
  222. end
  223. function data_augmentation.flip(src)
  224. local flip = torch.random(1, 4)
  225. local tr = torch.random(1, 2)
  226. local src, conversion = iproc.byte2float(src)
  227. local dest
  228. src = src:contiguous()
  229. if tr == 1 then
  230. -- pass
  231. elseif tr == 2 then
  232. src = src:transpose(2, 3):contiguous()
  233. end
  234. if flip == 1 then
  235. dest = iproc.hflip(src)
  236. elseif flip == 2 then
  237. dest = iproc.vflip(src)
  238. elseif flip == 3 then
  239. dest = iproc.hflip(iproc.vflip(src))
  240. elseif flip == 4 then
  241. dest = src
  242. end
  243. if conversion then
  244. dest = iproc.float2byte(dest)
  245. end
  246. return dest
  247. end
  248. local function test_blur()
  249. torch.setdefaulttensortype("torch.FloatTensor")
  250. local image =require 'image'
  251. local src = image.lena()
  252. image.display({image = src, min=0, max=1})
  253. local dest = data_augmentation.blur(src, 1.0, "3,5", 0.5, 0.6)
  254. image.display({image = dest, min=0, max=1})
  255. dest = data_augmentation.blur(src, 1.0, "3", 1.0, 1.0)
  256. image.display({image = dest, min=0, max=1})
  257. dest = data_augmentation.blur(src, 1.0, "5", 0.75, 0.75)
  258. image.display({image = dest, min=0, max=1})
  259. end
  260. --test_blur()
  261. return data_augmentation