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

相关推荐
TMT星球5 分钟前
地瓜机器人2026工博会首秀:端云一体全栈AI底座,覆盖视觉检测、工业数采与具身作业
人工智能·机器人·视觉检测
TechEdu2026067 分钟前
[人工智能]Python12: SciPy 实战指南
人工智能·python·scipy
海宇服务10 分钟前
零信任架构实战:基于海宇股权穿透构建自动化招投标供应商关联排查网关
运维·人工智能·架构·自动化
qq_3646821510 分钟前
读书笔记如何快速整理?
人工智能·金融
9i编程22 分钟前
11. 教 AI 上班:带出我的数字同事 —— 前后端部署上线 130,按 v1.7 回归清零与总结
人工智能·openai·ai编程
踏雪亦无痕24 分钟前
论文笔记:Distilling the Knowledge in a Neural Network
论文阅读·人工智能·深度学习·知识蒸馏
soft_hub127 分钟前
如何从采访录音中提取新闻重点?
人工智能
Allen_LVyingbo29 分钟前
生命过程的分子语言:AI 解析范式与场景应用全景研究(下)
大数据·人工智能·知识图谱·健康医疗·数据库开发·数据库架构
豆角焖肉29 分钟前
SpringBoot 集成腾讯 AI 人脸识别
人工智能·spring boot·后端·人脸识别·腾讯 ai·base64 图片
AI情绪识别开源31 分钟前
检信 ALLEMOTION AI驱动振动感知健康分析平台多维度技术参数
人工智能·宽度优先·推荐算法