pairwise_transform.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. require 'image'
  2. local gm = require 'graphicsmagick'
  3. local iproc = require 'iproc'
  4. local data_augmentation = require 'data_augmentation'
  5. local pairwise_transform = {}
  6. local function random_half(src, p)
  7. if torch.uniform() < p then
  8. local filter = ({"Box","Box","Blackman","SincFast","Jinc"})[torch.random(1, 5)]
  9. return iproc.scale(src, src:size(3) * 0.5, src:size(2) * 0.5, filter)
  10. else
  11. return src
  12. end
  13. end
  14. local function crop_if_large(src, max_size)
  15. local tries = 4
  16. if src:size(2) > max_size and src:size(3) > max_size then
  17. local rect
  18. for i = 1, tries do
  19. local yi = torch.random(0, src:size(2) - max_size)
  20. local xi = torch.random(0, src:size(3) - max_size)
  21. rect = iproc.crop(src, xi, yi, xi + max_size, yi + max_size)
  22. -- ignore simple background
  23. if rect:float():std() >= 0 then
  24. break
  25. end
  26. end
  27. return rect
  28. else
  29. return src
  30. end
  31. end
  32. local function preprocess(src, crop_size, options)
  33. local dest = src
  34. dest = random_half(dest, options.random_half_rate)
  35. dest = crop_if_large(dest, math.max(crop_size * 2, options.max_size))
  36. dest = data_augmentation.flip(dest)
  37. dest = data_augmentation.color_noise(dest, options.random_color_noise_rate)
  38. dest = data_augmentation.overlay(dest, options.random_overlay_rate)
  39. dest = data_augmentation.shift_1px(dest)
  40. return dest
  41. end
  42. local function active_cropping(x, y, size, p, tries)
  43. assert("x:size == y:size", x:size(2) == y:size(2) and x:size(3) == y:size(3))
  44. local r = torch.uniform()
  45. if p < r then
  46. local xi = torch.random(0, y:size(3) - (size + 1))
  47. local yi = torch.random(0, y:size(2) - (size + 1))
  48. local xc = iproc.crop(x, xi, yi, xi + size, yi + size)
  49. local yc = iproc.crop(y, xi, yi, xi + size, yi + size)
  50. return xc, yc
  51. else
  52. local samples = {}
  53. local best_se = 0.0
  54. local best_xc, best_yc
  55. local m = torch.FloatTensor(x:size(1), size, size)
  56. for i = 1, tries do
  57. local xi = torch.random(0, y:size(3) - (size + 1))
  58. local yi = torch.random(0, y:size(2) - (size + 1))
  59. local xc = iproc.crop(x, xi, yi, xi + size, yi + size)
  60. local yc = iproc.crop(y, xi, yi, xi + size, yi + size)
  61. local xcf = iproc.byte2float(xc)
  62. local ycf = iproc.byte2float(yc)
  63. local se = m:copy(xcf):add(-1.0, ycf):pow(2):sum()
  64. if se >= best_se then
  65. best_xc = xcf
  66. best_yc = ycf
  67. best_se = se
  68. end
  69. end
  70. return best_xc, best_yc
  71. end
  72. end
  73. function pairwise_transform.scale(src, scale, size, offset, n, options)
  74. local filters = {
  75. "Box","Box", -- 0.012756949974688
  76. "Blackman", -- 0.013191924552285
  77. --"Cartom", -- 0.013753536746706
  78. --"Hanning", -- 0.013761314529647
  79. --"Hermite", -- 0.013850225205266
  80. "SincFast", -- 0.014095824314306
  81. "Jinc", -- 0.014244299255442
  82. }
  83. local unstable_region_offset = 8
  84. local downscale_filter = filters[torch.random(1, #filters)]
  85. local y = preprocess(src, size, options)
  86. assert(y:size(2) % 4 == 0 and y:size(3) % 4 == 0)
  87. local down_scale = 1.0 / scale
  88. local x = iproc.scale(iproc.scale(y, y:size(3) * down_scale,
  89. y:size(2) * down_scale, downscale_filter),
  90. y:size(3), y:size(2))
  91. x = iproc.crop(x, unstable_region_offset, unstable_region_offset,
  92. x:size(3) - unstable_region_offset, x:size(2) - unstable_region_offset)
  93. y = iproc.crop(y, unstable_region_offset, unstable_region_offset,
  94. y:size(3) - unstable_region_offset, y:size(2) - unstable_region_offset)
  95. assert(x:size(2) % 4 == 0 and x:size(3) % 4 == 0)
  96. assert(x:size(1) == y:size(1) and x:size(2) == y:size(2) and x:size(3) == y:size(3))
  97. local batch = {}
  98. for i = 1, n do
  99. local xc, yc = active_cropping(x, y,
  100. size,
  101. options.active_cropping_rate,
  102. options.active_cropping_tries)
  103. xc = iproc.byte2float(xc)
  104. yc = iproc.byte2float(yc)
  105. if options.rgb then
  106. else
  107. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  108. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  109. end
  110. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  111. end
  112. return batch
  113. end
  114. function pairwise_transform.jpeg_(src, quality, size, offset, n, options)
  115. local unstable_region_offset = 8
  116. local y = preprocess(src, size, options)
  117. local x = y
  118. for i = 1, #quality do
  119. x = gm.Image(x, "RGB", "DHW")
  120. x:format("jpeg"):depth(8)
  121. if options.jpeg_sampling_factors == 444 then
  122. x:samplingFactors({1.0, 1.0, 1.0})
  123. else -- 420
  124. x:samplingFactors({2.0, 1.0, 1.0})
  125. end
  126. local blob, len = x:toBlob(quality[i])
  127. x:fromBlob(blob, len)
  128. x = x:toTensor("byte", "RGB", "DHW")
  129. end
  130. x = iproc.crop(x, unstable_region_offset, unstable_region_offset,
  131. x:size(3) - unstable_region_offset, x:size(2) - unstable_region_offset)
  132. y = iproc.crop(y, unstable_region_offset, unstable_region_offset,
  133. y:size(3) - unstable_region_offset, y:size(2) - unstable_region_offset)
  134. assert(x:size(2) % 4 == 0 and x:size(3) % 4 == 0)
  135. assert(x:size(1) == y:size(1) and x:size(2) == y:size(2) and x:size(3) == y:size(3))
  136. local batch = {}
  137. for i = 1, n do
  138. local xc, yc = active_cropping(x, y, size,
  139. options.active_cropping_rate,
  140. options.active_cropping_tries)
  141. xc = iproc.byte2float(xc)
  142. yc = iproc.byte2float(yc)
  143. if options.rgb then
  144. else
  145. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  146. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  147. end
  148. if torch.uniform() < options.nr_rate then
  149. -- reductiong noise
  150. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  151. else
  152. -- ratain useful details
  153. table.insert(batch, {yc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  154. end
  155. end
  156. return batch
  157. end
  158. function pairwise_transform.jpeg(src, style, level, size, offset, n, options)
  159. if style == "art" then
  160. if level == 1 then
  161. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  162. size, offset, n, options)
  163. elseif level == 2 then
  164. local r = torch.uniform()
  165. if r > 0.6 then
  166. return pairwise_transform.jpeg_(src, {torch.random(27, 70)},
  167. size, offset, n, options)
  168. elseif r > 0.3 then
  169. local quality1 = torch.random(37, 70)
  170. local quality2 = quality1 - torch.random(5, 10)
  171. return pairwise_transform.jpeg_(src, {quality1, quality2},
  172. size, offset, n, options)
  173. else
  174. local quality1 = torch.random(52, 70)
  175. local quality2 = quality1 - torch.random(5, 15)
  176. local quality3 = quality1 - torch.random(15, 25)
  177. return pairwise_transform.jpeg_(src,
  178. {quality1, quality2, quality3},
  179. size, offset, n, options)
  180. end
  181. else
  182. error("unknown noise level: " .. level)
  183. end
  184. elseif style == "photo" then
  185. if level == 1 then
  186. return pairwise_transform.jpeg_(src, {torch.random(80, 95)},
  187. size, offset, n,
  188. options)
  189. elseif level == 2 then
  190. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  191. size, offset, n,
  192. options)
  193. else
  194. error("unknown noise level: " .. level)
  195. end
  196. else
  197. error("unknown style: " .. style)
  198. end
  199. end
  200. function pairwise_transform.test_jpeg(src)
  201. local options = {color_noise = true,
  202. random_half = true,
  203. overlay = true,
  204. active_cropping_rate = 0.5,
  205. active_cropping_tries = 10,
  206. rgb = true
  207. }
  208. for i = 1, 9 do
  209. local xy = pairwise_transform.jpeg(src,
  210. "art",
  211. torch.random(1, 2),
  212. 128, 7, 1, options)
  213. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min=0, max=1})
  214. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min=0, max=1})
  215. end
  216. end
  217. function pairwise_transform.test_scale(src)
  218. local options = {color_noise = true,
  219. random_half = true,
  220. overlay = true,
  221. active_cropping_rate = 0.5,
  222. active_cropping_tries = 10,
  223. rgb = true
  224. }
  225. for i = 1, 10 do
  226. local xy = pairwise_transform.scale(src, 2.0, 128, 7, 1, options)
  227. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
  228. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
  229. end
  230. end
  231. return pairwise_transform