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

相关推荐
漂流瓶jz17 分钟前
【AI】大模型本地部署与量化:Ollama、transformers、llama.cpp实践
人工智能·llm·ai编程
飞哥数智坊2 小时前
AI时代,我们到底该学什么、用什么,为什么还没提效?
人工智能
飞哥数智坊2 小时前
GPT 做架构,国内模型写代码:这条 AI 开发路线能跑通吗?
人工智能·ai编程
AI_AGENT_DEV_AI3 小时前
AI 智能体的开发与上线
人工智能
VL——MOESR3 小时前
【具身智能】VLA论文阅读随笔
论文阅读·人工智能·机器学习·具身智能·vla
OCR_133716212753 小时前
从模板匹配到AI泛化识别:文本抽取如何重构护照阅读器落地生态
人工智能·重构
大唐荣华3 小时前
从OpenAI关停Sora看世界模型、AI视频与国内具身智能赛道路线分化
人工智能·openai·sora·内容生成
DevOps老兵3 小时前
AI Infra实战02:GPU监控实战,用DCGM+Prometheus+Grafana看清每一张卡
人工智能·grafana·prometheus·ai infra·gpu监控·dcgm
旧日之血_Hayter3 小时前
Antigravity 工作流的实践记录
人工智能
dozenyaoyida3 小时前
AI与大模型新闻日报 | 2026-09-01
人工智能·ai·大模型·新闻