pairwise_transform.lua 8.6 KB

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