pairwise_transform.lua 8.9 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, filters)
  7. if torch.uniform() < p then
  8. local filter = filters[torch.random(1, #filters)]
  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, options.downsampling_filters)
  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
  89. if options.gamma_correction then
  90. x = iproc.scale(iproc.scale_with_gamma22(y, y:size(3) * down_scale,
  91. y:size(2) * down_scale, downsampling_filter),
  92. y:size(3), y:size(2), options.upsampling_filter)
  93. else
  94. x = iproc.scale(iproc.scale(y, y:size(3) * down_scale,
  95. y:size(2) * down_scale, downsampling_filter),
  96. y:size(3), y:size(2), options.upsampling_filter)
  97. end
  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 or level == 3 then
  173. -- level 2/3 adjusting by -nr_rate. for level3, -nr_rate=1
  174. local r = torch.uniform()
  175. if r > 0.6 then
  176. return pairwise_transform.jpeg_(src, {torch.random(27, 70)},
  177. size, offset, n, options)
  178. elseif r > 0.3 then
  179. local quality1 = torch.random(37, 70)
  180. local quality2 = quality1 - torch.random(5, 10)
  181. return pairwise_transform.jpeg_(src, {quality1, quality2},
  182. size, offset, n, options)
  183. else
  184. local quality1 = torch.random(52, 70)
  185. local quality2 = quality1 - torch.random(5, 15)
  186. local quality3 = quality1 - torch.random(15, 25)
  187. return pairwise_transform.jpeg_(src,
  188. {quality1, quality2, quality3},
  189. size, offset, n, options)
  190. end
  191. else
  192. error("unknown noise level: " .. level)
  193. end
  194. elseif style == "photo" then
  195. -- level adjusting by -nr_rate
  196. return pairwise_transform.jpeg_(src, {torch.random(30, 70)},
  197. size, offset, n,
  198. options)
  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