1

I find it really hard to visualize reshaping 4D 5D arrays in numpy/pytorch. (I assume both reshape in similar patter, I am using pytorch currently!).

Like suppose I have videos with dimension [N x C x D x H x W]

(num videos x channels video x frames video x height video x width video)

Suppose I want to reshape video into frames as [N x C x H x W], how should I proceed in reshape.

Simple applying x = x.reshape(N*D, C, H, W) doesn't actually do it, it gives wrong order of elements.

Can you help me with how to do this, and any slight of intuition of pattern you used?

On a sidenote, if i have one video (i.e suppose 1x3x100x256x256 I use :

the following code approach:

x = x.squeeze(0).T.reshape((100,3,256,256))[:,:,None,:,:] and it works

great. Couldnt figure out for more than 1 video.

Thanks!

As per the request :

input = np.random.randn(N,C,D,H,W)
output = np.zeros((N*D,C,H,W))

As per the request, a for loop based code to show what I want for h in range(N): for i in range(D): for j in range(C): for k in range(H): for l in range(W): output[h*D + i,j,k,l] = input[h,j,i,k,l]

Naman
  • 372
  • 4
  • 20
  • Could you show a working loopy code? Perhaps with `x.squeeze(0).T.reshape((100,3,256,256))[:,:,None,:,:]` that stores into an output array? – Divakar May 24 '18 at 06:55
  • Ahh you mean reshape via for loops?? – Naman May 24 '18 at 06:58
  • Yes, a working solution code using loops, just so that we have a reference to work with. – Divakar May 24 '18 at 06:58
  • @Divakar here you go :), it might be slightly incorrect indexing wise, will check, but you'll get what I wanted – Naman May 24 '18 at 07:06

1 Answers1

3

Simply swap the second and third axes, and then merge the new second axis (old third one) with the first one with reshaping -

output = input_array.swapaxes(1,2).reshape(N*D,C,H,W)

We can also use transpose : input_array.transpose(0,2,1,3,4) to get the same swapping axes effect.

For a general intuitive method, please refer to Intuition and idea behind reshaping 4D array to 2D array in NumPy.

Divakar
  • 218,885
  • 19
  • 262
  • 358
  • So in such kind of operation 5D to 4D, reshaping i should permute axes to get them adjacent and then reshape? – Naman May 24 '18 at 07:23
  • Also I thought about it, but I just dont get it intuitively, like need to check for dummy values. Any thing you know that helps or anything intuition or some references (for high dimension array operations) – Naman May 24 '18 at 07:25
  • That looks epic! Thanks, just what I was looking for :) – Naman May 24 '18 at 07:30