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

相关推荐
zyplayer-doc1 分钟前
升级表格编辑器,AI客服应用支持转人工客服,AI问答风格与性能优化,zyplayer-doc 2.5.6 发布啦!
人工智能·编辑器·飞书·开源软件·创业创新·有道云笔记
~~李木子~~3 小时前
中文垃圾短信分类实验报告
人工智能·分类·数据挖掘
TsingtaoAI7 小时前
企业实训|自动驾驶中的图像处理与感知技术——某央企汽车集团
图像处理·人工智能·自动驾驶·集成学习
王哈哈^_^7 小时前
YOLO11实例分割训练任务——从构建数据集到训练的完整教程
人工智能·深度学习·算法·yolo·目标检测·机器学习·计算机视觉
檐下翻书1738 小时前
从入门到精通:流程图制作学习路径规划
论文阅读·人工智能·学习·算法·流程图·论文笔记
SalvoGao8 小时前
Python学习 | 怎么理解epoch?
数据结构·人工智能·python·深度学习·学习
搬砖者(视觉算法工程师)9 小时前
自动驾驶汽车技术的工程原理与应用
人工智能·计算机视觉·自动驾驶
CV实验室9 小时前
2025 | 哈工大&鹏城实验室等提出 Cascade HQP-DETR:仅用合成数据实现SOTA目标检测,突破虚实鸿沟!
人工智能·目标检测·计算机视觉·哈工大
aitoolhub9 小时前
培训ppt高效制作:稿定设计 + Prompt 工程 30 分钟出图指南
人工智能·prompt·aigc
oranglay9 小时前
提示词(Prompt Engineering)核心思维
人工智能·prompt