Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

_mask.py 1.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  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. """:class:`torch.Tensor` subclass for segmentation and detection masks with shape ``[..., H, W]``.
  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)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...