pairwise_transform_scale.lua 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. if xc:size(1) > 1 then
  54. yc = iproc.rgb2y(yc)
  55. xc = iproc.rgb2y(xc)
  56. end
  57. end
  58. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  59. end
  60. return batch
  61. end
  62. function pairwise_transform.test_scale(src)
  63. torch.setdefaulttensortype("torch.FloatTensor")
  64. local options = {random_color_noise_rate = 0.5,
  65. random_half_rate = 0.5,
  66. random_overlay_rate = 0.5,
  67. random_unsharp_mask_rate = 0.5,
  68. active_cropping_rate = 0.5,
  69. active_cropping_tries = 10,
  70. max_size = 256,
  71. x_upsampling = false,
  72. downsampling_filters = "Box",
  73. rgb = true
  74. }
  75. local image = require 'image'
  76. local src = image.lena()
  77. for i = 1, 10 do
  78. local xy = pairwise_transform.scale(src, 2.0, 128, 7, 1, options)
  79. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
  80. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
  81. end
  82. end
  83. return pairwise_transform