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

相关推荐
淡漠的蓝精灵3 分钟前
mem0aimem0,给AI加记忆层的开源方案
人工智能·其他
精益数智小屋7 分钟前
什么是进销存库存表?进销存库存表包含哪些内容?
大数据·运维·数据库·人工智能·安全
新加坡内哥谈技术10 分钟前
推理架构的转变
人工智能
零梦ing11 分钟前
解决claude下载问题
人工智能
2601_9577808411 分钟前
Agent记忆系统架构设计与工程实践:从短期暂存到长期持久化
大数据·网络·人工智能·架构·agent
weixin_66812 分钟前
Windows 上通过 WSL2 部署 Qwen3-ASR-0.6B - AI
人工智能·windows
聚铭网络13 分钟前
聚铭网络入选数说安全《AI重塑网络安全:网络安全智能化产品与市场报告》
网络·人工智能·安全
小小测试开发14 分钟前
OpenAI Daybreak 深度解析:AI 驱动的漏洞检测与修复实战指南
人工智能
_Evan_Yao16 分钟前
责任链模式在Agent编排中的应用:让AI Agent学会“踢皮球”
java·人工智能·后端·责任链模式