繁体   English   中英

Pytorch:如何将图像块转换为特征向量矩阵?

[英]Pytorch: How to transform image patches into matrix of feature vectors?

为了在 neural.network 中用作输入,我想从图像块中获取特征向量矩阵。 我正在使用 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])

在这里,我最终得到一个 4x4 张量的 7x7 补丁,但是我想对每个补丁进行矢量化以获得具有维度的矩阵X (16:补丁数 xd:特征向量的维度)。 我不确定是否可以在这里使用 flatten() 以及我将如何使用它 go 。

要关闭它,请将评论的内容移至此处:

#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:

>>> torch.Size([4, 4, 7, 7])

然后:

x.reshape(-1,7,7)
x.shape

Output:

torch.Size([16,7,7])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM