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