pairwise_transform.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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","Sinc","Lanczos", "Catrom"})[torch.random(1, 6)]
  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.unsharp_mask(dest, options.random_unsharp_mask_rate)
  40. dest = data_augmentation.shift_1px(dest)
  41. return dest
  42. end
  43. local function active_cropping(x, y, size, p, tries)
  44. assert("x:size == y:size", x:size(2) == y:size(2) and x:size(3) == y:size(3))
  45. local r = torch.uniform()
  46. if p < r then
  47. local xi = torch.random(0, y:size(3) - (size + 1))
  48. local yi = torch.random(0, y:size(2) - (size + 1))
  49. local xc = iproc.crop(x, xi, yi, xi + size, yi + size)
  50. local yc = iproc.crop(y, xi, yi, xi + size, yi + size)
  51. return xc, yc
  52. else
  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. if options.style == "photo" then
  76. filters = {
  77. "Box", "lanczos", "Catrom"
  78. }
  79. else
  80. filters = {
  81. "Box","Box", -- 0.012756949974688
  82. "Blackman", -- 0.013191924552285
  83. --"Catrom", -- 0.013753536746706
  84. --"Hanning", -- 0.013761314529647
  85. --"Hermite", -- 0.013850225205266
  86. "Sinc", -- 0.014095824314306
  87. "Lanczos", -- 0.014244299255442
  88. }
  89. end
  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 torch.uniform() < options.jpeg_chroma_subsampling_rate then
  129. -- YUV 420
  130. x:samplingFactors({2.0, 1.0, 1.0})
  131. else
  132. -- YUV 444
  133. x:samplingFactors({1.0, 1.0, 1.0})
  134. end
  135. local blob, len = x:toBlob(quality[i])
  136. x:fromBlob(blob, len)
  137. x = x:toTensor("byte", "RGB", "DHW")
  138. end
  139. x = iproc.crop(x, unstable_region_offset, unstable_region_offset,
  140. x:size(3) - unstable_region_offset, x:size(2) - unstable_region_offset)
  141. y = iproc.crop(y, unstable_region_offset, unstable_region_offset,
  142. y:size(3) - unstable_region_offset, y:size(2) - unstable_region_offset)
  143. assert(x:size(2) % 4 == 0 and x:size(3) % 4 == 0)
  144. assert(x:size(1) == y:size(1) and x:size(2) == y:size(2) and x:size(3) == y:size(3))
  145. local batch = {}
  146. for i = 1, n do
  147. local xc, yc = active_cropping(x, y, size,
  148. options.active_cropping_rate,
  149. options.active_cropping_tries)
  150. xc = iproc.byte2float(xc)
  151. yc = iproc.byte2float(yc)
  152. if options.rgb then
  153. else
  154. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  155. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  156. end
  157. if torch.uniform() < options.nr_rate then
  158. -- reducing noise
  159. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  160. else
  161. -- ratain useful details
  162. table.insert(batch, {yc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  163. end
  164. end
  165. return batch
  166. end
  167. function pairwise_transform.jpeg(src, style, level, size, offset, n, options)
  168. if style == "art" then
  169. if level == 1 then
  170. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  171. size, offset, n, options)
  172. elseif level == 2 then
  173. local r = torch.uniform()
  174. if r > 0.6 then
  175. return pairwise_transform.jpeg_(src, {torch.random(27, 70)},
  176. size, offset, n, options)
  177. elseif r > 0.3 then
  178. local quality1 = torch.random(37, 70)
  179. local quality2 = quality1 - torch.random(5, 10)
  180. return pairwise_transform.jpeg_(src, {quality1, quality2},
  181. size, offset, n, options)
  182. else
  183. local quality1 = torch.random(52, 70)
  184. local quality2 = quality1 - torch.random(5, 15)
  185. local quality3 = quality1 - torch.random(15, 25)
  186. return pairwise_transform.jpeg_(src,
  187. {quality1, quality2, quality3},
  188. size, offset, n, options)
  189. end
  190. else
  191. error("unknown noise level: " .. level)
  192. end
  193. elseif style == "photo" then
  194. if level == 1 then
  195. return pairwise_transform.jpeg_(src, {torch.random(70, 90)},
  196. size, offset, n,
  197. options)
  198. elseif level == 2 then
  199. return pairwise_transform.jpeg_(src, {torch.random(50, 70)},
  200. size, offset, n,
  201. options)
  202. else
  203. error("unknown noise level: " .. level)
  204. end
  205. else
  206. error("unknown style: " .. style)
  207. end
  208. end
  209. function pairwise_transform.test_jpeg(src)
  210. torch.setdefaulttensortype("torch.FloatTensor")
  211. local options = {random_color_noise_rate = 0.5,
  212. random_half_rate = 0.5,
  213. random_overlay_rate = 0.5,
  214. random_unsharp_mask_rate = 0.5,
  215. jpeg_chroma_subsampling_rate = 0.5,
  216. nr_rate = 1.0,
  217. active_cropping_rate = 0.5,
  218. active_cropping_tries = 10,
  219. max_size = 256,
  220. rgb = true
  221. }
  222. local image = require 'image'
  223. local src = image.lena()
  224. for i = 1, 9 do
  225. local xy = pairwise_transform.jpeg(src,
  226. "art",
  227. torch.random(1, 2),
  228. 128, 7, 1, options)
  229. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min=0, max=1})
  230. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min=0, max=1})
  231. end
  232. end
  233. function pairwise_transform.test_scale(src)
  234. torch.setdefaulttensortype("torch.FloatTensor")
  235. local options = {random_color_noise_rate = 0.5,
  236. random_half_rate = 0.5,
  237. random_overlay_rate = 0.5,
  238. random_unsharp_mask_rate = 0.5,
  239. active_cropping_rate = 0.5,
  240. active_cropping_tries = 10,
  241. max_size = 256,
  242. rgb = true
  243. }
  244. local image = require 'image'
  245. local src = image.lena()
  246. for i = 1, 10 do
  247. local xy = pairwise_transform.scale(src, 2.0, 128, 7, 1, options)
  248. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
  249. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
  250. end
  251. end
  252. return pairwise_transform