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

相关推荐
Agent产品评测局2 分钟前
物流供应链自动化解决方案选型,全链路提效指南:从硬件集成到AI Agent的演进路径
运维·人工智能·ai·chatgpt·自动化
FPGA-ADDA3 分钟前
第五篇(下):智能无线电与6G候选技术——从机器学习到通感一体化
人工智能·机器学习·信号处理·fpga·通信系统
Omics Pro6 分钟前
空间组学下一代机器学习与深度学习
大数据·人工智能·深度学习·算法·机器学习·语言模型·自然语言处理
七七powerful7 分钟前
运维养龙虾--Nacos 3.0 + MCP Router:打造 AI 原生应用的服务治理新范式
人工智能·nacos·nacos mcp
新缸中之脑8 分钟前
用Stitch和AI Studio改造应用UI
人工智能·ui
花千树-01010 分钟前
5分钟用 Java 构建你的第一个 AI 应用
java·人工智能·spring boot·langchain·aigc·ai编程
学术头条11 分钟前
清华、智谱团队提出Vision2Web:基于Agent验证评估视觉网站开发
人工智能·科技·ai
北京软秦科技有限公司11 分钟前
AI报告文档审核深度赋能化工行业质量管理:IACheck驱动报告质量跃升与合规风险精准管控新范式
大数据·人工智能
Goodwin23 分钟前
TypeScript 成 AI 应用层标配?GitHub Trending 告诉你2026前端往哪走
前端·人工智能·github
mit6.82424 分钟前
Pendulum
人工智能