pairwise_transform_jpeg_scale.lua 5.2 KB

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