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

相关推荐
辛迪聊物业数字化14 小时前
物业费收缴率从68%提升到97%落地复盘:中小物业收费系统选型避坑指南
人工智能·嵌入式硬件·物联网·网络协议·系统架构
科技之门14 小时前
大捷智能携手中科曙光打造工业AI设计超级云平台——AI设计应用进入快车道!
人工智能
4SAPI15 小时前
营销内容生成大模型 API 生产选型指南:4SAPI、硅基流动与 OpenRouter 的架构权衡
数据库·人工智能·架构·php
lazy熊15 小时前
AI 编程工具跑通后,第一条任务应该怎么写?
人工智能
天树星智源15 小时前
J-Comm:不传文本传雅可比坐标,token 省下 74.8%
人工智能·机器人·大模型
谢白羽15 小时前
从传统运维到 AI Infra: K8s 上调度 100 张 GPU踩坑笔记
运维·人工智能·缓存·kubernetes·vllm·sglang·vllm-omni
镜子AI15 小时前
镜子AI工程实践:用熵权法 + TOPSIS 做多方案选型排序(零依赖 Python)
开发语言·人工智能·python
维基框架15 小时前
OpenAI GPT-6.1 Terra发布AI开始真正操作电脑,使用成本也降了
大数据·人工智能·gpt
志尊宝15 小时前
AI原生前端开发(028):AI辅助的调试与错误修复
人工智能·笔记·重构·ai-native
海宇服务15 小时前
零信任架构实战:基于海宇个人大数据报告构建自动化汽车融资租赁资信核验网关
大数据·人工智能·架构·自动化