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

相关推荐
工业机器视觉设计和实现1 分钟前
微分方程的联想
人工智能·神经网络·感想
罗罗攀12 分钟前
PyTorch学习笔记|神经网络的损失函数
人工智能·pytorch·笔记·神经网络·学习
枫叶林FYL38 分钟前
第9章 因果推理与物理理解
人工智能·算法·机器学习
AIBox3651 小时前
openclaw api 配置排查与接入指南:网关启动、配置文件和模型接入全流程
javascript·人工智能·gpt
LoserChaser1 小时前
OpenClaw 指令大全:分类详解与使用指南
人工智能·ai·语言模型
TDengine (老段)1 小时前
TDengine IDMP 可视化 —— 面板
大数据·数据库·人工智能·物联网·ai·时序数据库·tdengine
大模型任我行1 小时前
英伟达:解耦训练与推演的服务架构
人工智能·语言模型·自然语言处理·论文笔记
newsxun1 小时前
中创汇联双城峰会圆满举办 多维赋能实体高质量发展
大数据·人工智能
人工智能AI技术1 小时前
Karpathy开源第二大脑方案,有望替代向量数据库,让AI永不失忆
人工智能
之歆1 小时前
打造你的 AI 浏览器助手:从零到一的完整实践
人工智能