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

相关推荐
未来智慧谷32 分钟前
华为发布AI推理新技术,降低对HBM内存依赖
人工智能·华为
AKAMAI34 分钟前
通过Akamai分布式计算区域实现直播传输
人工智能·分布式·云计算
坐在地上想成仙40 分钟前
计算机视觉(4)-相机基础知识恶补
人工智能·数码相机·计算机视觉
sssammmm1 小时前
AI入门学习--如何写好prompt?
人工智能·学习·prompt
阿群今天学习了吗2 小时前
“鱼书”深度学习进阶笔记(3)第四章
人工智能·笔记·python·深度学习·算法
神齐的小马2 小时前
机器学习 [白板推导](十)[马尔可夫链蒙特卡洛法]
人工智能·机器学习·概率论
白-胖-子2 小时前
深度剖析主流AI大模型的编程语言与架构选择:行业实践与技术细节解读
人工智能·架构
AI模块工坊2 小时前
IEEE 2025 | 重磅开源!SLAM框架用“法向量+LRU缓存”,将三维重建效率飙升72%!
人工智能·深度学习·神经网络·机器学习·计算机视觉
nbsaas-boot2 小时前
用生成器守住架构,用 AI 放大效率:一套可落地的 AI 编程方法论
人工智能·架构
文弱_书生3 小时前
为什么神经网络的权重矩阵具有低秩特性?如何理解和解释?
人工智能·神经网络·矩阵