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

相关推荐
敢敢のwings12 小时前
TACDYN-WAM 深度解读:机器人如何预测接触变化,并据此生成动作
人工智能·机器学习·机器人
沉默王二12 小时前
阿里版Codex,再次惊艳到我了。
人工智能·openai·agent
正在走向自律12 小时前
【1/5】人工智能训练师 一级/高级技师 考证学习方向:从“可评审的交付”到“可定义的标准与可传承的体系”
人工智能·人工智能训练师·一级/特级·ai技术负责人·数据体系负责人·行业解决方案架构·技术评审与标准制定
时文鲜读12 小时前
算力基建提速,数据中心安全建设与工业可靠性如何同步升级?
大数据·人工智能·安全
坐吃山猪12 小时前
果蝇AI07-Mac部署踩坑
人工智能·深度学习·机器学习·果蝇ai
挖掘狂人12 小时前
AI PC 分水岭:2026 过半 PC 搭载本地 AI,端云协同才是核心
人工智能·程序员·ai编程
吴名氏.13 小时前
Java转AI Agent教程(第九周)错误矩阵
java·人工智能·矩阵·ai agent
lisw0513 小时前
作为人类代理的大语言模型及其角色分类!
人工智能·学习·语言模型
高洁0113 小时前
自然语言处理的技术演进与核心任务
人工智能·python·深度学习·机器学习·迁移学习
Jsundoku13 小时前
第十四章 深度学习 -- 基于模型的方法
人工智能·强化学习