pairwise_transform_utils.lua 3.0 KB

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