Pytorch change format of tensors from BxWxH to B, N, 3
I have a tensor A with the shape BxWxH (B=Batch size, W=Width, H=Height) and want to change it to a tensor B of shape BxNx3 (B=Batch size, N=Number of points=W*H).
Tensor A represents a depth map, e.g. tensor[0,1,2] =&gt; gives the depth value for the pixel (1,2) in batch 0.
Tensor B also represents a depth map but in a different format. Each point in tensor B has the following three dimensions: (x coord, y coord, depth value).
How can I transform tensor A into tensor B?

You are looking for meshgrid to give you the x and y coordinates of each pixel:
b, w, h = A.shape
x, y = torch.meshgrid(torch.arange(w), torch.arange(h))
B = torch.cat((x[None, ...], y[None, ...], A), dim=0)
B = B.reshape(b, w*h, 3) 

