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

相关推荐
deming_su28 分钟前
AI产品架构师核心理论知识点文档
人工智能
XD74297163631 分钟前
科技晚报|2026年5月13日:AI 开始补全库审查、移动入口和弹性调度
人工智能·科技·开发者工具·科技晚报
卷Java1 小时前
2026年4月AI军备竞赛全景:DeepSeek V4 vs GPT-5.5 vs Gemini vs Claude
人工智能·gpt·大模型
人月神话-Lee1 小时前
【图像处理】亮度与对比度——图像的线性变换
图像处理·人工智能·ios·ai编程·swift
shchojj1 小时前
Generative AI applications -- Chatting
人工智能
易观Analysys1 小时前
重构与崛起——OpenClaw时代的中国Agent产业生态报告
人工智能
kishu_iOS&AI1 小时前
NLP —— 英译法实例
人工智能·ai·自然语言处理
Alter12302 小时前
从“力大砖飞”到“拟态共生”,新华三定义AI基础设施的系统级进化
大数据·运维·人工智能
哔哩哔哩技术2 小时前
bili-fe-workflow —商业化智能开发工作流实践
人工智能
王木风2 小时前
终端里的编程副驾:DeepSeek-TUI-项目深度拆解,实测与原理分析
linux·运维·人工智能·rust·node.js