使用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 小时前
主流GEO优化系统技术对比评测
人工智能·信息可视化
儒雅芝士1 小时前
RethinkFun深度学习笔记
人工智能·笔记·深度学习
xiaoginshuo1 小时前
流程自动化从传统RPA升级到AI Agent,如何匹配合适的自动化方案
人工智能·自动化·rpa
这张生成的图像能检测吗1 小时前
(论文速读)XLNet:语言理解的广义自回归预训练
人工智能·计算机视觉·nlp·注意力机制
新缸中之脑1 小时前
Ollama视觉模型实测
人工智能
悠闲蜗牛�1 小时前
边缘AI推理实战:从服务器到嵌入式设备的模型部署与优化
运维·服务器·人工智能
小鸡吃米…1 小时前
TensorFlow - 构建计算图
人工智能·python·tensorflow
shuidaoyuxing1 小时前
机器人具身智能概念
人工智能·机器人
土拨鼠烧电路1 小时前
笔记12:AI在快消:超越概念的四大落地场景
人工智能·笔记
xiaozhazha_1 小时前
技术选型深度解析:企业级AI智能办公系统架构设计与“人机协同”实践——以快鹭为例
人工智能·系统架构