pairwise_transform_utils.lua 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 test_scale = 2
  59. if test_scale < scale then
  60. test_scale = scale
  61. end
  62. local lowres = gm.Image(y, "RGB", "DHW"):
  63. size(y:size(3) * 0.5, y:size(2) * 0.5, "Box"):
  64. size(y:size(3), y:size(2), "Box"):
  65. toTensor(t, "RGB", "DHW")
  66. local best_se = 0.0
  67. local best_xi, best_yi
  68. local m = torch.FloatTensor(y:size(1), size, size)
  69. for i = 1, tries do
  70. local xi = torch.random(0, x:size(3) - (size + 1)) * scale
  71. local yi = torch.random(0, x:size(2) - (size + 1)) * scale
  72. local xc = iproc.crop(y, xi, yi, xi + size, yi + size)
  73. local lc = iproc.crop(lowres, xi, yi, xi + size, yi + size)
  74. local xcf = iproc.byte2float(xc)
  75. local lcf = iproc.byte2float(lc)
  76. local se = m:copy(xcf):add(-1.0, lcf):pow(2):sum()
  77. if se >= best_se then
  78. best_xi = xi
  79. best_yi = yi
  80. best_se = se
  81. end
  82. end
  83. local yc = iproc.crop(y, best_xi, best_yi, best_xi + size, best_yi + size)
  84. local xc = iproc.crop(x, best_xi / scale, best_yi / scale, best_xi / scale + size / scale, best_yi / scale + size / scale)
  85. return xc, yc
  86. end
  87. end
  88. return pairwise_transform_utils