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

相关推荐
To_OC4 小时前
别死磕 Prompt 了!我用 Harness 流水线,让大模型自动产出高质量代码
人工智能·llm·agent
恒拓高科WorkPlus4 小时前
企业级即时通讯与数字协同平台:BeeWorks如何连接沟通、文档、业务与AI
人工智能
XGeFei4 小时前
【AI应用/Agent智能体搭建平台2】Dify——自部署
人工智能·笔记·学习
hans汉斯5 小时前
《软件工程与应用》期刊推荐&10月版面征稿中
图像处理·人工智能·深度学习·算法·音视频·软件工程
前端开发江鸟5 小时前
为了实现“小说自由”,我搭了一套 AI 小说工作流,结果却不如回家种田
人工智能
叠层归一研究院5 小时前
AGI 系统(八):符号范畴嵌入函子 — SymCat ↪ C_M107 严格化
c语言·开发语言·人工智能·算法·transformer·agi
2601_964840275 小时前
4G云门禁普惠化技术实践:基于边缘计算破解成本、隐私、部署三大行业瓶颈
大数据·人工智能·边缘计算
明朝百晓生5 小时前
Deep RL learning[2026/8]
开发语言·javascript·人工智能
具身智能进化论5 小时前
国产协作机器人品牌如何选择,企业长期使用更看重什么
大数据·人工智能·机器人·工厂方法模式
AKAMAI5 小时前
2026年Gartner Peer Insights 边缘分布平台客户之声将Akamai评为客户之选
人工智能·云计算