[docs]classImage(TVTensor):""":class:`torch.Tensor` subclass for images with shape ``[..., C, H, W]``. .. note:: In the :ref:`transforms <transforms>`, ``Image`` instances are largely interchangeable with pure :class:`torch.Tensor`. See :ref:`this note <passthrough_heuristic>` for more details. Args: data (tensor-like, PIL.Image.Image): Any data that can be turned into a tensor with :func:`torch.as_tensor` as well as PIL images. dtype (torch.dtype, optional): Desired data type. If omitted, will be inferred from ``data``. device (torch.device, optional): Desired device. If omitted and ``data`` is a :class:`torch.Tensor`, the device is taken from it. Otherwise, the image is constructed on the CPU. requires_grad (bool, optional): Whether autograd should record operations. If omitted and ``data`` is a :class:`torch.Tensor`, the value is taken from it. Otherwise, defaults to ``False``. """def__new__(cls,data:Any,*,dtype:Optional[torch.dtype]=None,device:Optional[Union[torch.device,str,int]]=None,requires_grad:Optional[bool]=None,)->Image:ifisinstance(data,PIL.Image.Image):fromtorchvision.transforms.v2importfunctionalasFdata=F.pil_to_tensor(data)tensor=cls._to_tensor(data,dtype=dtype,device=device,requires_grad=requires_grad)iftensor.ndim<2:raiseValueErroreliftensor.ndim==2:tensor=tensor.unsqueeze(0)returntensor.as_subclass(cls)def__repr__(self,*,tensor_contents:Any=None)->str:# type: ignore[override]returnself._make_repr()
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.