pairwise_transform_scale.lua 3.1 KB

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