2

我有一个 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
4

1 回答 1

4

去掉多余的东西,问题是:

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)

你能从那里拿走吗?

于 2016-08-15T23:58:56.157 回答