Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoencoder MaxUnpool2d missing 'Indices' argument

Tags:

pytorch

The following model returns the error: TypeError: forward() missing 1 required positional argument: 'indices'

I've exhausted many online examples and they all look similar to my code. My maxpool layer returns both the input and the indices for the unpool layer. Any ideas on what's wrong?

class autoencoder(nn.Module):
def __init__(self):
    super(autoencoder, self).__init__()
    self.encoder = nn.Sequential(
        ...
        nn.MaxPool2d(2, stride=1, return_indices=True)
    )
    self.decoder = nn.Sequential(
        nn.MaxUnpool2d(2, stride=1),
        ...
    )

def forward(self, x):
    x = self.encoder(x)
    x = self.decoder(x)
    return x
like image 477
Rooterbuster Avatar asked Oct 25 '25 04:10

Rooterbuster


1 Answers

Similar to the question here, the solution seems to be to separate the maxunpool layer from the decoder and explicitly pass its required parameters. nn.Sequential only takes one parameter.

class SimpleConvAE(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.MaxPool2d(2, stride=2, return_indices=True),
    )

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

    self.decoder = nn.Sequential(
        ...
    )

def forward(self, x):
    encoded, indices = self.encoder(x)
    out = self.unpool(encoded, indices)
    out = self.decoder(out)
    return (out, encoded)
like image 57
Rooterbuster Avatar answered Oct 26 '25 18:10

Rooterbuster