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

相关推荐
Coder_Boy_几秒前
基于SpringAI的智能OPS平台开发前置技能FAISS
人工智能·springboot·faiss
浪子sunny1 分钟前
实时行情Tick数据-金融高频数据解决方案
人工智能
DatGuy6 分钟前
Week 31: 深度学习补遗:Mamba
人工智能·深度学习
点云SLAM8 分钟前
Manipulate 英文单词学习
人工智能·英文单词学习·雅思备考·manipulate·操作 / 操控·操纵 / 控制·处理 / 调整
艺术是真的秃头9 分钟前
Trae:当编程从“编写”转向“对话”与“委派”
人工智能·python·ai·aigc
石像鬼₧魂石11 分钟前
SQLMap 注入完整操作流程(新手通关手册)
linux·人工智能·windows·学习
week_泽13 分钟前
OpenCV图像拼接原理与实践笔记
人工智能·笔记·opencv
万俟淋曦16 分钟前
【论文速递】2025年第43周(Oct-19-25)(Robotics/Embodied AI/LLM)
人工智能·深度学习·机器学习·机器人·论文·具身智能·robotic
其美杰布-富贵-李20 分钟前
迁移学习与对抗迁移学习
人工智能·机器学习·迁移学习
许泽宇的技术分享25 分钟前
2025年度技术之旅:在AI浪潮下的个人突破、持续创作与平衡之道
大数据·人工智能