[docs]classBatchImages(TATensor):""":class:`torch.Tensor` subclass for batch of images. Args: data: Any data that can be turned into a tensor with :func:`torch.as_tensor`. dtype: Desired data type. If omitted, will be inferred from ``data``. device: 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: 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[dtype]=None,device:Optional[Union[device,str,int]]=None,requires_grad:Optional[bool]=None,)->BatchImages:tensor=cls._to_tensor(data,dtype=dtype,device=device,requires_grad=requires_grad)iftensor.ndim<4:raiseValueErrorreturntensor.as_subclass(cls)
[docs]@classmethoddefcat(cls,images_batches:Sequence[BatchImages])->BatchImages:"""Concatenates the sequence of images batches into a single batch."""attrs=["requires_grad","device","dtype",]forbatch_imagesinimages_batches:ifnotisinstance(batch_images,BatchImages):raiseValueError("All batches must be of type BatchImages.")forattrinattrs:ifgetattr(batch_images,attr)!=getattr(images_batches[0],attr):raiseValueError(f"All batches of images must have the same {attr} attribute.")returncls(torch.cat([images.dataforimagesinimages_batches],0))
[docs]defto_samples(self)->list[Image]:"""Get the tensors."""return[Image(image.clone())forimageinself]