padder.py 1.1 KB

12345678910111213141516171819202122232425262728
  1. import torch.nn.functional as F
  2. class InputPadder:
  3. """Pads images such that dimensions are divisible by 8"""
  4. # TODO: Ideally, this should be part of the eval transforms preset, instead
  5. # of being part of the validation code. It's not obvious what a good
  6. # solution would be, because we need to unpad the predicted flows according
  7. # to the input images' size, and in some datasets (Kitti) images can have
  8. # variable sizes.
  9. def __init__(self, dims, mode="sintel"):
  10. self.ht, self.wd = dims[-2:]
  11. pad_ht = (((self.ht // 8) + 1) * 8 - self.ht) % 8
  12. pad_wd = (((self.wd // 8) + 1) * 8 - self.wd) % 8
  13. if mode == "sintel":
  14. self._pad = [pad_wd // 2, pad_wd - pad_wd // 2, pad_ht // 2, pad_ht - pad_ht // 2]
  15. else:
  16. self._pad = [pad_wd // 2, pad_wd - pad_wd // 2, 0, pad_ht]
  17. def pad(self, *inputs):
  18. return [F.pad(x, self._pad, mode="replicate") for x in inputs]
  19. def unpad(self, x):
  20. ht, wd = x.shape[-2:]
  21. c = [self._pad[2], ht - self._pad[3], self._pad[0], wd - self._pad[1]]
  22. return x[..., c[0] : c[1], c[2] : c[3]]