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

相关推荐
大数据在线21 小时前
中国空间智能,梦想照进现实
人工智能·世界模型·空间智能·李飞飞·飞渡科技
星爷AG I21 小时前
16-4 类比(AGI基础理论)
人工智能·agi
l1t21 小时前
DeepSeek辅助测试不同文件格式的读写性能和大小
数据库·人工智能·python
QQsuccess21 小时前
人工智能(AI)全体系学习——系列二
人工智能·学习·机器学习
码农小白AI21 小时前
AI报告审核赋能质量安全:IACheck助力机体结构检测报告精准合规
大数据·人工智能
沉睡的无敌雄狮21 小时前
跨平台AI运营繁琐?矩阵跃动小陌GEO多端同步技术,解放技术运营双手
大数据·人工智能
2501_9431240521 小时前
7×24小时无人值守:矩阵跃动龙虾机器人+GEO,AI流量闭环效率实测报告
人工智能·矩阵·机器人
renhongxia121 小时前
唤醒过去:利用记忆模拟机器人流动唤醒效应
人工智能·微服务·架构·机器人·知识图谱
Elastic 中国社区官方博客21 小时前
Serverless 中用于负载均衡的 Elasticsearch 副本
大数据·运维·人工智能·elasticsearch·搜索引擎·云原生·serverless
ryrhhhh21 小时前
从0到1搭建AI流量闭环:矩阵跃动龙虾机器人+GEO的技术选型与实操指南
人工智能·矩阵·机器人