pairwise_transform.lua 8.2 KB

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