pairwise_transform.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. "Box","Box", -- 0.012756949974688
  76. "Blackman", -- 0.013191924552285
  77. --"Cartom", -- 0.013753536746706
  78. --"Hanning", -- 0.013761314529647
  79. --"Hermite", -- 0.013850225205266
  80. "Sinc", -- 0.014095824314306
  81. "Lanczos", -- 0.014244299255442
  82. "Catrom"
  83. }
  84. local unstable_region_offset = 8
  85. local downscale_filter = filters[torch.random(1, #filters)]
  86. local y = preprocess(src, size, options)
  87. assert(y:size(2) % 4 == 0 and y:size(3) % 4 == 0)
  88. local down_scale = 1.0 / scale
  89. local x = iproc.scale(iproc.scale(y, y:size(3) * down_scale,
  90. y:size(2) * down_scale, downscale_filter),
  91. y:size(3), y:size(2))
  92. x = iproc.crop(x, unstable_region_offset, unstable_region_offset,
  93. x:size(3) - unstable_region_offset, x:size(2) - unstable_region_offset)
  94. y = iproc.crop(y, unstable_region_offset, unstable_region_offset,
  95. y:size(3) - unstable_region_offset, y:size(2) - unstable_region_offset)
  96. assert(x:size(2) % 4 == 0 and x:size(3) % 4 == 0)
  97. assert(x:size(1) == y:size(1) and x:size(2) == y:size(2) and x:size(3) == y:size(3))
  98. local batch = {}
  99. for i = 1, n do
  100. local xc, yc = active_cropping(x, y,
  101. size,
  102. options.active_cropping_rate,
  103. options.active_cropping_tries)
  104. xc = iproc.byte2float(xc)
  105. yc = iproc.byte2float(yc)
  106. if options.rgb then
  107. else
  108. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  109. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  110. end
  111. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  112. end
  113. return batch
  114. end
  115. function pairwise_transform.jpeg_(src, quality, size, offset, n, options)
  116. local unstable_region_offset = 8
  117. local y = preprocess(src, size, options)
  118. local x = y
  119. for i = 1, #quality do
  120. x = gm.Image(x, "RGB", "DHW")
  121. x:format("jpeg"):depth(8)
  122. if torch.uniform() < options.jpeg_chroma_subsampling_rate then
  123. -- YUV 420
  124. x:samplingFactors({2.0, 1.0, 1.0})
  125. else
  126. -- YUV 444
  127. x:samplingFactors({1.0, 1.0, 1.0})
  128. end
  129. local blob, len = x:toBlob(quality[i])
  130. x:fromBlob(blob, len)
  131. x = x:toTensor("byte", "RGB", "DHW")
  132. end
  133. x = iproc.crop(x, unstable_region_offset, unstable_region_offset,
  134. x:size(3) - unstable_region_offset, x:size(2) - unstable_region_offset)
  135. y = iproc.crop(y, unstable_region_offset, unstable_region_offset,
  136. y:size(3) - unstable_region_offset, y:size(2) - unstable_region_offset)
  137. assert(x:size(2) % 4 == 0 and x:size(3) % 4 == 0)
  138. assert(x:size(1) == y:size(1) and x:size(2) == y:size(2) and x:size(3) == y:size(3))
  139. local batch = {}
  140. for i = 1, n do
  141. local xc, yc = active_cropping(x, y, size,
  142. options.active_cropping_rate,
  143. options.active_cropping_tries)
  144. xc = iproc.byte2float(xc)
  145. yc = iproc.byte2float(yc)
  146. if options.rgb then
  147. else
  148. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  149. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  150. end
  151. if torch.uniform() < options.nr_rate then
  152. -- reducing noise
  153. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  154. else
  155. -- ratain useful details
  156. table.insert(batch, {yc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  157. end
  158. end
  159. return batch
  160. end
  161. function pairwise_transform.jpeg(src, style, level, size, offset, n, options)
  162. if style == "art" then
  163. if level == 1 then
  164. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  165. size, offset, n, options)
  166. elseif level == 2 then
  167. local r = torch.uniform()
  168. if r > 0.6 then
  169. return pairwise_transform.jpeg_(src, {torch.random(27, 70)},
  170. size, offset, n, options)
  171. elseif r > 0.3 then
  172. local quality1 = torch.random(37, 70)
  173. local quality2 = quality1 - torch.random(5, 10)
  174. return pairwise_transform.jpeg_(src, {quality1, quality2},
  175. size, offset, n, options)
  176. else
  177. local quality1 = torch.random(52, 70)
  178. local quality2 = quality1 - torch.random(5, 15)
  179. local quality3 = quality1 - torch.random(15, 25)
  180. return pairwise_transform.jpeg_(src,
  181. {quality1, quality2, quality3},
  182. size, offset, n, options)
  183. end
  184. else
  185. error("unknown noise level: " .. level)
  186. end
  187. elseif style == "photo" then
  188. if level == 1 then
  189. return pairwise_transform.jpeg_(src, {torch.random(70, 90)},
  190. size, offset, n,
  191. options)
  192. elseif level == 2 then
  193. return pairwise_transform.jpeg_(src, {torch.random(50, 70)},
  194. size, offset, n,
  195. options)
  196. else
  197. error("unknown noise level: " .. level)
  198. end
  199. else
  200. error("unknown style: " .. style)
  201. end
  202. end
  203. function pairwise_transform.test_jpeg(src)
  204. torch.setdefaulttensortype("torch.FloatTensor")
  205. local options = {random_color_noise_rate = 0.5,
  206. random_half_rate = 0.5,
  207. random_overlay_rate = 0.5,
  208. random_unsharp_mask_rate = 0.5,
  209. jpeg_chroma_subsampling_rate = 0.5,
  210. nr_rate = 1.0,
  211. active_cropping_rate = 0.5,
  212. active_cropping_tries = 10,
  213. max_size = 256,
  214. rgb = true
  215. }
  216. local image = require 'image'
  217. local src = image.lena()
  218. for i = 1, 9 do
  219. local xy = pairwise_transform.jpeg(src,
  220. "art",
  221. torch.random(1, 2),
  222. 128, 7, 1, options)
  223. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min=0, max=1})
  224. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min=0, max=1})
  225. end
  226. end
  227. function pairwise_transform.test_scale(src)
  228. torch.setdefaulttensortype("torch.FloatTensor")
  229. local options = {random_color_noise_rate = 0.5,
  230. random_half_rate = 0.5,
  231. random_overlay_rate = 0.5,
  232. random_unsharp_mask_rate = 0.5,
  233. active_cropping_rate = 0.5,
  234. active_cropping_tries = 10,
  235. max_size = 256,
  236. rgb = true
  237. }
  238. local image = require 'image'
  239. local src = image.lena()
  240. for i = 1, 10 do
  241. local xy = pairwise_transform.scale(src, 2.0, 128, 7, 1, options)
  242. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
  243. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
  244. end
  245. end
  246. return pairwise_transform