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

相关推荐
数智前线5 小时前
AI Coding,正在把存储推向前台
人工智能
论文复现现场10 小时前
AutoDL、算家云与公有云 GPU 怎么选?环境复现、计费与断点恢复对比
pytorch·深度学习·云计算·gpu
唠点键盘之外的10 小时前
15 微调 vs RAG:到底怎么选
人工智能·机器学习·面试·aigc
具身AGI11 小时前
人机协同预训练:三条路线,一条拉开差距
人工智能
东风破_11 小时前
LangSmith:从链路追踪到 RAG 自动化评估
人工智能
东方芷兰11 小时前
Agent 技术摘要 04 —— AI4S、VLM、VLA、VLN、WM、AI Infra
人工智能
catchadmin11 小时前
用 Jev 与 Laravel AI SDK 检测垃圾邮件和自动回复
数据库·人工智能·laravel
一 铭12 小时前
Pi实战 05:本地模型 · MCP · 安全沙箱篇
人工智能·ai·agent·harness
198******1263412 小时前
2026企业AI办公工具选型指南:从评估框架到场景适配
大数据·运维·人工智能
OxYGC12 小时前
[AI工程]Jev 决策模型第二篇:三原语、一次多问与置信度路由,从 Playground 到能上线的代码
人工智能