pairwise_transform_scale.lua 3.4 KB

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