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

相关推荐
Eric.46几秒前
AI 漫剧量产实战:StableDiffusion 帧稳画算法 + ComfyUI 自动质检流水线搭建(附完整代码 + 配置)
人工智能·深度学习·stable diffusion·comfyui·ai漫剧
代码方舟1 分钟前
企业级对公银行 KYC 架构:基于天远人脸身份证比对A构建自动化客户尽调网关
运维·人工智能·架构·自动化
xingyun863 分钟前
DeepSeek涨价,你换了吗?——AI大模型服务成本与替代方案深度分析
人工智能
程序员cxuan4 分钟前
DeepSeek Harness 必装的插件公布了!
人工智能·后端·程序员
Claire_888 分钟前
自然语言处理在法律文书生成中的应用:以离婚协议为例的多工具功能对比
人工智能·自然语言处理
樊小肆12 分钟前
DeepSeeker-Code源码导读05-计划模式与子Agent
人工智能·agent
办公室马主任12 分钟前
乐图数字化打通哪几个环节?从车间数据到经营决策的数据链路设计
大数据·人工智能·系统架构·制造
delishcomcn14 分钟前
数字孪生+AI预测:热烫膜分切机迈向“黑灯工厂”的关键路径
大数据·人工智能
ITresearchGuest15 分钟前
高级前端开发职业规划(2026—2035)
人工智能
皮皮虾❀16 分钟前
典铭云赛(FDE服务模式)开通钉钉AI功能全攻略:版本选择与配置指南
人工智能·钉钉