pairwise_transform_user.lua 1.5 KB

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