# How to Make a 3D bilinear kernel suitable for upsampling?

Hello all, I am following the caffe code to make a 3D bilinear kernel weight for upsampling (using `ConvTranspose3d`). This is caffe code and my pytorch code:

CAFFE

``````int f = ceil(blob->shape(-1) / 2.);
float c = (2 * f - 1 - f % 2) / (2. * f);
for (int i = 0; i < blob->count(); ++i) {
float x = i % blob->shape(-1);
float y = (i / blob->shape(-1)) % blob->shape(-2);
float z = (i/(blob->shape(-1)*blob->shape(-2))) % blob->shape(-3);
data[i] = (1 - fabs(x / f - c)) * (1 - fabs(y / f - c)) * (1-fabs(z / f - c));
}
``````

PYTORCH

``````def get_upsampling_weight(in_channels, out_channels, kernel_size):
"""Make a 3D bilinear kernel suitable for upsampling"""
factor = (kernel_size + 1) // 2
if kernel_size % 2 == 1:
center = factor - 1
else:
center = factor - 0.5
og = np.ogrid[:kernel_size, :kernel_size, :kernel_size]
filt = (1 - abs(og - center) / factor) * \
(1 - abs(og - center) / factor) * \
(1 - abs(og - center) / factor)

weight = np.zeros((in_channels, out_channels, kernel_size, kernel_size, kernel_size),
dtype=np.float64)

weight[range(in_channels), range(out_channels), :, :, :] = filt
``````

The problem is that it worked when I used `in_channels=out_channels`, while has error when `in_channels # out_channels` in pytorch 0.5 and python 3.6. This is an example to use

``````w=get_upsampling_weight(4, 16, 3)
``````

The error is

``````in get_upsampling_weight
weight[range(in_channels), range(out_channels), :, :, :] = filt
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (4,) (16,)

``````

How could I fix it? Thanks all

1 Like