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

相关推荐
小道士写程序2 分钟前
自然语言处理NLP - 第3章 从字符到Token:机器的输入长什么样
人工智能·机器学习
Mr数据杨2 分钟前
钻石价格预测实战 从 Kaggle 回归赛题看结构化数据建模落地
人工智能·数据分析·kaggle竞赛
Mr数据杨5 分钟前
从 Kaggle 到实战的多标签文本分类案例拆解
人工智能·数据分析·kaggle竞赛
福昕办公8 分钟前
智能体手机落地,Agent 正在从“云端调用“走向“长进软件本体“
人工智能·智能手机
DolphinScheduler社区13 分钟前
把 Apache DolphinScheduler 变成 Agent 的“手和脚”:从调度平台到自然语言数据入口
人工智能·开源·apache·agent·技术分享·海豚调度·大数据工作流调度
weixin_4462608528 分钟前
基于熵的选择性智能体引导:从非完美视觉语言模型教师学习自主策略
人工智能·学习·语言模型
夜雪一千33 分钟前
如何写一个数据分析 Skill
人工智能·数据挖掘·数据分析
桃西西呀33 分钟前
RAG 接个向量库就完事?从切块到重排的 7 步流水线,我替你踩了 8 个深坑
人工智能·llm·ai编程
YOLO数据集集合38 分钟前
无人机屋顶与地物分割数据集 | 屋顶分割 航拍识别 城市规划 材质分类 实例分割9036期
人工智能·分类·无人机·材质·中国城市建筑·建筑识别
zhangfeng113344 分钟前
CodeBuddy‑CLI 默认授权(权限模式)命令行参数
人工智能·ai编程