pairwise_transform_scale.lua 3.1 KB

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