Costumized ensemble from pre-trained models

Hi all,

I have trained three separate pre -trained models (squeeznet, resnet, alexnet) and I want to create an ensemble. This how my code look like but when i do model.eval(), I got the error : RuntimeError: mat1 dim 1 must match mat2 dim 0
Can you please help me out? Where I am doing wrong?

Thank you so much!

class MyEnsemble(nn.Module):
def init(self, modelA, modelB, nb_classes=11):
super(MyEnsemble, self).init()
self.modelA = modelA
self.modelB = modelB
self.modelC= modelC
# Remove last linear layer

self.modelA.fc = nn.Identity()

self.modelB.classifer = nn.Identity()

    # Remove last linear layer
    self.modelA.classifier = nn.Identity()
    self.modelB.fc = nn.Identity()
    self.modelC.classifier = nn.Identity()
    
    # Create new classifier
    self.classifier = nn.Linear(512 + 2048 + 4096, nb_classes)
    
def forward(self, x):
    x1 = self.modelA(x.clone())  # clone to make sure x is not changed by inplace methods
    x1 = x1.view(x1.size(0), -1)
    
    x2 = self.modelB(x)
    x2 = x2.view(x2.size(0), -1)
    
    x3 = self.modelB(x)
    x3 = x2.view(x2.size(0), -1)
    
    x = torch.cat((x1,x2,x3), dim=1)
    x = self.classifier(nn.functional.relu(x))
    return x

Based on the error message I guess that the number of input features in self.classifier doesn’t match the feature dimension for x.
Print the shape of x after using torch.cat and before feeding it to self.classifier and adapt the in_features if necessary.

Thank you so much! I check that out and the in_features I was passing to the self_classifier was wrong! Thank you so much for your reply, now it’s working!