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

相关推荐
2601_9654542313 小时前
AI视频制作:做历史建筑视频,宫殿园林的老图难找,AI怎么自动配古建素材?
运维·人工智能·音视频
Geek-Chow13 小时前
05.3 · DeepSeek 推理负载均衡详解:Prefill LB、Decode LB、VE LB 与 EPLB 冗余专家
人工智能
志尊宝13 小时前
AI原生前端开发(026):AI驱动的项目初始化与配置
人工智能·ai-native
Binovate缤江数智13 小时前
数智开关如何协同视频巡检?从小餐馆到连锁门店的用电安全架构解析
大数据·人工智能
明志数科13 小时前
人类视频与机器人示教的数据配比:分层训练的数据采购与采集设计
人工智能·算法
Flynn Liang13 小时前
Spring AI ChatClient 详解:system/user/assistant 角色、call 与 stream 到底怎么选
java·人工智能·spring·springai·流式输出·chatclient·java接入大模型
Ivanqhz13 小时前
MoE 原理、TC/EC 路由、LSH-MoE 与负载均衡浅析
运维·网络·人工智能·深度学习·算法·机器学习·负载均衡
码农小白AI13 小时前
无损检测 NDT 批量报告审核效率如何提升?IACheck AI 报告文档审核 Agent 优化批量报告审核
大数据·人工智能
hrrrrxeeeee13 小时前
不会编程适合考什么证书?AI应用认证与技术型认证的门槛区别
人工智能
shxjnpl13 小时前
粤语客服录音如何实现自动质检?从 ASR 语音识别到大模型分析
人工智能·语音识别·实时音视频