How to unfold 3D tensor?

Hi all, I have an input tensor of shape 12x3x3 which corresponds to 12 patches of size 3x3, can be explained as {Patch1, Patch2, …, Patch12}. I want to convert it to 1x12x9, keeping the patches as described below,

Patch1 Patch2 Patch3
Patch4 Patch5 Patch6
Patch7 Patch8 Patch9
Patch10 Patch11 Patch12

I tried torch.unfold() with different parameters, but couldn’t find the right way. Any ideas?

How about inp.view(4, 3, 3, 3).permute(0, 2, 1, 3).reshape(1, 12, 9)?

(If anything, unfold would be used when getting into the other direction with overlapping patches.)

1 Like

Yes, that’s it, thank you. Much better than my desperate attempts. And for a different patch size ps, it should be,

inp.view(4, 3, ps, ps).permute(0, 2, 1, 3).reshape(1, 4 * ps, 3 * ps)