transpose(3, 0, 1, 2) 是什么意思?

What does transpose(3, 0, 1, 2) mean?

这是什么意思?

data.transpose(3, 0, 1, 2)

此外,如果 data.shape == (10, 10, 10),为什么我会得到 ValueError: axes don't match array

看看numpy.transpose

Use transpose(a, argsort(axes)) to invert the transposition of tensors when using the axes keyword argument.

Transposing a 1-D array returns an unchanged view of the original array.


例如

>>> x = np.arange(4).reshape((2,2))
>>> x
array([[0, 1],
       [2, 3]])
>>>
>>> np.transpose(x)
array([[0, 2],
       [1, 3]])

您在转置中指定的值过多

>>> a = np.arange(8).reshape(2,2,2)
>>> a.shape (2, 2, 2)
>>> a.transpose([2,0,1])
array([[[0, 2],
        [4, 6]],

       [[1, 3],
        [5, 7]]])
>>> a.transpose(3,0,1,2) Traceback (most recent call last):   File "<interactive input>", line 1, in <module> ValueError: axes don't match array
>>>

根据 np.transpose 的 python 文档,np.transpose 函数的第二个参数是 axes,它是一个 整数列表,选修的 默认情况下 反转尺寸,否则排列轴 根据给出的值.

示例:

>>> x = np.arange(9).reshape((3,3))
>>> x
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

>>> np.transpose(x, (0,1))
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

>>> np.transpose(x, (1,0))
array([[0, 3, 6],
       [1, 4, 7],
       [2, 5, 8]])

运算从 (samples, rows, columns, channels) 转换为 (samples, channels, rows, cols), 也许 opencv 到 pytorch.

问题是您采用了 3 维矩阵并应用了 4 维转置。 你的命令是将一个 4d 矩阵(batch,rows,cols,channel) 转换为另一个 4d 矩阵 (rows,cols,channel,batch) 但你需要一个命令来转换 3d matrix.so remove 3 and write data.transpose(2, 0, 1).

让我讨论Python3。

I use the transpose function in python as data.transpose(3, 0, 1, 2)

这是错误的,因为此操作需要 4 个维度,而您只提供 3 个维度(如 (10,10,10))。可重现为:

>>> a = np.arange(60).reshape((1,4,5,3))
>>> b = a.transpose((2,0,1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: axes don't match array

如果图像批次为 1,您可以通过将 (10,10,10) 重塑为 (1,10,10,10) 来简单地添加另一个维度。这可以通过以下方式完成:

w,h,c = original_image.shape #10,10,10
modified_img = np.reshape((1,w,h,c)) #(1,10,10,10)

what does it mean of 3, 0, 1, 2.

对于二维 numpy 数组,transpose 对于数组(矩阵)的操作正如名称所示。但是对于像你这样的高维数组,它基本上可以作为 moveaxis.

>>> a = np.arange(60).reshape((4,5,3))
>>> b = a.transpose((2,0,1))
>>> b.shape
(3, 4, 5)
>>> c = np.moveaxis(a,-1,0)
>>> c.shape
(3, 4, 5)
>>> b
array([[[ 0,  3,  6,  9, 12],
        [15, 18, 21, 24, 27],
        [30, 33, 36, 39, 42],
        [45, 48, 51, 54, 57]],

       [[ 1,  4,  7, 10, 13],
        [16, 19, 22, 25, 28],
        [31, 34, 37, 40, 43],
        [46, 49, 52, 55, 58]],

       [[ 2,  5,  8, 11, 14],
        [17, 20, 23, 26, 29],
        [32, 35, 38, 41, 44],
        [47, 50, 53, 56, 59]]])
>>> c
array([[[ 0,  3,  6,  9, 12],
        [15, 18, 21, 24, 27],
        [30, 33, 36, 39, 42],
        [45, 48, 51, 54, 57]],

       [[ 1,  4,  7, 10, 13],
        [16, 19, 22, 25, 28],
        [31, 34, 37, 40, 43],
        [46, 49, 52, 55, 58]],

       [[ 2,  5,  8, 11, 14],
        [17, 20, 23, 26, 29],
        [32, 35, 38, 41, 44],
        [47, 50, 53, 56, 59]]])

很明显,这两种方法都一样。

对于所有 i, j, k, l,以下内容成立:

arr[i, j, k, l] == arr.transpose(3, 0, 1, 2)[l, i, j, k]

transpose(3, 0, 1, 2) 将数组维度从 (a, b, c, d) 重新排序为 (d, a, b, c):

>>> arr = np.zeros((10, 11, 12, 13))

>>> arr.transpose(3, 0, 1, 2).shape
(13, 10, 11, 12)