简体   繁体   English

在 PyTorch 中使用转置卷积进行上采样

[英]Using Transposed convolution for upsampling in PyTorch

I have a 4D tensor of (2,1024,4,6) .我有一个(2,1024,4,6)的 4D 张量。 I want to use transposed convolution for upsampling spatial dimensions of such tensor by factor of two and reducing the channel numbers from 1024 to the 512. I want to have a 4D tensor like this (2,512,8,12) .我想使用转置卷积将这种张量的空间维度上采样两倍,并将通道数从 1024 减少到 512。我想要一个像(2,512,8,12)这样的 4D 张量。 How can I do that?我怎样才能做到这一点? Also, is the transposed convolution a good idea for reducing the channel numbers?另外,转置卷积是减少通道数的好主意吗? For example I used the following script but it is not working:例如,我使用了以下脚本,但它不起作用:

nn.ConvTranspose3d(in_channels=1024, out_channels=512, kernel_size=(1,2,2), stride=(1,3,2), padding=(0,1,1))

It seems you should be using ConvTranspose2d instead of ConvTranspose3d since your input tensor is 4D, shaped NCHW .看来您应该使用ConvTranspose2d而不是ConvTranspose3d因为您的输入张量是 4D,形状NCHW There are different ways of getting to these results but one straightforward approach is to use a kernel size of 2 with a matching stride:有不同的方法可以得到这些结果,但一种直接的方法是使用大小为2的 kernel 并具有匹配的步幅:

>>> conv = nn.ConvTranspose2d(1024, 512, kernel_size=2, stride=2)

Here is an inference example:这是一个推理示例:

>>> conv(torch.rand(2, 1024, 4, 6)).shape
torch.Size([2, 512, 8, 12])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM