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

相关推荐
三掌柜66613 小时前
22秒攻击窗口下的防御重构:AI Threat Defense 与 Agent 安全护栏实践
人工智能·安全
七牛云行业应用13 小时前
WorkBuddy自定义模型失败怎么办?从接口鉴权到协议兼容的完整排查
人工智能·agent·ai编程
宸津-代码粉碎机13 小时前
Java面试核心:JVM三大GC垃圾回收算法原理与生产场景落地分析
java·大数据·人工智能·python·spring
蓝速科技13 小时前
蓝速 K10 三防平板在工业恶劣工况下的选型与应用指南
大数据·运维·数据库·人工智能·科技
小静AI工程实验室13 小时前
Codex 实用教程:Windows、macOS、Linux 安装配置到首个可验收项目
人工智能·python·开发工具·codex
Chloe.Zz13 小时前
提示词工程
人工智能·语言模型·自然语言处理
知了一笑13 小时前
AI也没想到,三年红透半边天
人工智能·ai·互联网·aigc·职场
zwd200513 小时前
Mac 上写英文遇到不会的词,有哪些工具?输入法、翻译软件、写作助手怎么选
人工智能·macos·效率工具·英语写作·输入法·deepl·翻译工具
Easy88AI13 小时前
DeepSeek-V4.1-Flash 接入实战:从官方 API 到聚合网关(以 Easy88AI 为例)
人工智能·深度学习
WUYOUGYLU13 小时前
大模型时代:人工智能如何重塑我们的工作与生活
人工智能