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

相关推荐
oak隔壁找我29 分钟前
一款运行在 DSHDeepSeek Harness(DSH)中的 MySQL 客户端插件
人工智能
oak隔壁找我1 小时前
一款运行在 DSH(DeepSeek Harness)中的 Redis 管理插件
人工智能
孟健1 小时前
一个人做的免费AI工具,月访问千万的选品与技术拆解
人工智能·aigc
乌鲁木齐德润恒圣信息科技有限公司3 小时前
GEO生成式引擎优化:技术原理与43引擎自动测试系统
大数据·前端·人工智能
SelectDB4 小时前
SelectDB 和阿里云 SelectDB 有什么区别?公司关系、产品版本与部署方式详解
人工智能
SelectDB4 小时前
什么是 AI Agent 数据库?SelectDB 如何构建 Agent Native 实时分析基座
人工智能
严肃小白兔4 小时前
第 14/24 期:正则化方法及其对神经网络的影响
人工智能·神经网络·机器学习
江润舟5 小时前
万物|炼器:从零手搓工业级旋转目标检测网络·卷7 —— OBB检测头与DFL解码(三)
人工智能·python·深度学习
峯5805 小时前
清华博士90分钟AI与神经网络-内容梳理
人工智能
AI创界者5 小时前
Python 进阶实战(五十六):KV Cache 驱逐闭环反馈与 Prefill / Decode 分离协同调度
人工智能·python·aigc