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

相关推荐
橙露2 小时前
数据特征工程:缺失值、异常值、标准化一站式解决方案
人工智能·机器学习
新加坡内哥谈技术2 小时前
OpenAI 的 Codex 团队如何工作并利用 AI
人工智能
星河耀银海2 小时前
人工智能大模型的安全与隐私保护:技术防御与合规实践
人工智能·安全·ai·隐私
love530love2 小时前
Scoop 完整迁移指南:从 C 盘到 D 盘的无缝切换
java·服务器·前端·人工智能·windows·scoop
njsgcs2 小时前
agentscope提取msg+llama_index 查询
人工智能
小和尚同志3 小时前
什么?oh-my-opencode 太重了?那试试 oh-my-opencode-slim
人工智能·aigc
一路往蓝-Anbo3 小时前
第 9 章:Linux 设备树 (DTS) ——屏蔽与独占外设
linux·运维·服务器·人工智能·stm32·嵌入式硬件
飞哥数智坊3 小时前
把模型焊死在芯片上,就能跑出 17,000 tokens/秒?这是一条死路,还是一条新路?
人工智能
多恩Stone4 小时前
【3D-AICG 系列-11】Trellis 2 的 Shape VAE 训练流程梳理
人工智能·pytorch·算法·3d·aigc
tuotali20264 小时前
氢气压缩机技术规范亲测案例分享
人工智能·python