Pytorch卷积自动编码器

Pytorch卷积自动编码器,pytorch,autoencoder,Pytorch,Autoencoder,如何构造卷积自动编码器的解码器部分?假设我有这个 (输入->conv2d->maxpool2d->maxunpol2d->convTranspose2d->输出): Pytorch特定问题:为什么我不能在解码器部分使用maxUnpol2D。这给了我以下错误: TypeError: forward() missing 1 required positional argument: 'indices' 还有一个概念性问题:我们在解码器中所做的不应该与在编码器中所做的相反吗?我看到了一些实现,它们似

如何构造卷积自动编码器的解码器部分?假设我有这个

(输入->conv2d->maxpool2d->maxunpol2d->convTranspose2d->输出)

Pytorch特定问题:为什么我不能在解码器部分使用maxUnpol2D。这给了我以下错误:

TypeError: forward() missing 1 required positional argument: 'indices'

还有一个概念性问题:我们在解码器中所做的不应该与在编码器中所做的相反吗?我看到了一些实现,它们似乎只关心解码器的输入和输出的维度。和是一些示例。

对于问题的torch部分,unpool模块具有从池模块返回的索引作为必需的位置参数,这些索引将以
返回\ u index=True返回。所以你可以

class ConvDAE(nn.Module):
    def __init__(self):
        super().__init__()

        # input: batch x 3 x 32 x 32 -> output: batch x 16 x 16 x 16
        self.encoder = nn.Sequential(
            nn.Conv2d(3, 16, 3, stride=1, padding=1), # batch x 16 x 32 x 32
            nn.ReLU(),
            nn.BatchNorm2d(16),
            nn.MaxPool2d(2, stride=2, return_indices=True)
        )

        self.unpool = nn.MaxUnpool2d(2, stride=2, padding=0)

        self.decoder = nn.Sequential( 
            nn.ConvTranspose2d(16, 16, 3, stride=2, padding=1, output_padding=1), 
            nn.ReLU(),
            nn.BatchNorm2d(16),
            nn.ConvTranspose2d(16, 3, 3, stride=1, padding=1, output_padding=0), 
            nn.ReLU()
        )

    def forward(self, x):
        print(x.size())
        out, indices = self.encoder(x)
        out = self.unpool(out, indices)
        out = self.decoder(out)
        print(out.size())
        return out
至于问题的一般部分,我不认为最先进的技术是使用对称解码器部分,因为已经证明,去卷积/转置卷积会产生棋盘效应,许多方法倾向于使用上采样模块。您将通过PyTorch频道更快地找到更多信息

class ConvDAE(nn.Module):
    def __init__(self):
        super().__init__()

        # input: batch x 3 x 32 x 32 -> output: batch x 16 x 16 x 16
        self.encoder = nn.Sequential(
            nn.Conv2d(3, 16, 3, stride=1, padding=1), # batch x 16 x 32 x 32
            nn.ReLU(),
            nn.BatchNorm2d(16),
            nn.MaxPool2d(2, stride=2, return_indices=True)
        )

        self.unpool = nn.MaxUnpool2d(2, stride=2, padding=0)

        self.decoder = nn.Sequential( 
            nn.ConvTranspose2d(16, 16, 3, stride=2, padding=1, output_padding=1), 
            nn.ReLU(),
            nn.BatchNorm2d(16),
            nn.ConvTranspose2d(16, 3, 3, stride=1, padding=1, output_padding=0), 
            nn.ReLU()
        )

    def forward(self, x):
        print(x.size())
        out, indices = self.encoder(x)
        out = self.unpool(out, indices)
        out = self.decoder(out)
        print(out.size())
        return out