2016-07-25 62 views
0

我試圖從TensorFlow中的卷積圖層獲取圖像數據。 我有一個數組,如:如何將4D陣列轉換爲另一個4D的切換尺寸在Python中

data = [N, Width, Height, Channel] 

其中N是圖像數,寬度和高度的圖像的尺寸和通道的信道的索引。

什麼我需要的是另一種四維陣列,如:

[N, Channel, Width, Height] 

的原因是由N和頻道進入週期並獲得字節的二維數組爲每個圖像的ECH通道。

img = Image.fromarray(data[N][Channel], 'L') 
img.save('my.png') 
+0

我的問題已解決。感謝球員們的快速答案! – Verych

回答

1

使用變調功能重新排序的尺寸。

https://www.tensorflow.org/versions/r0.9/api_docs/python/array_ops.html#transpose

你會做這樣的事情在tensorflow代碼。

image = tf.transpose(image, perm = [0, 3, 1, 2]) 

在perm參數中,您可以指定所需維度的新順序。在這種情況下,您將通道尺寸(3)移到第二個位置。

如果您想在將它輸入到tensorflow模型之前執行此操作,您可以使用np.transpose以相同的方式。

1

只需使用np.transpose

x = np.zeros((32, 10, 10, 3)) # image with 3 channels, size 10x10 
res = np.tranpose(x, (0, 3, 1, 2)) 
print res.shape # prints (32, 3, 10, 10)