pairwise_transform_jpeg_scale.lua 5.2 KB

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