Numpy填充4D单位全部为零

时间:2016-08-15 21:51:20

标签: python arrays numpy pad

我有一个4D numpy数组,但每个元素都是一个可变大小的3D卷。基本上它是一个numpy的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

0 个答案:

没有答案