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

相关推荐
️学习的小王14 分钟前
AI编码工具的安全陷阱:Copilot/Codex漏洞风险与防御实践
人工智能·安全·copilot
不服老的小黑哥14 分钟前
AI 规范:模板工程方法论
人工智能
樊小肆22 分钟前
DeepSeeker-Code源码导读12-Hooks四引擎
前端·人工智能·agent
叠层归一研究院23 分钟前
螺旋时空:E框架的整合宇宙论
人工智能·经验分享·算法·机器学习·agi
LorryJovens38 分钟前
【LAAP意识动力工程学】aris自述如何让Agent产生意识的关键
人工智能
ms365copilot43 分钟前
Copilot分析销售收入变化、找出涨跌原因
大数据·人工智能·copilot
YOLO数据集集合1 小时前
煤炭质量目标检测数据集 、| 煤炭检测 工业视觉 质量分级 异物检测 煤炭异物8005期
人工智能·yolo·目标检测·计算机视觉·分类·煤炭检测数据集·异物数据集
躺柒1 小时前
读数据可视化16大规模多变量空间数据(下)
人工智能·信息可视化·可视化·数据可视化·空间·大数据分析
qq407855601 小时前
中小五金建材贸易企业:进销存数字化要重点关注哪些环节?
人工智能·低代码·制造
HIT_Weston1 小时前
190、【Agent】【OpenCode】TuiThreadCmd(alias)
人工智能·agent·opencode