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

相关推荐
brave and determined8 分钟前
工程设计类学习(DAY25):高频PCB设计:射频布局与屏蔽腔体规范
人工智能·emc·电磁兼容·emi·屏蔽罩·电磁屏蔽·层叠设计
AI专业测评39 分钟前
2026年AI写作软件底层逻辑横评:长篇网文如何破局“上下文遗忘”与“AI消痕”?
人工智能·自然语言处理·ai写作
霖大侠1 小时前
CPAL: Cross-Prompting Adapter With LoRAs forRGB+X Semantic Segmentation
人工智能·深度学习·算法·机器学习·transformer
火山引擎开发者社区1 小时前
火山 AI 生态新玩法:ArkClaw 一键生成漫剧,效率直接拉满
人工智能
一起来学吧1 小时前
【OpenClaw系列教程】第一篇:OpenClaw 完整介绍——开源 AI 智能体平台
人工智能·ai·openclaw·养龙虾
飞Link1 小时前
工业级时序异常检测利器:USAD 算法深度解析与实战
人工智能·深度学习·机器学习
光锥智能2 小时前
家庭服务机器人爆发前夜,追觅扫地机抢跑下个时代
人工智能·机器人
JGDT_2 小时前
筑牢数字底座,驱动智慧未来——全方位数据中台解决方案
大数据·人工智能·科技·系统架构
balmtv2 小时前
GPT-5.4推理技术深度拆解:计算机使用、工具搜索与极限推理的架构实现
人工智能·gpt·架构
2501_933329552 小时前
舆情监测系统的技术演进:从数据采集到AI中台,Infoseek如何实现“监测+处置”一体化
开发语言·人工智能·自然语言处理·系统架构