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

相关推荐
Omics Pro2 分钟前
华大等NC|微生物多样性与抗菌物质发现
大数据·人工智能·深度学习·语言模型·excel
传说故事3 分钟前
【论文阅读】Being-H0.7: A Latent World-Action Model from Egocentric Videos
论文阅读·人工智能·具身智能
葡萄城技术团队5 分钟前
智慧表格(SpreadJS + AI):拥抱 Web 端对话式办公新时代
前端·人工智能
Jagger_7 分钟前
Claudian 插件安装与 Claude Code 配置指南
人工智能
gsls2008088 分钟前
常见AI编码助手
人工智能·ai编码
词元Max13 分钟前
2.8 pydantic 数据校验:AI 开发的隐形利器
开发语言·人工智能·python
数据存储张15 分钟前
人工智能绘画的进化:大模型图像生成能力横评
人工智能
福客AI智能客服24 分钟前
AI智能客服系统正在改变“服务是如何被完成的”
人工智能
数智工坊27 分钟前
R-CNN目标检测算法精读全解
网络·人工智能·深度学习·算法·目标检测·r语言·cnn
俊哥V33 分钟前
每日 AI 研究简报 · 2026-04-22
人工智能·ai