_video.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from __future__ import annotations
  2. from typing import Any, Optional, Union
  3. import torch
  4. from ._tv_tensor import TVTensor
  5. class Video(TVTensor):
  6. """[BETA] :class:`torch.Tensor` subclass for videos.
  7. Args:
  8. data (tensor-like): Any data that can be turned into a tensor with :func:`torch.as_tensor`.
  9. dtype (torch.dtype, optional): Desired data type. If omitted, will be inferred from
  10. ``data``.
  11. device (torch.device, optional): Desired device. If omitted and ``data`` is a
  12. :class:`torch.Tensor`, the device is taken from it. Otherwise, the video is constructed on the CPU.
  13. requires_grad (bool, optional): Whether autograd should record operations. If omitted and
  14. ``data`` is a :class:`torch.Tensor`, the value is taken from it. Otherwise, defaults to ``False``.
  15. """
  16. def __new__(
  17. cls,
  18. data: Any,
  19. *,
  20. dtype: Optional[torch.dtype] = None,
  21. device: Optional[Union[torch.device, str, int]] = None,
  22. requires_grad: Optional[bool] = None,
  23. ) -> Video:
  24. tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad)
  25. if data.ndim < 4:
  26. raise ValueError
  27. return tensor.as_subclass(cls)
  28. def __repr__(self, *, tensor_contents: Any = None) -> str: # type: ignore[override]
  29. return self._make_repr()