pairwise_transform_jpeg_scale.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 == 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 x_noise = add_jpeg_noise(x, style, noise_level, options)
  90. local xs, ys, ls, ns = pairwise_utils.flip_augmentation(x, y, lowres_y, x_noise)
  91. for i = 1, n do
  92. local t = (i % #xs) + 1
  93. local xc, yc
  94. if torch.uniform() < options.nr_rate then
  95. -- scale + noise reduction
  96. xc, yc = pairwise_utils.active_cropping(ns[t], ys[t], ls[t],
  97. size,
  98. scale_inner,
  99. options.active_cropping_rate,
  100. options.active_cropping_tries)
  101. else
  102. -- scale
  103. xc, yc = pairwise_utils.active_cropping(xs[t], ys[t], ls[t],
  104. size,
  105. scale_inner,
  106. options.active_cropping_rate,
  107. options.active_cropping_tries)
  108. end
  109. xc = iproc.byte2float(xc)
  110. yc = iproc.byte2float(yc)
  111. if options.rgb then
  112. else
  113. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  114. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  115. end
  116. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  117. end
  118. return batch
  119. end
  120. function pairwise_transform.test_jpeg_scale(src)
  121. torch.setdefaulttensortype("torch.FloatTensor")
  122. local options = {random_color_noise_rate = 0.5,
  123. random_half_rate = 0.5,
  124. random_overlay_rate = 0.5,
  125. random_unsharp_mask_rate = 0.5,
  126. active_cropping_rate = 0.5,
  127. active_cropping_tries = 10,
  128. max_size = 256,
  129. x_upsampling = false,
  130. downsampling_filters = "Box",
  131. rgb = true
  132. }
  133. local image = require 'image'
  134. local src = image.lena()
  135. for i = 1, 10 do
  136. local xy = pairwise_transform.jpeg_scale(src, 2.0, "art", 1, 128, 7, 1, options)
  137. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
  138. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
  139. end
  140. end
  141. return pairwise_transform