使用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参数

相关推荐
SeatuneWrite3 分钟前
**AI仿真人剧工具2025推荐,解锁沉浸式互动叙事新体验*
人工智能·python
l1t4 分钟前
利用网易有道龙虾调用ollama本地模型生成幻灯片内容
人工智能·爬虫
b1ue1ue1ue6 分钟前
LLM Note
人工智能
小雨中_16 分钟前
2.1 PaLM 及其变体(PaLM / PaLM 2)
人工智能·深度学习·机器学习·分类·数据挖掘·palm
pccai-vip17 分钟前
OpenClaw(原名 Clawdbot/Moltbot)正处于从“聊天助手”向“自主智能体(Agent)”转型的关键期
人工智能
一个努力编程人26 分钟前
NLP 领域————BERT算法
人工智能·自然语言处理·bert
aircrushin30 分钟前
Gemini 3.1 Pro架构深度解析与AGI能力评测
人工智能
deephub30 分钟前
并行多智能体系统的协调测试实战:从轨迹捕获到CI/CD的六个步骤
人工智能·ci/cd·大语言模型·aiagent
yunhuibin33 分钟前
LeNet、AlexNet、VGGNet、NiN总结
人工智能·python·深度学习·神经网络
Emotional。38 分钟前
AI Agent 开发实战:用 LangChain 构建智能邮件助手
linux·服务器·网络·人工智能·python·langchain