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