Reputation: 812
I have a tensor A of shape (1,12,2,2) as follows:
([[[[1., 3.],
[9., 11.],
[[ 2., 4.],
[10., 12.]],
[[ 5., 7.],
[13., 15.]],
[[ 6., 8.],
[14., 16.]],
[[17., 19.],
[25., 27.]],
[[18., 20.],
[26., 28.]],
[[21., 23.],
[29., 31.]],
[[22., 24.],
[30., 32.]],
[[33., 35.],
[41., 43.]],
[[34., 36.],
[42., 44.]],
[[37., 39.],
[45., 47.]],
[[38., 40.],
[46., 48.]]]])
I want to shuffle it using pytorch to produce the following tensor B of shape (1,3,4,4):
tensor([[[[ 1., 6., 3., 8.],
[21., 34., 23., 36.],
[ 9., 14., 11., 16.],
[29., 42., 31., 44.]],
[[ 2., 17., 4., 19.],
[22., 37., 24., 39.],
[10., 25., 12., 27.],
[30., 45., 32., 47.]],
[[ 5., 18., 7., 20.],
[33., 38., 35., 40.],
[13., 26., 15., 28.],
[41., 46., 43., 48.]]]])
I have implemented this using two for loops as follows:
B = torch.zeros(1,3,4,4, dtype=torch.float)
ctr = 0
for i in range(2):
for j in range(2):
B[:,:,i:4:2,j:4:2] = A[:,ctr:ctr+3,:,:]
ctr = ctr+3
I'm looking for any way to implement this in a vectorized manner in pytorch without these for loops. Maybe using functions like .permute()
, etc.
Upvotes: 1
Views: 561
Reputation: 1383
Just generalising the above solution for any upsampling factor 'r' like in pixel shuffle.
B = A.reshape(-1,r,3,s,s).permute(2,3,0,4,1).reshape(1,3,rs,rs)
Here 's' is the spatial resolution of each channel in 'A' and 'r' is the upsampling factor. For the particular case, r=2 and s=2. This solution should work for arbitary values of 'r' with appropriate size of 'A'.
So for the problem in hand s=2, r=2 and so the solution goes as
B = A.reshape(-1,2,3,2,2).permute(2,3,0,4,1).reshape(1,3,4,4)
as posted by @ddoGas
Similarly if 'A' had been of size (1, 192, 356, 532) and wanting to upsample by r=8 do
B = A.reshape(-1,8,3,356,532).permute(2,3,0,4,1).reshape(1,3,2848,4256)
Upvotes: 1
Reputation: 871
This will do the trick
B = A.reshape(2,2,3,2,2).permute(2,3,0,4,1).reshape(1,3,4,4)
Upvotes: 4