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

相关推荐
孙启超1 小时前
【大模型应用开发】LLM 到底是什么,以及它是怎么训练的
人工智能·lora·llm·微调·sft·token·rlhf
新知图书2 小时前
7.1 需求分析与规划 《AI Agent智能体开发实践》
人工智能·agent·ai agent·智能体
clorinda2 小时前
机器学习文本分类入门:从数据清洗到中文评论词向量转换
人工智能
小宋10212 小时前
Dify 知识库实战:从 PDF 导入到带引用回答,完整搭建企业问答助手
人工智能·ai编程
Lee_jerome2 小时前
从 PyTorch 权重到 RK3588 板端推理:ResNet18 二分类模型完整部署教程
pytorch·边缘计算·rk3588·模型部署·onnx·resnet18·int8量化
用户938515635073 小时前
从Vibe Coding到SDD:规范驱动开发如何拯救AI编程失控
人工智能
蓝速科技3 小时前
蓝速科技桌面 AI 双屏翻译机:开放安卓系统商用价值解析
人工智能·科技
博、、3 小时前
社区家政平台开发实战指南:从需求分析到系统部署全流程解析
人工智能·数据挖掘·需求分析
Python私教4 小时前
别急着加 llms.txt:企业官网面向 AI 搜索的工程清单
前端·人工智能·seo
Tokenge4 小时前
AI 前沿日报|2026.08.19:模型安全按下减速键,智能体进入“可控落地”新阶段
人工智能·安全