pairwise_transform_scale.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 = gm.Image(y, "RGB", "DHW"):
  40. size(y:size(3) * 0.5, y:size(2) * 0.5, "Box"):
  41. size(y:size(3), y:size(2), "Box"):
  42. toTensor(t, "RGB", "DHW")
  43. local xs = {}
  44. local ys = {}
  45. local lowreses = {}
  46. for j = 1, 2 do
  47. -- TTA
  48. local xi, yi, ri
  49. if j == 1 then
  50. xi = x
  51. yi = y
  52. ri = lowres_y
  53. else
  54. xi = x:transpose(2, 3):contiguous()
  55. yi = y:transpose(2, 3):contiguous()
  56. ri = lowres_y:transpose(2, 3):contiguous()
  57. end
  58. local xv = image.vflip(xi)
  59. local yv = image.vflip(yi)
  60. local rv = image.vflip(ri)
  61. table.insert(xs, xi)
  62. table.insert(ys, yi)
  63. table.insert(lowreses, ri)
  64. table.insert(xs, xv)
  65. table.insert(ys, yv)
  66. table.insert(lowreses, rv)
  67. table.insert(xs, image.hflip(xi))
  68. table.insert(ys, image.hflip(yi))
  69. table.insert(lowreses, image.hflip(ri))
  70. table.insert(xs, image.hflip(xv))
  71. table.insert(ys, image.hflip(yv))
  72. table.insert(lowreses, image.hflip(rv))
  73. end
  74. for i = 1, n do
  75. local t = (i % #xs) + 1
  76. local xc, yc = pairwise_utils.active_cropping(xs[t], ys[t], lowreses[t],
  77. size,
  78. scale_inner,
  79. options.active_cropping_rate,
  80. options.active_cropping_tries)
  81. xc = iproc.byte2float(xc)
  82. yc = iproc.byte2float(yc)
  83. if options.rgb then
  84. else
  85. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  86. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  87. end
  88. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  89. end
  90. return batch
  91. end
  92. function pairwise_transform.test_scale(src)
  93. torch.setdefaulttensortype("torch.FloatTensor")
  94. local options = {random_color_noise_rate = 0.5,
  95. random_half_rate = 0.5,
  96. random_overlay_rate = 0.5,
  97. random_unsharp_mask_rate = 0.5,
  98. active_cropping_rate = 0.5,
  99. active_cropping_tries = 10,
  100. max_size = 256,
  101. x_upsampling = false,
  102. downsampling_filters = "Box",
  103. rgb = true
  104. }
  105. local image = require 'image'
  106. local src = image.lena()
  107. for i = 1, 10 do
  108. local xy = pairwise_transform.scale(src, 2.0, 128, 7, 1, options)
  109. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
  110. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
  111. end
  112. end
  113. return pairwise_transform