使用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 小时前
基于「YOLO目标检测 + 多模态AI分析」的人员摔倒智能检测分析预警系统
人工智能·yolo·目标检测·计算机视觉·毕业设计·大作业
JackieZhengChina13 小时前
阿里开源项目Pixelle-Video 详解:开源AI全自动短视频引擎,零门槛一键生成成片
人工智能·开源项目·视频制作
aLTttY13 小时前
Spring Boot 3.x 集成 AI 大模型实战指南
人工智能·spring boot·后端
@insist12313 小时前
信息安全工程师-密码学专题(中):对称加密、RSA 与哈希算法
人工智能·密码学·哈希算法·软考·信息安全工程师·软件水平考试
新知图书13 小时前
基于ReAct模式的智能体系统示例
人工智能·agent·智能体
guslegend13 小时前
第16节:如何科学调节切片长度与滑动窗口,结合倒排索引与向量锁引对比优化
人工智能·大模型·rag
深度学习lover13 小时前
<数据集>yolo 家庭垃圾识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·家庭垃圾识别
无心水13 小时前
【Hermes:安装部署】11、Docker 部署 Hermes:干净隔离、数据持久化完整教程 —— 从零搭建你的专属智能体服务
人工智能·openclaw·养龙虾·hermes agent·hermes·本地智能体·养马
j_xxx404_14 小时前
我用 Codex 做了一个智能围棋机器人系统:从 AI 引擎接入到前后端联调的完整实战
c++·人工智能·python·机器人·软件工程·团队开发·react
好运的阿财14 小时前
OpenClaw工具拆解之 image+pdf
人工智能·python·程序人生·pdf·ai编程·openclaw·openclaw工具