【问题标题】:Freezing conv layers in pre trained VGG16 model在预训练的 VGG16 模型中冻结卷积层
【发布时间】:2021-09-30 13:40:49
【问题描述】:
model_conv = torchvision.models.vgg16(pretrained=True)
for param in model_conv.parameters():
    param.requires_grad = False
model_conv.classifier.requires_grad_=True
model_conv.classifier[6].out_features=len(class_names)

model_conv = model_conv.to(device)

criterion = nn.CrossEntropyLoss()

optimizer_conv = optim.SGD(model_conv.classifier.parameters(), lr=0.001, momentum=0.9)

exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=7, gamma=0.1)

在 VGG16 模型中,我想在我的图像上训练分类器层并冻结卷积层。我遇到了同样的错误。

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

【问题讨论】:

  • 请注意对答案提供反馈,这是我第四次在没有收到任何回复的情况下回答您的问题(请参阅hereherehere 和 @987654324 @...)。
  • 对不起,伙计,我现在提醒自己回复答案。我通常忘记将它们标记为解决方案。从现在开始会这样做。

标签: pytorch computer-vision


【解决方案1】:

首先requires_grad_ 是一个就地函数,而不是你可以做的属性:

>>> model_conv.classifier.requires_grad_(True)

或者直接修改requires_grad属性(就像你在for循环中所做的那样):

>>> model_conv.classifier.requires_grad = True

第二,不能通过覆盖out_features来改变层中神经元的数量。这个属性只包含神经元的数量,对层的底层内容没有影响。您需要用新初始化的层覆盖该层。例如:

>>> classifier = model_conv.classifier[6]
>>> model_conv.classifier[6] = nn.Linear(classifier.in_features, class_names)

一个最小的例子是:

model_conv = torchvision.models.vgg16(pretrained=True).requires_grad_(False)
clf = model_conv.classifier[6]
clf.out_features=nn.Linear(clf.in_features, len(class_names))

optimizer_conv = optim.SGD(model_conv.classifier.parameters(), lr=0.001, momentum=0.9)
x = torch.rand(1,3,100,100, requires_grad=True)
model_conv(x).mean().backward()
optimizer_conv.step()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-27
    • 2018-12-11
    • 2020-06-13
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    相关资源
    最近更新 更多