Hello, I want to upsample a feature map by a scale factor=2 but with Transposed Convolution.
Here is a code for this purpose but by using torch.nn.Upsamle.
nn.Upsample(scale_factor=2, mode=‘nearest’)
Is there any possibility to do this with Transposed Convolution and if it is possible, what parameter should I give to
torch.nn.ConvTranspose2d(in_channels , out_channels , kernel_size , stride=1 , padding=0 , output_padding=0 , groups=1 , bias=True , dilation=1 , padding_mode=‘zeros’ , device=None , dtype=None )?
Hi Abolfazl!
Use kernel_size = stride = 2
(and for in_channels
and out_channels
,
use whatever is appropriate for your use case):
>>> import torch
>>> torch.__version__
'1.13.0'
>>> t = torch.ones (1, 1, 8, 8)
>>> t.shape
torch.Size([1, 1, 8, 8])
>>> convtran = torch.nn.ConvTranspose2d (in_channels = 1, out_channels = 1, kernel_size = 2, stride = 2)
>>> convtran (t).shape
torch.Size([1, 1, 16, 16])
>>> convtran.weight
Parameter containing:
tensor([[[[ 0.0681, -0.1747],
[-0.3762, 0.0049]]]], requires_grad=True)
>>> convtran.bias
Parameter containing:
tensor([0.3983], requires_grad=True)
Note, that ConvTranspose2d
has trainable weight
and bias
parameters,
so it is a trainable up-sampling (which is probably a good thing).
Best.
K. Frank
Thank you for your attention