有什么方法可以转换一个numpy数组或pytorch张量?

问题描述 投票:0回答:1

我一直在一个图像数据集上尝试一些图像识别神经网络。我的图像是在形状[39209,30,30,3]的数组(或张量)中。然而,我在github上找到的一些代码,我的图像被要求为数组形状[39209,3,30,30]。我以为会有一个快速的方法来转换数组,但事实证明这是相当困难的。有谁知道这是否可能?

python arrays image tensor
1个回答
1
投票

你可以使用 torch.transpose https:/pytorch.orgdocsmastergeneratedtorch.transpose.html。

import torch

a = torch.ones((10, 30, 30, 3))

b = torch.transpose(a, 1, -1)

print(b.shape)

torch.Size([10, 3, 30, 30])

numpy 也有 transpose https:/docs.scipy.orgdocnumpyreferencegeneratednumpy.transpose.html。

© www.soinside.com 2019 - 2024. All rights reserved.