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

相关推荐
pla8888888812 小时前
海光K100_AI单卡ComfyUI+MiniMax-H3环境调优实战:视频生成速度大幅提升
人工智能·音视频
CVHub12 小时前
智能标注工具 X-AnyLabeling v4 全面升级:迈向 AI 时代的数据生产基础设施
人工智能·算法·github
AI导出鸭12 小时前
怎么让腾讯元宝做表格?AI导出鸭苹果版通过解析引擎将元宝表格HTML转为结构化数据,一键导出标准Excel或Word
人工智能·chatgpt·html·excel·ai导出鸭
wujian831113 小时前
豆包导出word手机,就用AI导出鸭:一站式批量导出方案深度解析
人工智能·ai·chatgpt·智能手机·word·ai导出鸭
拿本唠嗑AI研究13 小时前
从电脑到手机:DeepSeek Harness Windows安装与手机互动教程(避坑完全版)
人工智能·windows·智能手机·deepseek·harness
ZJU_统一阿萨姆20 小时前
【算子开发】矩阵乘法(GEMM)入门与共享内存优化
人工智能·线性代数·矩阵·系统架构
AI码农小姐姐20 小时前
AI漫剧用什么软件制作?知漫剧对比即梦/豆包/可灵怎么选?
人工智能
YH552698421 小时前
GPT‑5.6 Sol 原本支持 1M 上下文,Codex 现已放开此前限制,如何看待这次调整?
java·jvm·人工智能·gpt·算法·chatgpt
ZYJCSZKJ21 小时前
AI数字人实时交互系统的工程架构与多方言适配实践
人工智能·架构·交互·ai数字人直播系统
2601_9659584621 小时前
口腔黏膜脱皮超2周未愈建议及时就医
人工智能·python