一段concatenate 特征的代码:

 pretrained_model = models.resnet18(pretrained=True)

my_model1 = nn.Sequential(*list(pretrained_model.children())[:-1])
my_model2 = nn.Sequential(*list(pretrained_model.children())[:-1])

class Net(nn.Module):
def init(self):
super(Net, self).init()
self.feature1 = my_model1
self.feature2 = my_model2
self.fc = nn.Linear(1024, 117)

def forward(self, x,y):

    x1= self.feature1(x)
    x2= self.feature2(y)
    x3 = torch.cat((x1,x2),1)
    x3 = x3.view(x3.size(0), -1)
    x3 = self.fc(x3)
    return x3
net=Net()

p=Variable(torch.rand(4,3,240,240))
q=Variable(torch.rand(4,3,240,240))
o=net(p,q)

(附上原文作者的链接:https://discuss.pytorch.org/t/concat-features-from-multiple-pretrained-models-to-do-classification/8214

多特征融合。

 

class MyEnsemble(nn.Module):
    def __init__(self, modelA, modelB, nb_classes=10):
        super(MyEnsemble, self).__init__()
        self.modelA = modelA
        self.modelB = modelB
        # Remove last linear layer
        self.modelA.fc = nn.Identity()
        self.modelB.fc = nn.Identity()
        
        # Create new classifier
        self.classifier = nn.Linear(2048+512, 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)
        x = torch.cat((x1, x2), dim=1)
        
        x = self.classifier(F.relu(x))
        return x

# Train your separate models
# ...
# We use pretrained torchvision models here
modelA = models.resnet50(pretrained=True)
modelB = models.resnet18(pretrained=True)

# Freeze these models
for param in modelA.parameters():
    param.requires_grad_(False)

for param in modelB.parameters():
    param.requires_grad_(False)

# Create ensemble model
model = MyEnsemble(modelA, modelB)
x = torch.randn(1, 3, 224, 224)
output = model(x)

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐