带有全零的 Numpy 填充 4D 单元

Numpy padding 4D units with all zeros

我有一个 4D numpy 数组,但每个元素都是一个可变大小的 3D 体积。本质上它是 3D 体积的 numpy 列表。所以 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)

你能从那里拿走吗?