反卷积函数ConvTransposed2d参数使用方法
1.打五角星的5个公式,只要满足前1,2,3,5,kernal_size和stride大小随便选
2.中间运算流程看看就行了,用于了解每个参数的意义
class BasicDeConv2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1,out_padding=0, need_relu=True,
bn=nn.BatchNorm2d):
super(BasicDeConv2d, self).__init__()
self.conv = nn.ConvTranspose2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size,
stride=stride
, padding=padding, dilation=dilation, output_padding=out_padding, bias=False)
self.bn = bn(out_channels)
self.relu = nn.ReLU()
self.need_relu = need_relu
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
if self.need_relu:
x = self.relu(x)
return x
input = torch.randn(2, 32, 48, 48)
conv1 = BasicDeConv2d(32, 16, kernel_size=9, stride=4, padding=4, out_padding=3)
conv2 = BasicDeConv2d(16, 8, kernel_size=3, stride=2, padding=1, out_padding=1)
o1 = conv1(input)
o2 = conv2(o1)
print(o2.shape)
对于反卷积,其实亚像素卷积也可以做到,而且速度更快,但我总觉得它不能叫卷积,而是将channel分割再拼起来
参考文章:
https://www.cnblogs.com/kk17/p/10111768.html
https://blog.csdn.net/qq_41573860/article/details/117474254
https://blog.csdn.net/Hunter_Murphy/article/details/106870845
https://blog.csdn.net/w55100/article/details/106467776