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

相关推荐
用户2991422196803 分钟前
GitHub 热榜项目:日榜(2026-11-07)
人工智能·开源·github
tellmewhoisi16 分钟前
深度学习0:数学知识:正态分布3(积分,指数函数积分公式)
人工智能·深度学习
无忧_wy23 分钟前
Claude-Mem 记忆增强系统新手部署与调用指南
人工智能
用户29914221968024 分钟前
AI 日报(2026年11月7日)
人工智能
code2cat42 分钟前
【随笔】KV缓存卸载:逐层预取能藏住多少传输时间
人工智能·缓存·大语言模型
tellmewhoisi1 小时前
深度学习0:数学知识:正态分布1
人工智能·深度学习·算法
ting94520001 小时前
Databox MCP Connectors 技术深度解析:协议架构、集成原理与企业落地实践
人工智能·架构
码农飞哥2 小时前
WorkBuddy 两个月没动,今晚测试红了 2 条
人工智能·效率·ai工具·workbuddy
硅谷秋水2 小时前
面向科学的科学AI智能体:任务、架构、评估与挑战综述
人工智能·科技·语言模型
叼烟扛炮2 小时前
[LangChain] 学习笔记(04):手搓 ChatGPT 打字机——流式输出(SSE)与模型初始化选型
人工智能·笔记·chatgpt·langchain