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

相关推荐
正经教主13 分钟前
【FDE系列】阶段3:Day 58:Prompt 安全 — 注入、越狱与防护
网络·人工智能·安全·prompt·fde
TechEdu20260622 分钟前
[人工智能]Python09:numpy.random.Statistics统计实战指南
人工智能·numpy
可乐ea30 分钟前
AI Agent 工具调用准确性评测:选择错误与参数错误分开测
大数据·人工智能·算法·大模型·工具调用·ai智能体·agent评测
昨日之日200635 分钟前
yovoice:本地配音工具箱,支持音色克隆与情绪控制,专为旁白、有声书和视频配音打造
人工智能·音视频
DP DPharness36 分钟前
选型时怎么比:dsh-knowledge 与三类 RAG 方案的维度对照
人工智能·dpharness
数智顾问1 小时前
(90页PPT)IBM集团管理驾驶舱项目蓝图规划(附下载方式)
大数据·人工智能·物联网
二川bro1 小时前
Windows下Claude Code从安装到落地完整踩坑记录
人工智能
ZzT1 小时前
Claude Code Mods 是什么:给 Claude 加工具、在终端画界面
人工智能·ai编程·claude
I'mChloe1 小时前
Windows部署BiliNote:Docker安装、AI视频转写、Markdown笔记与cpolar远程访问
人工智能·windows·docker
喜欢打篮球的普通人1 小时前
MiniMind 学习笔记(十二):Pretrain 实操——从版本梳理到 8GB 显卡上的真实训练
人工智能·笔记·学习