修改Resnet 50 和VGG16 FC 层输出进行迁移学习方法仅供参考
Resnet50
方法一:
resnet50 = models.resnet50(pretrained=True)
print('Before:{%s}\n' % resnet50)
for param in resnet50.parameters(): param.requires_grad = False
fc_inputs = resnet50.fc.in_features
resnet50.fc = nn.Linear(fc_inputs,config.class_number) # config.class_number 目标分类数
***#注意后面Loss用cross_entropy, cross_entropy 相当于logsoftmax()+NLLloss()***
loss = nn.CrossEntropyLoss()
方法二:
resnet50 = models.resnet50(pretrained=True)
print('Before:{%s}\n' % resnet50)
for param in resnet50.parameters(): param.requires_grad = False
num_ftrs = resnet50.fc.in_features
resnet50.fc = nn.Sequential(nn.Linear(num_ftrs, config.class_number), # config.class_number 目标分类数,
nn.LogSoftmax(dim=1))
***#注意后面Loss用NLLloss(), 因为已经定义了nn.LogSoftmax(dim=1)***
loss = nn.NLLLoss()
VGG16
方法一:
vgg = models.vgg16(pretrained=True)
print('Before:{%s}\n' % vgg)
for param in vgg.features.parameters(): param.requires_grad = False
vgg.classifier = nn.Sequential(torch.nn.Linear(25088, 4096),
torch.nn.ReLU(),
torch.nn.Dropout(p=0.5),
torch.nn.Linear(4096, 4096),
torch.nn.ReLU(),
torch.nn.Dropout(p=0.5),
torch.nn.Linear(4096, config.class_number))
print('========================================')
print('After:{%s}\n' % vgg)
loss_function = nn.CrossEntropyLoss()
方法二:
vgg = models.vgg16(pretrained=True)
print('Before:{%s}\n' % vgg)
for param in vgg.features.parameters(): param.requires_grad = False
vgg.classifier[6] = nn.Linear(4096, config.class_number)
print('========================================')
print('After:{%s}\n' % vgg)
loss_function = nn.CrossEntropyLoss()
方法三: 方法三参考的是Vishnu Subramanian 的Deep Learning with PyTorch 书,但是不知道为什么修改后打印模型显示的输出out_features个数已经更改,Debug查看output.size 维度还是1000,不建议使用。
vgg = models.vgg16(pretrained=True)
print('Before:{%s}\n' % vgg)
for param in vgg.features.parameters(): param.requires_grad = False
# Freeze layers
vgg.classifier[6].out_features = config.class_number # transfer learning, out_features for class number 这种修改方法还是有问题
print('========================================')
print('After:{%s}\n' % vgg)
loss_function = nn.CrossEntropyLoss()
来源:CSDN
作者:Zachary Zheng
链接:https://blog.csdn.net/zachary_hao/article/details/104748866