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

相关推荐
DisonTangor8 小时前
【字节拥抱开源】Lance: 多任务协同的统一多模态建模
人工智能·ai作画·开源·aigc
冬奇Lab8 小时前
Agent系列(七):知识库集成——Agent 调用 RAG 的正确姿势
人工智能·agent
ITyunwei09878 小时前
主流 SaaS 工单系统对比
运维·服务器·人工智能
青风979 小时前
SDDGR:基于稳定扩散的深度生成重放,用于类增量对象检测(CVPR 2024)
网络·人工智能·深度学习·神经网络·计算机视觉
冬奇Lab9 小时前
一天一个开源项目(第114篇):stop-slop - 一个教 AI 消除自身写作口癖的 Skill 文件
人工智能
天青色等烟雨..9 小时前
R+VIC模型融合实践技术应用及未来气候变化模型预测
大数据·人工智能·arcgis·语言模型·数据分析
云栖梦泽在9 小时前
AI安全实战:AI系统应急响应的实战演练案例
大数据·人工智能·安全
wanzehongsheng9 小时前
户外追日光伏技术对比:双轴太阳花与三轴智能太阳花场景适配分析
人工智能·能源·光伏·光伏支架·光伏太阳花
北辰alk9 小时前
AI Agent 记忆系统架构设计:OpenClaw、Claude Code、Hermes Agent 深度对比
人工智能
忆~遂愿9 小时前
《大模型驱动软件测试》| 软件工程3.0时代,大模型驱动测试实战指南
人工智能·深度学习·神经网络·机器学习·自然语言处理·软件工程·知识图谱