预先训练的重新NET模型中的偏差术语不可用吗?

发布于 2025-02-09 22:27:41 字数 885 浏览 3 评论 0 原文

我正在使用Resnet进行神经网络分类,并希望尝试在预先训练和未经训练的网络之间进行比较。但是,我确实想使用偏置项,这不是Pytorch的Resnet模块中的默认设置。

是否有一种方法可以包括预先训练的模型并在其中使用偏差术语?

我当前代码的简短片段,我从这里重新定义了重新连接体系结构 - set bias = true true

net = resnet18(pretrained=True)
net.fc = nn.Linear(512, num_classes)

现在明显的错误现在是

resnet加载state_dict中的错误(s): state_dict中缺少键:“ conv1.bias”,“ layer1.0.conv1.bias”,“ layer1.0.conv2.bias”,“ layer1.1.conv1.bias”,“ layer1.conv2 .bias“,” layer2.0.conv1.bias”,“ layer2.0.conv2.bias”,“ layer2.0.downsample.0.bias”,“ layer2.1.conv1.bias”,“ layer2.1 。 .1.conv2.bias“,” layer4.0.conv1.bias”,“ layer4.0.conv2.bias”,“ layer4.0.downsample.0.bias”,“ layer4.1.conv1.bias”, “ layer4.1.conv2.bias”。

I am using a ResNet for neural network classification and wish to try out a comparison between pre-trained and non-pre-trained networks. However, I do want to use the Bias term which is not the default setting in Pytorch's ResNet modules.

Is there a way to include a pre-trained model and use bias terms on top of that?

A very brief snippet of my current code, I redefine ResNet architecture from here - https://pytorch.org/vision/0.8/_modules/torchvision/models/resnet.html and set Bias = True

net = resnet18(pretrained=True)
net.fc = nn.Linear(512, num_classes)

The obvious error right now is

Error(s) in loading state_dict for ResNet:
Missing key(s) in state_dict: "conv1.bias", "layer1.0.conv1.bias", "layer1.0.conv2.bias", "layer1.1.conv1.bias", "layer1.1.conv2.bias", "layer2.0.conv1.bias", "layer2.0.conv2.bias", "layer2.0.downsample.0.bias", "layer2.1.conv1.bias", "layer2.1.conv2.bias", "layer3.0.conv1.bias", "layer3.0.conv2.bias", "layer3.0.downsample.0.bias", "layer3.1.conv1.bias", "layer3.1.conv2.bias", "layer4.0.conv1.bias", "layer4.0.conv2.bias", "layer4.0.downsample.0.bias", "layer4.1.conv1.bias", "layer4.1.conv2.bias".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北恋 2025-02-16 22:27:41

来更改摘要中给出的_Resnet函数,它应该忽略非匹配键并避免崩溃

def _resnet(arch, block, layers, pretrained, progress, **kwargs):
    model = ResNet(block, layers, **kwargs)
    if pretrained:
        state_dict = load_state_dict_from_url(model_urls[arch],
                                              progress=progress,
                                              strict=False)
        model.load_state_dict(state_dict)
    return model

您应该通过添加strict = false

You should change the _resnet function given in the snippet

def _resnet(arch, block, layers, pretrained, progress, **kwargs):
    model = ResNet(block, layers, **kwargs)
    if pretrained:
        state_dict = load_state_dict_from_url(model_urls[arch],
                                              progress=progress,
                                              strict=False)
        model.load_state_dict(state_dict)
    return model

By adding strict=False it should ignore non matching keys and avoid crashing

倾其所爱 2025-02-16 22:27:41

默认_Resnet函数应如下更改:

def _resnet(arch, block, layers, pretrained, progress, **kwargs):
    model = ResNet(block, layers, **kwargs)
    if pretrained:
        state_dict = load_state_dict_from_url(model_urls[arch],
                                              progress=progress)
        model.load_state_dict(state_dict, strict=False)
    return model

这将使我们从state_dict加载预训练的权重并忽略非匹配键。

The default _resnet function should be changed as follows:

def _resnet(arch, block, layers, pretrained, progress, **kwargs):
    model = ResNet(block, layers, **kwargs)
    if pretrained:
        state_dict = load_state_dict_from_url(model_urls[arch],
                                              progress=progress)
        model.load_state_dict(state_dict, strict=False)
    return model

This would let us load the pre-trained weights from state_dict and ignore the non-matching keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文