使用pytorch搭建ResNet并基于迁移学习训练

这里的迁移学习方法是载入预训练权重的方法

python 复制代码
    net = resnet34()
    # load pretrain weights
    # download url: https://download.pytorch.org/models/resnet34-333f7ec4.pth
    model_weight_path = "./resnet34-pre.pth"
    assert os.path.exists(model_weight_path), "file {} does not exist.".format(model_weight_path)
    net.load_state_dict(torch.load(model_weight_path, map_location='cpu'))
    # for param in net.parameters():
    #     param.requires_grad = False

    # change fc layer structure
    in_channel = net.fc.in_features
    net.fc = nn.Linear(in_channel, 5)

这里的迁移学习方法是载入预训练权重的方法net = resnet34():注意这里没有传入参数num_classes 因为后面才载入所有的参数,会覆盖我们设定的classes

change fc layer structure

in_channel = net.fc.in_features # fc 为全连接层 in_features为特征矩阵的深度

net.fc = nn.Linear(in_channel, 5)

如果不想使用迁移学习的方法,则注释阴影部分,在net = resnet34()中传入num_classes参数

相关推荐
IvanCodes25 分钟前
OpenClaw保姆级安装教程:windows&ubuntu
人工智能
Serverless社区1 小时前
AgentRun实践指南:Agent 的宝藏工具—All-In-One Sandbox
人工智能
AngelPP2 小时前
拆解 OpenClaw 上下文引擎:一个 AI Agent 是如何管理"记忆"的
人工智能
老纪的技术唠嗑局2 小时前
OpenClaw + 6 个 Agent 运转半个月,从聊天到干活的完整工程实践
人工智能
Guo白给2 小时前
openclaw飞书部署手册
人工智能
itpretty2 小时前
如何用 skill-creator 创建、测试和优化 skill
人工智能·claude
蓝戒札记2 小时前
效率神器 NotebookLM:构建高质量知识库的完整教程与落地最佳实践
人工智能
chaors2 小时前
Langchain入门到精通0x02:ICEL
人工智能·langchain·ai编程
吴佳浩2 小时前
什么是算力?
人工智能·pytorch·llm
charlex3 小时前
【陈同学】走进 AI Agent:从“对话框”到“自主智能体”
人工智能·agent