簡體   English   中英

全零的Numpy padding 4D單位

[英]Numpy padding 4D units with all zeros

我有一個4D numpy數組,但是每個元素都是一個可變大小的3D體積。 本質上,它是3D卷的一小撮列表。 所以numpy數組的形狀是...

(Pdb) batch_x.shape
(3,)

並將元素i放入該列表中,它看起來像這樣……

(Pdb) batch_x[i].shape
(7, 70, 66)

我正在嘗試使用以下代碼用零填充每個3D體積...

for i in range(batch_size):
    pdb.set_trace()
    batch_x[i] = np.lib.pad(batch_x[i], (n_input_z - int(batch_x[i][:,0,0].shape[0]),
                                                    n_input_x - int(batch_x[i][0,:,0].shape[0]),
                                                    n_input_y - int(batch_x[i][0,0,:].shape[0])),
                                    'constant', constant_values=(0,0,0))
    batch_y[i] = np.lib.pad(batch_y[i], (n_input_z - int(batch_y[i][:,0,0].shape[0]),
                                                    n_input_x - int(batch_y[i][0,:,0].shape[0]),
                                                    n_input_y - int(batch_y[i][0,0,:].shape[0])),
                                    'constant', constant_values=(0,0,0))

有錯誤如下...

*** ValueError: Unable to create correctly shaped tuple from (3, 5, 9)

我試圖填充每個3D體積,以使它們都具有相同的形狀- [10,75,75] 請記住,就像我上面顯示的那樣, batch_x[i].shape = (7,70,66)因此錯誤消息至少告訴我我的尺寸應該正確。

作為證據,調試...

(Pdb) int(batch_x[i][:,0,0].shape[0])
7
(Pdb) n_input_z
10
(Pdb) (n_input_z - int(batch_x[i][:,0,0].shape[0]))
3

因此,除去多余的東西,問題是:

In [7]: x=np.ones((7,70,66),int)
In [8]: np.pad(x,(3,5,9),mode='constant',constant_values=(0,0,0))
...
ValueError: Unable to create correctly shaped tuple from (3, 5, 9)

定義pad的輸入似乎是一個問題。 我並沒有使用太多,但是我記得它在每個尺寸的開始和結束都需要焊盤尺寸。

從其文檔:

pad_width : {sequence, array_like, int}
    Number of values padded to the edges of each axis.
    ((before_1, after_1), ... (before_N, after_N)) unique pad widths
    for each axis.

因此,讓我們嘗試一個元組的元組:

In [13]: np.pad(x,((0,3),(0,5),(0,9)), mode='constant', constant_values=0).shape
Out[13]: (10, 75, 75)

你能從那里拿走嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM