I need to downsample my label tensor from (n, c, W, H) to (n, c, w, h) and my tensor has type of torch.int64 because it is a label tensor. I tried to use
F.interpolate(lb, size, mode = 'nearest'), but it has the result of
RuntimeError: upsample_nearest1d_forward is not implemented for type torch.cuda.LongTensor
So what is the correct way to do this?
Maybe you could just calculate the indices and slice the input tensor directly:
N, C, H, W = 1, 3, 10, 10
h, w = 5, 5
x = torch.randint(0, 10, (N, C, H, W))
iw = torch.linspace(0, W-1, w).long()
ih = torch.linspace(0, H-1, h).long()
x_interp = x[:, :, ih[:, None], iw]
Would that work for you?
Yes, good. Thanks a lot !!
Could you explain how this works? I’ve seen advanced indexing and normal slicing. This seems new.
This is basically advanced indexing in dims 2 and 3.
Similar to the numpy example with the difference that I get all values in dim 0 and 1.
So it’s basically broadcasting plus advanced indexing. Cool! Thank you.