带转置卷积的输出宽度和高度

Output width and heigh with transposed convolution

我有一个形状为 torch.Size([4, 256, 1, 5] 的输入,我想使用 torch.nn.ConvTranspose2d(ip_sz, op_sz, kernel_size, stride, padding, output_padding)

将其上采样到 torch.Size([4, 256, 2, 11])

我尝试了 kernel_size, stride, padding, output_padding 的不同组合,但是,我无法获得想要的结果。

import torch
import torch.nn as nn

class Dummy(nn.Module):
    def __init__(self, ip_sz, op_sz, kernel_size=3, stride=2, padding=1, output_padding=1):
        super(Dummy, self).__init__()
        self.conv1 = nn.ConvTranspose2d(ip_sz, op_sz, kernel_size=kernel_size, 
                                        stride=stride, padding=padding, output_padding=output_padding)
    
    def forward(self, x):
        x = self.conv1(x)
        print(x.shape)
        return x

dummy_model = Dummy(256, 256)
dummy_model(torch.rand([4, 256, 1, 5]))

使用以下参数的卷积应该有效:

new_dummy = Dummy(256, 256, kernel_size=(2, 3), stride=2, padding=0, output_padding=0)