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

相关推荐
13线4 分钟前
为什么要合并飞书和豆包
人工智能·飞书·火山引擎·豆包
wangqiaowq7 分钟前
AI智能体学习
人工智能
科创致远10 分钟前
科创致远 eSOP 电子作业指导书系统落地应用指南
大数据·人工智能·汽车·制造·精益工程
宅小年11 分钟前
让 AI 用上你的资料,RAG 是怎么做到的?
人工智能
宅小年14 分钟前
为什么你的 AI 越聊越“笨”,还越来越慢?
人工智能
宅小年16 分钟前
AI 技能地图
人工智能
宅小年27 分钟前
DeepSeek Harness 的插件,到底该怎么用?
人工智能
浔溺30 分钟前
al+大数据每日学习笔记39
pytorch
仙魁XAN34 分钟前
【WorkBuddy · 三件套:技能/专家/技能】第 17 章 · 配置 MCP 服务器
人工智能·workbuddy·workbuddy三件套·mcp 配置与使用
无忧.芙桃37 分钟前
AI 生产力工具实践(四):豆包如何成为日常学习与写作助手
人工智能