简体   繁体   English

如何从 pytorch 中的图像中提取补丁?

[英]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.

相关问题 如何从 pytorch/tensorflow 中的图像中提取补丁成 4 个相等的部分? - How to extract patches from an image in pytorch/tensorflow into 4 equal parts? PyTorch 中是否有提取图像块的功能? - Is there a function to extract image patches in PyTorch? 了解用于从图像中提取补丁的 tf.extract_image_patches - Understanding tf.extract_image_patches for extracting patches from an image 在python中从图像中提取任意矩形补丁 - Extract arbitrary rectangular patches from image in python 从图像中提取 N 个补丁 - Extract N number of patches from an image 如何从具有许多不同前景的图像中提取任意背景矩形块 - How to extract arbitrary background rectangular patches from an image with many different foregrounds 如何(快速)从特定点的二维图像中提取双线性插值块? - How to (quickly) extract bilinear-interpolated patches from a 2d image at specific points? 如何从 Pytorch 中的单个图像中提取特征向量? - How to extract feature vector from single image in Pytorch? 从(图像)数组中提取多个窗口/补丁,如另一个数组中所定义 - Extract multiple windows/patches from an (image) array, as defined in another array 使用python从图像中提取补丁 - extract patches from images with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM