[英]Pytorch: How to transform image patches into matrix of feature vectors?
For use as input in a neural.network, I want to obtain a matrix of feature vectors from image patches.为了在 neural.network 中用作输入,我想从图像块中获取特征向量矩阵。 I'm using the Fashion-MNIST dataset (28x28 images) and have used Tensor.unfold to obtain patches (16 7x7 patches) by doing:
我正在使用 Fashion-MNIST 数据集(28x28 图像)并使用 Tensor.unfold 通过以下方式获取补丁(16 7x7 补丁):
#example on one image
mnist_train = torchvision.datasets.FashionMNIST(
root="../data", train=True, transform=transforms.Compose([transforms.ToTensor()]), download=True)
x = mnist_train[0][0][-1, :, :]
x = x.unfold(0, 7, 7).unfold(1, 7, 7)
x.shape
>>> torch.Size([4, 4, 7, 7])
Here I end up with a 4x4 tensor of 7x7 patches, however I want to vectorize each patch to obtain a matrix X with dimensions (16: number of patches xd: dimensions of feature vector).在这里,我最终得到一个 4x4 张量的 7x7 补丁,但是我想对每个补丁进行矢量化以获得具有维度的矩阵X (16:补丁数 xd:特征向量的维度)。 I'm unsure whether flatten() can be used here and how I would go about using it.
我不确定是否可以在这里使用 flatten() 以及我将如何使用它 go 。
To close this out, moving the content of the comments to here:要关闭它,请将评论的内容移至此处:
#example on one image
mnist_train = torchvision.datasets.FashionMNIST(
root="../data", train=True,
transform=transforms.Compose([transforms.ToTensor()]), download=True)
x = mnist_train[0][0][-1, :, :]
x = x.unfold(0, 7, 7).unfold(1, 7, 7)
x.shape
Output: Output:
>>> torch.Size([4, 4, 7, 7])
And then:然后:
x.reshape(-1,7,7)
x.shape
Output: Output:
torch.Size([16,7,7])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.