pairwise_transform.lua 8.5 KB

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