<p>让我用Python3来讨论</p>
<blockquote>
<p>I use the transpose function in python as <code>data.transpose(3, 0, 1, 2)</code></p>
</blockquote>
<p>这是错误的,因为此操作需要4个维度,而您只提供3个维度(如<code>(10,10,10)</code>)。可复制为:</p>
<pre><code>>>> 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
</code></pre>
<p>如果图像批次为1,则可以通过将(10,10,10)重塑为(1,10,10,10)来添加另一个维度。这可以通过以下方式实现:</p>
<pre class="lang-py prettyprint-override"><code>w,h,c = original_image.shape #10,10,10
modified_img = np.reshape((1,w,h,c)) #(1,10,10,10)
</code></pre>
<blockquote>
<p>what does it mean of 3, 0, 1, 2.</p>
</blockquote>
<p>对于2D numpy数组,数组(矩阵)的<code>transpose</code>操作与名称所述的一样。但是对于像您这样的高维数组,它基本上是作为<code>moveaxis</code>工作的</p>
<pre><code>>>> 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]]])
</code></pre>
<p>显然,这两种方法都是一样的</p>