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

相关推荐
AKAMAI1 小时前
Akamai Cloud客户案例 | Avesha 在 Akamai 云上扩展 Kubernetes 解决方案
人工智能·云计算
wasp5202 小时前
AgentScope Java 核心架构深度解析
java·开发语言·人工智能·架构·agentscope
智算菩萨2 小时前
高效多模态大语言模型:从统一框架到训练与推理效率的系统化理论梳理
大数据·人工智能·多模态
free-elcmacom2 小时前
深度学习<4>高效模型架构与优化器的“效率革命”
人工智能·python·深度学习·机器学习·架构
liliangcsdn2 小时前
python模拟beam search优化LLM输出过程
人工智能·python
算法与编程之美2 小时前
深度学习任务中的多层卷积与全连接输出方法
人工智能·深度学习
Deepoch2 小时前
具身智能产业新范式:Deepoc开发板如何破解机器人智能化升级难题
人工智能·科技·机器人·开发板·具身模型·deepoc
浪子不回头4152 小时前
SGLang学习笔记
人工智能·笔记·学习
飞哥数智坊3 小时前
TRAE 国内版 SOLO 全放开
人工智能·ai编程·trae
落叶,听雪4 小时前
AI建站推荐
大数据·人工智能·python