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

相关推荐
严肃小白兔1 小时前
第 17/24 期:卷积神经网络的数学原理与计算过程
人工智能·神经网络·cnn
LabVIEW开发1 小时前
Sherborne 倾角仪 + LabVIEW 倾角怎么测?
人工智能·labview
stormzhangV1 小时前
一文说清楚 Neon 到底有多强?
人工智能·google·aigc
孟健2 小时前
拆解降噪站 Noise Reducer:定价页这样设计,月访问 49 万
人工智能·aigc
英辰朗迪AI获客2 小时前
英辰朗迪AI获客每日AI精选(2026.11.21)
人工智能
RSABLOCKCHAIN2 小时前
【AI应用】Agent三种实现范式的对比
人工智能
八月瓜科技2 小时前
专精特新企业如何搭建专利资产中台?三步落地法
大数据·数据库·人工智能·科技·深度学习·机器学习·oracle
码农飞哥4 小时前
WorkBuddy 六步,把每天点的几十下做成扩展
人工智能·效率·ai工具·workbuddy
aimartec4 小时前
跨城定制软件与APP开发技术选型与源码验收规范(中小企业实用版)
人工智能