Transform a tensor of [1,256,256] to [3,256,256]


l have a dataset following this format [batch, channel, width, height]= [10000,1,256,256]

to train resnet l need to have 3 channels.

My question is how to transform my data with 1 channel to 3 channels

[10000,1,256,256] to [10000,3,256,256] ?

Thank you

You can use .expand for this:

a = torch.randn(10, 1, 256, 256)
a = a.expand(-1, 3, -1, -1)

Note that this will repeat the channel dimension.

1 Like

thank you. This is what lā€™m looking for

Well, you are probably better off changing the first conv block parameters if you are only going to train on 1 channel data.

you can also do:

nn.Conv2d(1, 3, 1)(input)