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

相关推荐
人工智能AI技术7 小时前
10亿美元合作启发:AIGC正版IP应用开发,迪士尼+OpenAI技术拆解
人工智能
光羽隹衡7 小时前
深度学习——卷积神经网络实现手写数字识别
人工智能·深度学习·cnn
莫非王土也非王臣7 小时前
深度学习之对比学习
人工智能·深度学习·学习
AI_56787 小时前
Selenium+Python可通过 元素定位→操作模拟→断言验证 三步实现Web自动化测试
服务器·人工智能·python
冰西瓜6007 小时前
国科大高级人工智能期末复习(四)联结主义(下)——深度学习
人工智能·深度学习
檐下翻书1738 小时前
世界模型:AI理解物理空间的关键一步
人工智能
2013092416278 小时前
1968年 Hart, Nilsson, Raphael 《最小成本路径启发式确定的形式基础》A* 算法深度研究报告
人工智能·算法
InterestOriented8 小时前
破解银发学习痛点 兴趣岛 “普惠 + 品质” 模式打造积极老龄化范本
大数据·人工智能·学习
Mark_Aussie8 小时前
ADALog 日志异常检测
人工智能
Jouham8 小时前
教培获客破局:AI智能体如何重塑需求捕捉与转化新范式
人工智能