2016-08-26 19 views
0

在Python代碼我見下文(最初,標籤是類型(15093的兩行,即,1-d陣列)(來自PY-較快rcnn)爲什麼使用轉置比直接設置數組?

labels = labels.reshape((1, height, width, A)).transpose(0, 3, 1, 2) 
labels = labels.reshape((1, 1, A * height, width)) 

沒有任何理由的作者使用轉不是直接設置

labels = labels.reshape((1, A, height, width)) 
labels = labels.reshape((1, 1, A * height, width)) 

甚至,

labels = labels.reshape((1, 1, A * height, width)) 

?(我想這是關係到數據的順序初始標籤陣列中)

回答

1

答案很簡單:是的,有一個原因,給定相同的labels數組,你的3個方法的結果根本不一樣。

允許檢查用一個例子:

import numpy as np 
height, width, A = [2,3,4] 
arr=np.random.rand(1*height*width*A) 

print("Method 1") 
labels1=np.copy(arr) 
labels1 = labels1.reshape((1, height, width, A)).transpose(0, 3, 1, 2) 
labels1 = labels1.reshape((1, 1, A * height, width)) 
print(labels1) 

print("Method 2") 
labels2=np.copy(arr) 
labels2 = labels2.reshape((1, A, height, width)) 
labels2 = labels2.reshape((1, 1, A * height, width)) 
print(labels2) 

print("Method 3") 
labels3=np.copy(arr) 
labels3 = labels3.reshape((1, 1, A * height, width)) 
print(labels3) 

其中給出:

>>> Method 1 
>>> [[[[ 0.97360395 0.40639034 0.92936386] 
>>> [ 0.01687321 0.94744919 0.39188023] 
>>> [ 0.34210967 0.36342341 0.6938464 ] 
>>> [ 0.60065943 0.00356836 0.91785409] 
>>> [ 0.57095964 0.61036102 0.17318427] 
>>> [ 0.38002045 0.08596757 0.29407445] 
>>> [ 0.95899964 0.13046103 0.36286533] 
>>> [ 0.86970793 0.11659624 0.82073826]]]] 
>>> Method 2 
>>> [[[[ 0.97360395 0.34210967 0.57095964] 
>>> [ 0.95899964 0.40639034 0.36342341] 
>>> [ 0.61036102 0.13046103 0.92936386] 
>>> [ 0.6938464 0.17318427 0.36286533] 
>>> [ 0.01687321 0.60065943 0.38002045] 
>>> [ 0.86970793 0.94744919 0.00356836] 
>>> [ 0.08596757 0.11659624 0.39188023] 
>>> [ 0.91785409 0.29407445 0.82073826]]]] 
>>> Method 3 
>>> [[[[ 0.97360395 0.34210967 0.57095964] 
>>> [ 0.95899964 0.40639034 0.36342341] 
>>> [ 0.61036102 0.13046103 0.92936386] 
>>> [ 0.6938464 0.17318427 0.36286533] 
>>> [ 0.01687321 0.60065943 0.38002045] 
>>> [ 0.86970793 0.94744919 0.00356836] 
>>> [ 0.08596757 0.11659624 0.39188023] 
>>> [ 0.91785409 0.29407445 0.82073826]]]] 

所以方法圖1是從圖2和3不同,而圖2和3是相同的。

+0

感謝您的好例子! –

+0

因此,當我們首先重塑一維數組或者以有意義的格式排列數據時,數據就具有了含義。 –

相關問題