Batch-wise reordering of a tensor to image

Hi All,
I wonder how I can re-arrange a tensor to form an image in the fallowing format using torch operations that don’t affect the gradients. The input tensor is:
A= ([[[ 1., 2.],
[ 3., 4.]],
[[ 5., 6.],
[ 7., 8.]],
[[ 9., 10.],
[ 11., 12.]],
[[ 13., 14.],
[ 15., 16.]]])
has shape of 4x2x2
I want to change it to an image of 4x4
[[1,2,5,6],
[3,4,7,6],
[9,10,13,14],
[11,12,15,16]]
It is like the Batch-wise rearranging. I take each channel of tensor and put it in the image. You can see the photo of what I mean.

Hi,

I think this link is what you want.
In this case, I try the following code:

input = torch.tensor([[[1,2],
                       [3,4]],
                      [[5,6],
                       [7,8]],
                      [[9,10],
                       [11,12]],
                      [[13,14],
                       [15,16]]])
input = input.view(1,4,4) # 1 is used to fix the dim needed
output = input.unfold(2,2,2).unfold(3,2,2)
output = output.contiguous().view(4,4)

I think your code will yield the same result as A.view(4, 4) if I’m not mistaken.
This code should work:

A.view(2, 2, 2, 2).permute(0, 2, 1, 3).contiguous().view(4, 4)
1 Like