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

相关推荐
嵌入式-老费34 分钟前
自己动手写深度学习框架(感知机)
人工智能·深度学习
化作星辰1 小时前
使用 PyTorch来构建线性回归的实现
人工智能·pytorch·深度学习
mm-q29152227291 小时前
【天野学院5期】 第5期易语言半内存辅助培训班,主讲游戏——手游:仙剑奇侠传4,端游:神魔大陆2
人工智能·算法·游戏
谢景行^顾1 小时前
深度学习-损失函数
人工智能·深度学习
xier_ran1 小时前
关键词解释: LoRA(Low-Rank Adaptation)详解
人工智能
黄焖鸡能干四碗1 小时前
信息安全管理制度(Word)
大数据·数据库·人工智能·智慧城市·规格说明书
paopao_wu1 小时前
DeepSeek-OCR实战(01):基础运行环境搭建-Ubuntu
linux·人工智能·ubuntu·ai·ocr
Altair澳汰尔1 小时前
新闻速递丨Altair RapidMiner 数据分析和 AI 平台助力企业加速智能升级:扩展智能体 AI 及分析生态系统
人工智能·ai·数据分析·仿真·cae·rapidminer·数据自动化
oil欧哟1 小时前
GitHub星标3万,OpenAI 官方支持——深度解读 AI Agent 连接协议的行业标准 MCP
人工智能·github
极客BIM工作室1 小时前
单层前馈神经网络的万能逼近定理
人工智能·深度学习·神经网络