pytorch 修改预训练model

    class Net(nn.Module):
        def __init__(self , model):
            super(Net, self).__init__()
            #取掉model的后两层
            self.resnet_layer = nn.Sequential(*list(model.children())[:-2])
            self.transion_layer = nn.ConvTranspose2d(2048, 2048, kernel_size=14, stride=3)
            self.pool_layer = nn.MaxPool2d(32)  
            self.Linear_layer = nn.Linear(2048, 8)
            
        def forward(self, x):
            x = self.resnet_layer(x)
            x = self.transion_layer(x)
            x = self.pool_layer(x)
            x = x.view(x.size(0), -1) 
            x = self.Linear_layer(x) 
            return x

resnet = models.resnet50(pretrained= True)
model = Net(resnet)

参考: https://blog.csdn.net/whut_ldz/article/details/78874977

你可能感兴趣的:(pytorch 修改预训练model)