FiveCrop¶
- class torchvision.transforms.FiveCrop(size)[source]¶
将给定的图像裁剪为四个角和中心裁剪。 如果图像是torch Tensor,则期望其具有[…, H, W]形状,其中…表示任意数量的前导维度
注意
此转换返回一组图像的元组,并且您的数据集返回的输入和目标数量可能不匹配。请参阅以下示例以了解如何处理这种情况。
- Parameters:
size (序列或整数) – 期望的裁剪输出大小。如果 size 是一个
整数而不是像 (h, w) 这样的序列,则会生成一个大小为 (size, size) 的正方形裁剪。 如果提供一个长度为 1 的序列,它将被解释为 (size[0], size[0])。
示例
>>> transform = Compose([ >>> FiveCrop(size), # this is a list of PIL Images >>> Lambda(lambda crops: torch.stack([PILToTensor()(crop) for crop in crops])) # returns a 4D tensor >>> ]) >>> #In your test loop you can do the following: >>> input, target = batch # input is a 5d tensor, target is 2d >>> bs, ncrops, c, h, w = input.size() >>> result = model(input.view(-1, c, h, w)) # fuse batch size and ncrops >>> result_avg = result.view(bs, ncrops, -1).mean(1) # avg over crops
使用
FiveCrop的示例: