pairwise_transform_jpeg_scale.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.6 then
  30. return add_jpeg_noise_(src, {torch.random(27, 70)}, options)
  31. elseif r > 0.3 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. if options.gamma_correction then
  64. local small = iproc.scale_with_gamma22(y, y:size(3) * down_scale,
  65. y:size(2) * down_scale, downsampling_filter, blur)
  66. if options.x_upsampling then
  67. x = iproc.scale(small, y:size(3), y:size(2), options.upsampling_filter)
  68. else
  69. x = small
  70. end
  71. else
  72. local small = iproc.scale(y, y:size(3) * down_scale,
  73. y:size(2) * down_scale, downsampling_filter, blur)
  74. if options.x_upsampling then
  75. x = iproc.scale(small, y:size(3), y:size(2), options.upsampling_filter)
  76. else
  77. x = small
  78. end
  79. end
  80. x = add_jpeg_noise(x, style, noise_level, options)
  81. local scale_inner = scale
  82. if options.x_upsampling then
  83. scale_inner = 1
  84. end
  85. x = iproc.crop(x, unstable_region_offset, unstable_region_offset,
  86. x:size(3) - unstable_region_offset, x:size(2) - unstable_region_offset)
  87. y = iproc.crop(y, unstable_region_offset * scale_inner, unstable_region_offset * scale_inner,
  88. y:size(3) - unstable_region_offset * scale_inner, y:size(2) - unstable_region_offset * scale_inner)
  89. if options.x_upsampling then
  90. assert(x:size(2) % 4 == 0 and x:size(3) % 4 == 0)
  91. assert(x:size(1) == y:size(1) and x:size(2) == y:size(2) and x:size(3) == y:size(3))
  92. else
  93. assert(x:size(1) == y:size(1) and x:size(2) * scale == y:size(2) and x:size(3) * scale == y:size(3))
  94. end
  95. local batch = {}
  96. local lowres_y = gm.Image(y, "RGB", "DHW"):
  97. size(y:size(3) * 0.5, y:size(2) * 0.5, "Box"):
  98. size(y:size(3), y:size(2), "Box"):
  99. toTensor(t, "RGB", "DHW")
  100. local xs = {}
  101. local ys = {}
  102. local lowreses = {}
  103. for j = 1, 2 do
  104. -- TTA
  105. local xi, yi, ri
  106. if j == 1 then
  107. xi = x
  108. yi = y
  109. ri = lowres_y
  110. else
  111. xi = x:transpose(2, 3):contiguous()
  112. yi = y:transpose(2, 3):contiguous()
  113. ri = lowres_y:transpose(2, 3):contiguous()
  114. end
  115. local xv = image.vflip(xi)
  116. local yv = image.vflip(yi)
  117. local rv = image.vflip(ri)
  118. table.insert(xs, xi)
  119. table.insert(ys, yi)
  120. table.insert(lowreses, ri)
  121. table.insert(xs, xv)
  122. table.insert(ys, yv)
  123. table.insert(lowreses, rv)
  124. table.insert(xs, image.hflip(xi))
  125. table.insert(ys, image.hflip(yi))
  126. table.insert(lowreses, image.hflip(ri))
  127. table.insert(xs, image.hflip(xv))
  128. table.insert(ys, image.hflip(yv))
  129. table.insert(lowreses, image.hflip(rv))
  130. end
  131. for i = 1, n do
  132. local t = (i % #xs) + 1
  133. local xc, yc = pairwise_utils.active_cropping(xs[t], ys[t], lowreses[t],
  134. size,
  135. scale_inner,
  136. options.active_cropping_rate,
  137. options.active_cropping_tries)
  138. xc = iproc.byte2float(xc)
  139. yc = iproc.byte2float(yc)
  140. if options.rgb then
  141. else
  142. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  143. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  144. end
  145. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  146. end
  147. return batch
  148. end
  149. function pairwise_transform.test_jpeg_scale(src)
  150. torch.setdefaulttensortype("torch.FloatTensor")
  151. local options = {random_color_noise_rate = 0.5,
  152. random_half_rate = 0.5,
  153. random_overlay_rate = 0.5,
  154. random_unsharp_mask_rate = 0.5,
  155. active_cropping_rate = 0.5,
  156. active_cropping_tries = 10,
  157. max_size = 256,
  158. x_upsampling = false,
  159. downsampling_filters = "Box",
  160. rgb = true
  161. }
  162. local image = require 'image'
  163. local src = image.lena()
  164. for i = 1, 10 do
  165. local xy = pairwise_transform.jpeg_scale(src, 2.0, "art", 1, 128, 7, 1, options)
  166. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
  167. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
  168. end
  169. end
  170. return pairwise_transform