pairwise_transform_jpeg_scale.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. local pairwise_utils = require 'pairwise_transform_utils'
  2. local iproc = require 'iproc'
  3. local gm = require 'graphicsmagick'
  4. local pairwise_transform = {}
  5. local function add_jpeg_noise_(x, quality, options)
  6. for i = 1, #quality do
  7. x = gm.Image(x, "RGB", "DHW")
  8. x:format("jpeg"):depth(8)
  9. if torch.uniform() < options.jpeg_chroma_subsampling_rate then
  10. -- YUV 420
  11. x:samplingFactors({2.0, 1.0, 1.0})
  12. else
  13. -- YUV 444
  14. x:samplingFactors({1.0, 1.0, 1.0})
  15. end
  16. local blob, len = x:toBlob(quality[i])
  17. x:fromBlob(blob, len)
  18. x = x:toTensor("byte", "RGB", "DHW")
  19. end
  20. return x
  21. end
  22. local function add_jpeg_noise(src, style, level, options)
  23. if style == "art" then
  24. if level == 1 then
  25. return add_jpeg_noise_(src, {torch.random(65, 85)}, options)
  26. elseif level == 2 or level == 3 then
  27. -- level 2/3 adjusting by -nr_rate. for level3, -nr_rate=1
  28. local r = torch.uniform()
  29. if r > 0.4 then
  30. return add_jpeg_noise_(src, {torch.random(27, 70)}, options)
  31. elseif r > 0.1 then
  32. local quality1 = torch.random(37, 70)
  33. local quality2 = quality1 - torch.random(5, 10)
  34. return add_jpeg_noise_(src, {quality1, quality2}, options)
  35. else
  36. local quality1 = torch.random(52, 70)
  37. local quality2 = quality1 - torch.random(5, 15)
  38. local quality3 = quality1 - torch.random(15, 25)
  39. return add_jpeg_noise_(src, {quality1, quality2, quality3}, options)
  40. end
  41. else
  42. error("unknown noise level: " .. level)
  43. end
  44. elseif style == "photo" then
  45. -- level adjusting by -nr_rate
  46. return add_jpeg_noise_(src, {torch.random(30, 70)}, options)
  47. else
  48. error("unknown style: " .. style)
  49. end
  50. end
  51. function pairwise_transform.jpeg_scale(src, scale, style, noise_level, size, offset, n, options)
  52. local filters = options.downsampling_filters
  53. if options.data.filters then
  54. filters = options.data.filters
  55. end
  56. local unstable_region_offset = 8
  57. local downsampling_filter = filters[torch.random(1, #filters)]
  58. local blur = torch.uniform(options.resize_blur_min, options.resize_blur_max)
  59. local y = pairwise_utils.preprocess(src, size, options)
  60. assert(y:size(2) % 4 == 0 and y:size(3) % 4 == 0)
  61. local down_scale = 1.0 / scale
  62. local x
  63. local small = iproc.scale(y, y:size(3) * down_scale,
  64. y:size(2) * down_scale, downsampling_filter, blur)
  65. if options.x_upsampling then
  66. x = iproc.scale(small, y:size(3), y:size(2), "Box")
  67. else
  68. x = small
  69. end
  70. local scale_inner = scale
  71. if options.x_upsampling then
  72. scale_inner = 1
  73. end
  74. x = iproc.crop(x, unstable_region_offset, unstable_region_offset,
  75. x:size(3) - unstable_region_offset, x:size(2) - unstable_region_offset)
  76. y = iproc.crop(y, unstable_region_offset * scale_inner, unstable_region_offset * scale_inner,
  77. y:size(3) - unstable_region_offset * scale_inner, y:size(2) - unstable_region_offset * scale_inner)
  78. if options.x_upsampling then
  79. assert(x:size(2) % 4 == 0 and x:size(3) % 4 == 0)
  80. assert(x:size(1) == y:size(1) and x:size(2) == y:size(2) and x:size(3) == y:size(3))
  81. else
  82. assert(x:size(1) == y:size(1) and x:size(2) * scale == y:size(2) and x:size(3) * scale == y:size(3))
  83. end
  84. local batch = {}
  85. local lowres_y = gm.Image(y, "RGB", "DHW"):
  86. size(y:size(3) * 0.5, y:size(2) * 0.5, "Box"):
  87. size(y:size(3), y:size(2), "Box"):
  88. toTensor(t, "RGB", "DHW")
  89. local xs = {}
  90. local ns = {}
  91. local ys = {}
  92. local lowreses = {}
  93. for j = 1, 2 do
  94. -- TTA
  95. local xi, yi, ri
  96. if j == 1 then
  97. xi = x
  98. yi = y
  99. ri = lowres_y
  100. else
  101. xi = x:transpose(2, 3):contiguous()
  102. yi = y:transpose(2, 3):contiguous()
  103. ri = lowres_y:transpose(2, 3):contiguous()
  104. end
  105. local xv = image.vflip(xi)
  106. local yv = image.vflip(yi)
  107. local rv = image.vflip(ri)
  108. table.insert(xs, xi)
  109. table.insert(ys, yi)
  110. table.insert(lowreses, ri)
  111. table.insert(xs, xv)
  112. table.insert(ys, yv)
  113. table.insert(lowreses, rv)
  114. table.insert(xs, image.hflip(xi))
  115. table.insert(ys, image.hflip(yi))
  116. table.insert(lowreses, image.hflip(ri))
  117. table.insert(xs, image.hflip(xv))
  118. table.insert(ys, image.hflip(yv))
  119. table.insert(lowreses, image.hflip(rv))
  120. end
  121. for i = 1, n do
  122. local t = (i % #xs) + 1
  123. local xc, yc
  124. if torch.uniform() < options.nr_rate then
  125. -- scale + noise reduction
  126. if not ns[t] then
  127. ns[t] = add_jpeg_noise(xs[t], style, noise_level, options)
  128. end
  129. xc, yc = pairwise_utils.active_cropping(ns[t], ys[t], lowreses[t],
  130. size,
  131. scale_inner,
  132. options.active_cropping_rate,
  133. options.active_cropping_tries)
  134. else
  135. -- scale
  136. xc, yc = pairwise_utils.active_cropping(xs[t], ys[t], lowreses[t],
  137. size,
  138. scale_inner,
  139. options.active_cropping_rate,
  140. options.active_cropping_tries)
  141. end
  142. xc = iproc.byte2float(xc)
  143. yc = iproc.byte2float(yc)
  144. if options.rgb then
  145. else
  146. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  147. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  148. end
  149. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  150. end
  151. return batch
  152. end
  153. function pairwise_transform.test_jpeg_scale(src)
  154. torch.setdefaulttensortype("torch.FloatTensor")
  155. local options = {random_color_noise_rate = 0.5,
  156. random_half_rate = 0.5,
  157. random_overlay_rate = 0.5,
  158. random_unsharp_mask_rate = 0.5,
  159. active_cropping_rate = 0.5,
  160. active_cropping_tries = 10,
  161. max_size = 256,
  162. x_upsampling = false,
  163. downsampling_filters = "Box",
  164. rgb = true
  165. }
  166. local image = require 'image'
  167. local src = image.lena()
  168. for i = 1, 10 do
  169. local xy = pairwise_transform.jpeg_scale(src, 2.0, "art", 1, 128, 7, 1, options)
  170. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
  171. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
  172. end
  173. end
  174. return pairwise_transform