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

相关推荐
汤姆宝库12 分钟前
补全PPO:价值函数、GAE与裁剪
人工智能
阿里云大数据AI技术2 小时前
从“重复造轮”到“自主交付”---万物云 DataWorks Data Agent 落地实践实录
人工智能·agent
掘金安东尼2 小时前
智能体时代的预填充,为什么适合用流水线并行(Inferact 全文转译)
人工智能·agent
阿enna2 小时前
检测指标入门:IoU、Precision、Recall与mAP
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·cnn
掘金安东尼2 小时前
Google Antigravity 和 Gemini Neon:哪些行为会封号,怎么降低风险(Flavio Copes 全文转译)
人工智能·google·gemini
kaixin_啊啊2 小时前
【零基础学AI】第 35 章——添加网页界面并部署
人工智能
程序员cxuan2 小时前
绝了,WorkBuddy 可以打通微信了
人工智能·后端·程序员
程序员cxuan3 小时前
再见了反重力,Google 又来搞事了
人工智能·后端·程序员
阿里云大数据AI技术3 小时前
阿里云灵骏真武超节点 Day0 适配星辰大模型 Xing4.1-Preview
人工智能
阿里云大数据AI技术3 小时前
EMR Serverless Ray 正式发布一站式具身智能数据处理平台
人工智能