pairwise_transform_user.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. local pairwise_utils = require 'pairwise_transform_utils'
  2. local data_augmentation = require 'data_augmentation'
  3. local iproc = require 'iproc'
  4. local gm = {}
  5. gm.Image = require 'graphicsmagick.Image'
  6. local pairwise_transform = {}
  7. function pairwise_transform.user(x, y, size, offset, n, options)
  8. assert(x:size(1) == y:size(1))
  9. local scale_y = y:size(2) / x:size(2)
  10. assert(x:size(3) == y:size(3) / scale_y)
  11. x, y = pairwise_utils.preprocess_user(x, y, scale_y, size, options)
  12. assert(x:size(3) == y:size(3) / scale_y and x:size(2) == y:size(2) / scale_y)
  13. local batch = {}
  14. local lowres_y = nil
  15. local xs ={x}
  16. local ys = {y}
  17. local ls = {}
  18. if options.active_cropping_rate > 0 then
  19. lowres_y = pairwise_utils.low_resolution(y)
  20. end
  21. if options.pairwise_flip and n == 1 then
  22. xs[1], ys[1] = data_augmentation.pairwise_flip(xs[1], ys[1])
  23. elseif options.pairwise_flip then
  24. xs, ys, ls = pairwise_utils.flip_augmentation(x, y, lowres_y)
  25. end
  26. assert(#xs == #ys)
  27. local perm = torch.randperm(#xs)
  28. for i = 1, n do
  29. local t = perm[(i % #xs) + 1]
  30. local xc, yc = pairwise_utils.active_cropping(xs[t], ys[t], ls[t], size, scale_y,
  31. options.active_cropping_rate,
  32. options.active_cropping_tries)
  33. xc = iproc.byte2float(xc)
  34. yc = iproc.byte2float(yc)
  35. if options.rgb then
  36. else
  37. if xc:size(1) > 1 then
  38. yc = iproc.rgb2y(yc)
  39. xc = iproc.rgb2y(xc)
  40. end
  41. end
  42. if options.gcn then
  43. local mean = xc:mean()
  44. local stdv = xc:std()
  45. if stdv > 0 then
  46. xc:add(-mean):div(stdv)
  47. else
  48. xc:add(-mean)
  49. end
  50. end
  51. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  52. end
  53. return batch
  54. end
  55. return pairwise_transform