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

相关推荐
2501_9411474230 分钟前
人工智能赋能智慧城市互联网应用:智能交通、能源与公共管理优化实践探索》
人工智能
咚咚王者42 分钟前
人工智能之数据分析 numpy:第十五章 项目实践
人工智能·数据分析·numpy
水月wwww1 小时前
深度学习——神经网络
人工智能·深度学习·神经网络
司铭鸿2 小时前
祖先关系的数学重构:从家谱到算法的思维跃迁
开发语言·数据结构·人工智能·算法·重构·c#·哈希算法
机器之心2 小时前
从推荐算法优化到AI4S、Pico和大模型,杨震原长文揭秘字节跳动的技术探索
人工智能·openai
johnny2332 小时前
AI加持测试工具汇总:Strix、
人工智能·测试工具
机器之心2 小时前
哈工大深圳团队推出Uni-MoE-2.0-Omni:全模态理解、推理及生成新SOTA
人工智能·openai
w***Q3502 小时前
人工智能在智能家居中的控制
人工智能·智能家居
青瓷程序设计2 小时前
花朵识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
阿里云大数据AI技术2 小时前
PAI Physical AI Notebook详解4:基于仿真的GR00T-N1.5模型微调
人工智能