I’m new to Pytorch and I was wondering how to write a code that efficiently transforms channels into tiles. So from shape [c, n/(c/2), n/(c/2)] to shape [1, n, n], but respecting the tiles ordering, which .reshape does not do. I’ll explain better with an example:
Let’s assume I have a tensor with shape input.shape=torch.Size([4, 64, 64]), and I would like an output tensor with shape output.shape=torch.Size([1, 128, 128]), but where each channel of the first input tensor is a tile in a quarter of the output tensor, e.g. output[64:, :64]==input[1, 64, 64].
But I don’t want to use assignation or for loops. The code would have to be general enough to take also e.g. an input tensor of shape [16, 32, 32], and output a tensor with size [1, 128, 128], where the 16 channels are ordered as tiles.
To answer this question, it might be helpful if you wrote a naive for-loop version for testing purposes and to clarify how you want to rearrange the underlying data.
Let’s say I have an input of 4 channels all of shape [128, 128]. In this simple example, the channels all look the same. I would like to rearrange each channel like a tile in a mosaic with shape [128x2, 128x2].
Since I am a new user I can only embed a single image. So here’s the output I would like. And each of the 4 images was a channel in input.
I would like the code to be flexible enough to choose based on input channels, how to rearrange the tiles in the output. Ideally, given input with shape [C, H, W] (with H==W), I’d like the output to have shape [1, HxC/2. WxC/2]. But .reshape or .view do not rearrange the pixels in the way that I’d like them to be.