pairwise_transform_scale.lua 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. local pairwise_utils = require 'pairwise_transform_utils'
  2. local iproc = require 'iproc'
  3. local pairwise_transform = {}
  4. function pairwise_transform.scale(src, scale, size, offset, n, options)
  5. local filters = options.downsampling_filters
  6. local unstable_region_offset = 8
  7. local downsampling_filter = filters[torch.random(1, #filters)]
  8. local y = pairwise_utils.preprocess(src, size, options)
  9. assert(y:size(2) % 4 == 0 and y:size(3) % 4 == 0)
  10. local down_scale = 1.0 / scale
  11. local x
  12. if options.gamma_correction then
  13. local small = iproc.scale_with_gamma22(y, y:size(3) * down_scale,
  14. y:size(2) * down_scale, downsampling_filter)
  15. if options.x_upsampling then
  16. x = iproc.scale(small, y:size(3), y:size(2), options.upsampling_filter)
  17. else
  18. x = small
  19. end
  20. else
  21. local small = iproc.scale(y, y:size(3) * down_scale,
  22. y:size(2) * down_scale, downsampling_filter)
  23. if options.x_upsampling then
  24. x = iproc.scale(small, y:size(3), y:size(2), options.upsampling_filter)
  25. else
  26. x = small
  27. end
  28. end
  29. if options.x_upsampling then
  30. x = iproc.crop(x, unstable_region_offset, unstable_region_offset,
  31. x:size(3) - unstable_region_offset, x:size(2) - unstable_region_offset)
  32. y = iproc.crop(y, unstable_region_offset, unstable_region_offset,
  33. y:size(3) - unstable_region_offset, y:size(2) - unstable_region_offset)
  34. assert(x:size(2) % 4 == 0 and x:size(3) % 4 == 0)
  35. assert(x:size(1) == y:size(1) and x:size(2) == y:size(2) and x:size(3) == y:size(3))
  36. else
  37. assert(x:size(1) == y:size(1) and x:size(2) * scale == y:size(2) and x:size(3) * scale == y:size(3))
  38. end
  39. local scale_inner = scale
  40. if options.x_upsampling then
  41. scale_inner = 1
  42. end
  43. local batch = {}
  44. for i = 1, n do
  45. local xc, yc = pairwise_utils.active_cropping(x, y,
  46. size,
  47. scale_inner,
  48. options.active_cropping_rate,
  49. options.active_cropping_tries)
  50. xc = iproc.byte2float(xc)
  51. yc = iproc.byte2float(yc)
  52. if options.rgb then
  53. else
  54. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  55. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  56. end
  57. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  58. end
  59. return batch
  60. end
  61. function pairwise_transform.test_scale(src)
  62. torch.setdefaulttensortype("torch.FloatTensor")
  63. local options = {random_color_noise_rate = 0.5,
  64. random_half_rate = 0.5,
  65. random_overlay_rate = 0.5,
  66. random_unsharp_mask_rate = 0.5,
  67. active_cropping_rate = 0.5,
  68. active_cropping_tries = 10,
  69. max_size = 256,
  70. x_upsampling = false,
  71. downsampling_filters = "Box",
  72. rgb = true
  73. }
  74. local image = require 'image'
  75. local src = image.lena()
  76. for i = 1, 10 do
  77. local xy = pairwise_transform.scale(src, 2.0, 128, 7, 1, options)
  78. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
  79. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
  80. end
  81. end
  82. return pairwise_transform