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

相关推荐
TeDi TIVE17 分钟前
开源模型应用落地-工具使用篇-Spring AI-高阶用法(九)
人工智能·spring·开源
MY_TEUCK25 分钟前
Sealos 平台部署实战指南:结合 Cursor 与版本发布流程
java·人工智能·学习·aigc
三毛的二哥29 分钟前
BEV:典型BEV算法总结
人工智能·算法·计算机视觉·3d
j_xxx404_1 小时前
大语言模型 (LLM) 零基础入门:核心原理、训练机制与能力全解
人工智能·ai·transformer
飞哥数智坊1 小时前
全新 SOLO 在日常办公中的实际体验
人工智能·solo
<-->1 小时前
Megatron(全称 Megatron-LM,由 NVIDIA 开发)和 DeepSpeed(由 Microsoft 开发)
人工智能·pytorch·python·深度学习·transformer
朝新_1 小时前
【Spring AI 】图像与语音模型实战
java·人工智能·spring
Yuanxl9031 小时前
神经网络-Sequential 应用与实战
人工智能·深度学习·神经网络
火山引擎开发者社区2 小时前
Seedance 2.0 1080P 生成能力正式上线
人工智能
冬奇Lab2 小时前
一天一个开源项目(第79篇):生化危机女主角亲自开源的 AI 记忆系统 MemPalace
人工智能·开源·资讯