pairwise_transform_utils.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require 'image'
  2. local gm = require 'graphicsmagick'
  3. local iproc = require 'iproc'
  4. local data_augmentation = require 'data_augmentation'
  5. local pairwise_transform_utils = {}
  6. function pairwise_transform_utils.random_half(src, p, filters)
  7. if torch.uniform() < p then
  8. local filter = filters[torch.random(1, #filters)]
  9. return iproc.scale(src, src:size(3) * 0.5, src:size(2) * 0.5, filter)
  10. else
  11. return src
  12. end
  13. end
  14. function pairwise_transform_utils.crop_if_large(src, max_size)
  15. local tries = 4
  16. if src:size(2) > max_size and src:size(3) > max_size then
  17. local rect
  18. for i = 1, tries do
  19. local yi = torch.random(0, src:size(2) - max_size)
  20. local xi = torch.random(0, src:size(3) - max_size)
  21. rect = iproc.crop(src, xi, yi, xi + max_size, yi + max_size)
  22. -- ignore simple background
  23. if rect:float():std() >= 0 then
  24. break
  25. end
  26. end
  27. return rect
  28. else
  29. return src
  30. end
  31. end
  32. function pairwise_transform_utils.preprocess(src, crop_size, options)
  33. local dest = src
  34. dest = pairwise_transform_utils.random_half(dest, options.random_half_rate, options.downsampling_filters)
  35. dest = pairwise_transform_utils.crop_if_large(dest, math.max(crop_size * 2, options.max_size))
  36. dest = data_augmentation.flip(dest)
  37. dest = data_augmentation.color_noise(dest, options.random_color_noise_rate)
  38. dest = data_augmentation.overlay(dest, options.random_overlay_rate)
  39. dest = data_augmentation.unsharp_mask(dest, options.random_unsharp_mask_rate)
  40. dest = data_augmentation.shift_1px(dest)
  41. return dest
  42. end
  43. function pairwise_transform_utils.active_cropping(x, y, size, scale, p, tries)
  44. assert("x:size == y:size", x:size(2) * scale == y:size(2) and x:size(3) * scale == y:size(3))
  45. assert("crop_size % scale == 0", size % scale == 0)
  46. local r = torch.uniform()
  47. local t = "float"
  48. if x:type() == "torch.ByteTensor" then
  49. t = "byte"
  50. end
  51. if p < r then
  52. local xi = torch.random(0, x:size(3) - (size + 1))
  53. local yi = torch.random(0, x:size(2) - (size + 1))
  54. local yc = iproc.crop(y, xi * scale, yi * scale, xi * scale + size, yi * scale + size)
  55. local xc = iproc.crop(x, xi, yi, xi + size / scale, yi + size / scale)
  56. return xc, yc
  57. else
  58. local lowres = gm.Image(y, "RGB", "DHW"):
  59. size(y:size(3) * 0.5, y:size(2) * 0.5, "Box"):
  60. size(y:size(3), y:size(2), "Box"):
  61. toTensor(t, "RGB", "DHW")
  62. local best_se = 0.0
  63. local best_xi, best_yi
  64. local m = torch.FloatTensor(y:size(1), size, size)
  65. for i = 1, tries do
  66. local xi = torch.random(0, x:size(3) - (size + 1)) * scale
  67. local yi = torch.random(0, x:size(2) - (size + 1)) * scale
  68. local xc = iproc.crop(y, xi, yi, xi + size, yi + size)
  69. local lc = iproc.crop(lowres, xi, yi, xi + size, yi + size)
  70. local xcf = iproc.byte2float(xc)
  71. local lcf = iproc.byte2float(lc)
  72. local se = m:copy(xcf):add(-1.0, lcf):pow(2):sum()
  73. if se >= best_se then
  74. best_xi = xi
  75. best_yi = yi
  76. best_se = se
  77. end
  78. end
  79. local yc = iproc.crop(y, best_xi, best_yi, best_xi + size, best_yi + size)
  80. local xc = iproc.crop(x, best_xi / scale, best_yi / scale, best_xi / scale + size / scale, best_yi / scale + size / scale)
  81. return xc, yc
  82. end
  83. end
  84. return pairwise_transform_utils