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

相关推荐
johnsong12 小时前
AI前沿日报 · 2026-11-05:暗网与裂痕
人工智能
张彦峰ZYF12 小时前
升级了不等于变好了:AI回归测试的固定评测集、版本对比、质量阈值与CI门禁
人工智能·后端·模型或提示词升级·语言模型没有编译器与确定性输出·总体、切片与回退清单·红线、护栏与目标·ci门禁把判定写进合并路径
迪康coolmu12 小时前
企业U盘管控落地实战:基于迪康端点安全一体化管理系统的移动存储安全实践
运维·人工智能·经验分享·笔记·安全·电脑
GitCode官方12 小时前
活动预告 | 模力无限 × AtomGit珠海首场 AI Meetup,来了
人工智能·atomgit
QXWZ_IA13 小时前
电离层扰动影响RTK精度的机理与工程应对方法
人工智能
在所不辞兄13 小时前
为什么生成对抗网络训练常不稳定,模式崩溃与判别器过强之间是什么关系?
人工智能·深度学习·神经网络·机器学习
严肃小白兔13 小时前
Cloudflare 开始区分 AI 爬虫与个人 Agent:网站访问控制应该怎样设计?
人工智能·爬虫
vipxieliang14 小时前
端侧芯片的「每瓦 Token 数」竞赛:端侧 AI 如何重塑芯片设计的底层逻辑
人工智能·openai
AI小百科14 小时前
AI 进游戏:游戏里的 NPC 开始有自己的性格
人工智能·游戏·ai