_mask.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import annotations
  2. from typing import Any, Optional, Union
  3. import PIL.Image
  4. import torch
  5. from ._tv_tensor import TVTensor
  6. class Mask(TVTensor):
  7. """[BETA] :class:`torch.Tensor` subclass for segmentation and detection masks.
  8. Args:
  9. data (tensor-like, PIL.Image.Image): Any data that can be turned into a tensor with :func:`torch.as_tensor` as
  10. well as PIL images.
  11. dtype (torch.dtype, optional): Desired data type. If omitted, will be inferred from
  12. ``data``.
  13. device (torch.device, optional): Desired device. If omitted and ``data`` is a
  14. :class:`torch.Tensor`, the device is taken from it. Otherwise, the mask is constructed on the CPU.
  15. requires_grad (bool, optional): Whether autograd should record operations. If omitted and
  16. ``data`` is a :class:`torch.Tensor`, the value is taken from it. Otherwise, defaults to ``False``.
  17. """
  18. def __new__(
  19. cls,
  20. data: Any,
  21. *,
  22. dtype: Optional[torch.dtype] = None,
  23. device: Optional[Union[torch.device, str, int]] = None,
  24. requires_grad: Optional[bool] = None,
  25. ) -> Mask:
  26. if isinstance(data, PIL.Image.Image):
  27. from torchvision.transforms.v2 import functional as F
  28. data = F.pil_to_tensor(data)
  29. tensor = cls._to_tensor(data, dtype=dtype, device=device, requires_grad=requires_grad)
  30. return tensor.as_subclass(cls)