# Help! How is F.upsample implemented?

Since tensorrt does not support interploate/upsample operation, I want to use deconv to implement the bilinear upsamle.
However, it was found that although the original model chose the bilinear mode during using F.upsample, the results are still very different.
How is F.upsamle in mxnet implemented? Is it possible to get exactly the same result with transpose conv?
My test code(refer: nn.Upsample versus upsampling with transposed convolution):

``````def make_bilinear_weights(size, num_channels):
factor = (size + 1) // 2
if size % 2 == 1:
center = factor - 1
else:
center = factor - 0.5
og = np.ogrid[:size, :size]
filt = (1 - abs(og[0] - center) / factor) * \
(1 - abs(og[1] - center) / factor)
print filt
filt = torch.from_numpy(filt)
w = torch.zeros(num_channels, 1, size, size)
for i in range(num_channels):
w[i, 0] = filt
return w

x = np.array([[1, 2], [3, 4]], dtype=np.float32)
x = torch.from_numpy(x[np.newaxis, np.newaxis, :, :])

out1 = F.upsample(x, None, 2, 'bilinear', align_corners=True)
out2 = F.upsample(x, None, 2, 'bilinear', align_corners=False)
c = x.size(1)
out3 = F.conv_transpose2d(x, make_bilinear_weights(4, 1), stride=2, padding=1, groups=c)
print "Using usample and align_corners is True:"
print out1
print "Using usample and align_corners is False:"
print out2
print "Using conv transpose2d:"
print out3
``````

Result:

``````Using usample and align_corners is True:
tensor([[[[1.0000, 1.3333, 1.6667, 2.0000],
[1.6667, 2.0000, 2.3333, 2.6667],
[2.3333, 2.6667, 3.0000, 3.3333],
[3.0000, 3.3333, 3.6667, 4.0000]]]])
Using usample and align_corners is False:
tensor([[[[1.0000, 1.2500, 1.7500, 2.0000],
[1.5000, 1.7500, 2.2500, 2.5000],
[2.5000, 2.7500, 3.2500, 3.5000],
[3.0000, 3.2500, 3.7500, 4.0000]]]])
Using conv transpose2d:
tensor([[[[0.5625, 0.9375, 1.3125, 1.1250],
[1.1250, 1.7500, 2.2500, 1.8750],
[1.8750, 2.7500, 3.2500, 2.6250],
[1.6875, 2.4375, 2.8125, 2.2500]]]])
``````