Shortcuts

FiveCrop

class torchvision.transforms.v2.FiveCrop(size: Union[int, Sequence[int]])[source]

Crop the image or video into four corners and the central crop.

If the input is a torch.Tensor or a Image or a Video it can have arbitrary number of leading batch dimensions. For example, the image can have [..., C, H, W] shape.

Note

This transform returns a tuple of images and there may be a mismatch in the number of inputs and targets your Dataset returns. See below for an example of how to deal with this.

Parameters:

size (sequence or int) – Desired output size of the crop. If size is an int instead of sequence like (h, w), a square crop of size (size, size) is made. If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).

Example

>>> class BatchMultiCrop(transforms.Transform):
...     def forward(self, sample: Tuple[Tuple[Union[tv_tensors.Image, tv_tensors.Video], ...], int]):
...         images_or_videos, labels = sample
...         batch_size = len(images_or_videos)
...         image_or_video = images_or_videos[0]
...         images_or_videos = tv_tensors.wrap(torch.stack(images_or_videos), like=image_or_video)
...         labels = torch.full((batch_size,), label, device=images_or_videos.device)
...         return images_or_videos, labels
...
>>> image = tv_tensors.Image(torch.rand(3, 256, 256))
>>> label = 3
>>> transform = transforms.Compose([transforms.FiveCrop(224), BatchMultiCrop()])
>>> images, labels = transform(image, label)
>>> images.shape
torch.Size([5, 3, 224, 224])
>>> labels
tensor([3, 3, 3, 3, 3])

Examples using FiveCrop:

Illustration of transforms

Illustration of transforms

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources