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

相关推荐
程序员大狐狸几秒前
Ultra X7 358H 笔记本电脑,本地部署当红炸子鸡 Qwen3.8-27B-Q4,实测生成速度 5.6 tok/s
人工智能
styshoo1 分钟前
NVIDIA Dynamo Snapshot 介绍
人工智能
用户8314550980311 分钟前
如一 Agent 架构解读(一):数字分身的执行模型——Run、Turn 与四个时间尺度
人工智能
Kyops1 分钟前
GPT-6 把地图和交互组件带进 ChatGPT,答案终于可以直接操作了
人工智能
布吉岛的石头5 分钟前
Java 程序员第 49 阶段11:Java 接 BERT:用 ONNX Runtime 做句向量推理
java·人工智能·python·深度学习·bert·transformer
OCR_133716212756 分钟前
智能交通底层逻辑:国内车牌分类规范与AI识别适配技术解析
大数据·人工智能·分类
Vincy1237 分钟前
腾讯开源 WeKnora 深度评估:3 万 Star 的 RAG 框架,你的 Milvus 能直接用吗?
人工智能
染指111012 分钟前
140.Agent-多Agent框架-Agent执行Skills流程
数据库·人工智能·设计模式·langchain·agents
小慧姐姐呀20 分钟前
把大模型塞进桌面端:一次本地离线 AI 推理的选型与工程实践
人工智能
Chengyunlai22 分钟前
告诉模型输出 JSON,就一定能得到正确的 JSON 吗?
人工智能