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

相关推荐
鲲穹AI种草11 分钟前
小红书 AI 创作工具怎么选?鲲穹 RedNote 功能实测与横向对比
大数据·人工智能
这张生成的图像能检测吗17 分钟前
(论文速读)LogSAD:无需训练的结构异常与逻辑异常统一检测
人工智能·机器学习·计算机视觉·大模型·多模态·异常检测
Ai-_Man36 分钟前
您您这可以把Grok的多个会话比如说。左侧的多个会话一次性导出吗?不是单条会话里面的多次会对话。AI导出鸭
javascript·人工智能·ai·小程序·电脑
Eric.461 小时前
Wan‑Animate+MiniMax-H3 本地部署 AI 漫剧流水线:8G 显存动作迁移与动态分镜工程实战
前端·人工智能·comfyui·ai漫剧
搭贝1 小时前
上厕所的时间搭好一套系统:AI自动生成实测
开发语言·人工智能·低代码·ai·php·搭贝
明月_清风1 小时前
SaaS 的三层挣钱逻辑,正在被 AI 从第一层击穿
人工智能·后端
福兮说1 小时前
秋招复习操作系统并发,我让 AI 生成了一张知识图谱,15 条关系里改了 5 条
人工智能·操作系统·知识图谱·秋招
老A的AI实验室2 小时前
Cyber Weekly #84
人工智能·ai·llm·agi·genai
lie..2 小时前
30天从零开始学AI应用开发(Day 18):文档切分与检索优化:RAG 效果好坏的分水岭
人工智能·ai·大模型
Είναι η κοπέλα2 小时前
显存计算与模型选择:你的显卡能跑多大的模型
人工智能·pytorch·python·开源·conda