pairwise_transform_scale.lua 4.3 KB

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