Hello. I trained an autoencoder/decoder and saved the model.  I loaded the model and took the decoder portion off, in order to extract the features from the middle of the encoder.  But when i run the encoder, it states the following error;

AttributeError: ‘tuple’ object has no attribute ‘dim’

I thought tensors flowed through the entire object but maybe not.  How do I get tensors of features out of this encoder?  Code below:

class DenoisingAutoencoder(nn.Module):
def __init__(self):

    super(DenoisingAutoencoder, self).__init__()
                                                        # 32 x 32 x 3 (input)

    self.conv1e = nn.Conv2d(3, 24, 3, padding=2)        # 30 x 30 x 24
    self.conv2e = nn.Conv2d(24, 48, 3, padding=2)       # 28 x 28 x 48
    self.conv3e = nn.Conv2d(48, 96, 3, padding=2)       # 26 x 26 x 96
    self.conv4e = nn.Conv2d(96, 128, 3, padding=2)      # 24 x 24 x 128
    self.conv5e = nn.Conv2d(128, 256, 3, padding=2)     # 22 x 22 x 256
    self.mp1e   = nn.MaxPool2d(2, return_indices=True)  # 11 x 11 x 256

    self.mp1d = nn.MaxUnpool2d(2)
    self.conv5d = nn.ConvTranspose2d(256, 128, 3, padding=2)
    self.conv4d = nn.ConvTranspose2d(128, 96, 3, padding=2)
    self.conv3d = nn.ConvTranspose2d(96, 48, 3, padding=2)
    self.conv2d = nn.ConvTranspose2d(48, 24, 3, padding=2)
    self.conv1d = nn.ConvTranspose2d(24, 3, 3, padding=2)
    

def forward(self, x):
    # Encoder
    x = self.conv1e(x)
    x = F.relu(x)
    x = self.conv2e(x)
    x = F.relu(x)
    x = self.conv3e(x)
    x = F.relu(x)
    x = self.conv4e(x)
    x = F.relu(x)
    x = self.conv5e(x)
    x = F.relu(x)
    x, i = self.mp1e(x)
    
     # Decoder
    x = self.mp1d(x, i)
    x = self.conv5d(x)
    x = F.relu(x)
    x = self.conv4d(x)
    x = F.relu(x)
    x = self.conv3d(x)
    x = F.relu(x)
    x = self.conv2d(x)
    x = F.relu(x)
    x = self.conv1d(x)
    x = F.relu(x)
    
    return x


Then I bring it back and take off the decoder:

model = DenoisingAutoencoder()
model.load_state_dict(torch.load( “C:\Users\jordan.howell\Pytorch\UW_files\roof_autoencoder.pt”))
#pop off the decoder
new_model = nn.Sequential(*list(model.children())[:-6])
model = nn.Sequential(*new_model)
model.feature_vec = nn.Sequential(nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, 128))
model.cuda()
model.eval()

I’m not sure what I’m doing wrong.