[英]How to extract patches from an image in pytorch?
I want to extract image patches from an image with patch size 128 and stride 32, so I have this code, but it gives me an error:我想从补丁大小为 128、步幅为 32 的图像中提取图像补丁,所以我有这段代码,但它给了我一个错误:
from PIL import Image
img = Image.open("cat.jpg")
x = transforms.ToTensor()(img)
x = x.unsqueeze(0)
size = 128 # patch size
stride = 32 # patch stride
patches = x.unfold(1, size, stride).unfold(2, size, stride).unfold(3, size, stride)
print(patches.shape)
and the error I get is:我得到的错误是:
RuntimeError: maximum size for tensor at dimension 1 is 3 but size is 128
This is the only method I've found so far.这是迄今为止我发现的唯一方法。 but it gives me this error
但它给了我这个错误
The size of your x
is [1, 3, height, width]
.你的
x
的大小是[1, 3, height, width]
。 Calling x.unfold(1, size, stride)
tries to create slices of size 128 from dimension 1, which has size 3, hence it is too small to create any slice.调用
x.unfold(1, size, stride)
尝试从尺寸为 3 的维度 1 创建大小为 128 的切片,因此它太小而无法创建任何切片。
You don't want to create slices across dimension 1, since those are the channels of the image (RGB in this case) and they need to be kept as they are for all patches.您不想创建跨维度 1 的切片,因为这些是图像的通道(在本例中为 RGB),并且它们需要保持原样用于所有补丁。 The patches are only created across the height and width of an image.
仅在图像的高度和宽度上创建补丁。
patches = x.unfold(2, size, stride).unfold(3, size, stride)
The resulting tensor will have size [1, 3, num_vertical_slices, num_horizontal_slices, 128, 128]
.生成的张量将具有大小
[1, 3, num_vertical_slices, num_horizontal_slices, 128, 128]
。 You can reshape it to combine the slices to get a list of patches ie size of [1, 3, num_patches, 128, 128]
:您可以对其进行整形以组合切片以获得补丁列表,即
[1, 3, num_patches, 128, 128]
的大小:
patches = patches.reshape(1, 3, -1, size, size)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.