全零的Numpy padding 4D单位 [英] Numpy padding 4D units with all zeros

查看:138
本文介绍了全零的Numpy padding 4D单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个4D numpy数组,但是每个元素都是一个可变大小的3D体积.本质上,它是3D卷的一小撮列表.所以numpy数组的形状是...

I have a 4D numpy array, but each element is a variable size 3D volume. Essentially it is a numpy list of 3D volumes. So the numpy array has the shape...

(Pdb) batch_x.shape
(3,)

然后将元素i放在该列表中,看起来像这样...

And take element i in that list, and it looks like this...

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

我正在尝试使用以下代码用零填充每个3D体积...

I'm trying to pad each 3D volume with zeros, with the following code...

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)所以错误消息至少告诉我我的尺寸应该正确.

I'm trying to pad each 3D volume such that they all have the same shape -- [10,75,75]. Keep in mind, like I showed above, batch_x[i].shape = (7,70,66) So the error message is at least showing me that my dimensions should be correct.

为证据,调试...

(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

推荐答案

因此,除去了无关紧要的内容,问题是:

So stripped of the extraneous stuff, the problem is:

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的输入时看起来像一个问题.我并没有使用太多,但是我记得它在每个尺寸的开始和结束都需要焊盘尺寸.

Looks like a problem with defining the inputs to pad. I haven't used it much, but I recall it required pad size for both the start and end of each dimension.

来自其文档:

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.

因此,让我们尝试一个元组的元组:

So lets try a tuple of tuples:

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

你能从那里拿走吗?

这篇关于全零的Numpy padding 4D单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆