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

相关推荐
CHEEVEN_QY16 分钟前
B2B制造企业,AI搜索时代的信息架构怎么做
人工智能·制造·geo·结构化数据·b2b制造
月光船幽幽3 小时前
检测用户意图复杂性的关键方法
人工智能·python
Henry-SAP4 小时前
AI突破重塑未来科技格局
人工智能·云原生·sap·erp
空堂与归5 小时前
图像识别总是准确率低?用卷积神经网络CNN实现高效分类
人工智能·深度学习·分类·cnn
老猿AI洞察6 小时前
阿里云启用巴西数据中心,AI服务出海落子南半球
人工智能·阿里云·云计算
大金SEO6 小时前
GEO启动的正确顺序:先做信源清理,再谈内容扩张
大数据·人工智能
Bruce_Liuxiaowei6 小时前
从基础理论开始学习人工智能(三):盲目搜索——从状态空间图到生成-测试范式
人工智能·学习
qyz_hr6 小时前
拥抱AI,红海云筑牢企业组织与人力数据底层能力
人工智能
有梦想的骇客6 小时前
PyTorch学习笔记
pytorch·笔记·学习