Now I have a tensor list with for same-size-tensor A=[a,b,c,d], say [3,224,224], what is the simplest way to merge them back to one tensor [[a,b],[c,d]] with size [3,448,448]?
I think F.fold
should work assuming you want to create an output tensor where each “window” of the input is preserved:
# setup
channels = 3
h = 4
offset = channels * h * h
a = torch.arange(offset).view(channels, h, h).float()
b = torch.arange(offset).view(channels, h, h).float() + offset
c = torch.arange(offset).view(channels, h, h).float() + 2*offset
d = torch.arange(offset).view(channels, h, h).float() + 3*offset
A = [a, b, c, d]
out = torch.stack(A, dim=1)
out = out.permute(0, 2, 3, 1).reshape(channels*h*h, h)
res = F.fold(out, h*2, h, stride=h)
print(res)
# tensor([[[ 0., 1., 2., 3., 48., 49., 50., 51.],
# [ 4., 5., 6., 7., 52., 53., 54., 55.],
# [ 8., 9., 10., 11., 56., 57., 58., 59.],
# [ 12., 13., 14., 15., 60., 61., 62., 63.],
# [ 96., 97., 98., 99., 144., 145., 146., 147.],
# [100., 101., 102., 103., 148., 149., 150., 151.],
# [104., 105., 106., 107., 152., 153., 154., 155.],
# [108., 109., 110., 111., 156., 157., 158., 159.]],
# [[ 16., 17., 18., 19., 64., 65., 66., 67.],
# [ 20., 21., 22., 23., 68., 69., 70., 71.],
# [ 24., 25., 26., 27., 72., 73., 74., 75.],
# [ 28., 29., 30., 31., 76., 77., 78., 79.],
# [112., 113., 114., 115., 160., 161., 162., 163.],
# [116., 117., 118., 119., 164., 165., 166., 167.],
# [120., 121., 122., 123., 168., 169., 170., 171.],
# [124., 125., 126., 127., 172., 173., 174., 175.]],
# [[ 32., 33., 34., 35., 80., 81., 82., 83.],
# [ 36., 37., 38., 39., 84., 85., 86., 87.],
# [ 40., 41., 42., 43., 88., 89., 90., 91.],
# [ 44., 45., 46., 47., 92., 93., 94., 95.],
# [128., 129., 130., 131., 176., 177., 178., 179.],
# [132., 133., 134., 135., 180., 181., 182., 183.],
# [136., 137., 138., 139., 184., 185., 186., 187.],
# [140., 141., 142., 143., 188., 189., 190., 191.]]])
This is exactly what I need, thank you for your reply.