How to linearly interpolate tensor of points while preserving the points themselves

So I have a vector of 2D points of size BxNx2. I want to linearly interpolate the points such that I get the middle point between each point while also preserving the other points, which would effectively double the number of points to Bx2Nx2. Does anyone know how I can do this efficiently in a batch?

Would grid_sample work?
You might have to use a width dimension of size 1 to match the expected shapes.

1 Like

This seems to work, but then after implementing it and trying to train my model with it I seem to get this error:

  File "train.py", line 252, in upsnet_train
    loss.backward()
  File "envs/pytorch1.1/lib/python3.6/site-packages/torch/tensor.py", line 107, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "envs/pytorch1.1/lib/python3.6/site-packages/torch/autograd/__init__.py", line 93, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: CUDA error: invalid configuration argument

Note, after I interpolate the points, I compute a loss on top of it. The interpolation code is here:

B, N_old, N_new = points.size(0), points.size(1), points.size(1) * 2
grid = torch.cat([torch.zeros((B, N_new, 1, 1)),
                  torch.linspace(0, N_old, N_new).unsqueeze(0).repeat((B, 1)).unsqueeze(
                      -1).unsqueeze(-1)], dim=-1).cuda()
grid = (grid - N_old/2.) / N_old * 2.
rows = F.grid_sample(points.unsqueeze(1)[:, :, :, 0].unsqueeze(-1), grid).squeeze(1)
cols = F.grid_sample(points.unsqueeze(1)[:, :, :, 1].unsqueeze(-1), grid).squeeze(1)
points = torch.cat([rows,cols], dim=-1)

Could you post the shapes and dummy tensors so that we can reproduce this issue, please?