pairwise_transform_user.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. yc = iproc.rgb2y(yc)
  38. xc = iproc.rgb2y(xc)
  39. end
  40. if options.gcn then
  41. local mean = xc:mean()
  42. local stdv = xc:std()
  43. if stdv > 0 then
  44. xc:add(-mean):div(stdv)
  45. else
  46. xc:add(-mean)
  47. end
  48. end
  49. table.insert(batch, {xc, iproc.crop(yc, offset, offset, size - offset, size - offset)})
  50. end
  51. return batch
  52. end
  53. return pairwise_transform