data_augmentation.lua 7.9 KB

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