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

相关推荐
艺杯羹13 小时前
古典密码学攻防演进全景:从凯撒移位、频率分析到一次一密与恩尼格玛机破译
网络·人工智能·安全·网络安全·密码学·密码安全
灰灰202613 小时前
我的 AI 工作流:多模型组队,IMA知识库做底座
人工智能
盗理者13 小时前
AI Agent 工程实践|让 AI Agent 自动修 Bug:定位、修改、测试与人工审核
人工智能·bug·agent
katttt_13 小时前
实体经营,优先选卡特加特垂域专用AI
人工智能·卡特加特
阿里云大数据AI技术13 小时前
EMR Daft × Qoder:一句话生成并运行智驾数据处理 Pipeline
人工智能
OceanBase数据库官方博客13 小时前
OceanBase Harness Engineering?AI 产品真正的瓶(技术解析与实践)
大数据·人工智能·oceanbase
兰亭妙微UI设计公司13 小时前
兰亭妙微 UX 实战:新手引导全类型设计入门指南
人工智能·ux
手写码匠13 小时前
华为云Flexus+DeepSeek征文|Dify 多 Agent 故障演练实战:用混沌工程主动“搞破坏“,让智能体系统越炸越稳
人工智能·深度学习·算法·aigc
Summer-Bright13 小时前
[马斯克600亿买下Cursor] —— AI 应用简报 08.13-08.17
人工智能·ai·ai应用·马斯克
内蒙深海大鲨鱼14 小时前
5.Introduction to PyTorch YouTube Series--visualable train
人工智能·pytorch·python