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

相关推荐
王中阳Go2 分钟前
09 Go Eino AI应用开发实战 | Hertz Web 框架搭建
人工智能·后端·go
智驱力人工智能12 分钟前
从人海战术到智能巡逻 城市街道违规占道AI识别系统的实践与思考 占道经营检测系统价格 占道经营AI预警系统
人工智能·安全·yolo·目标检测·无人机·边缘计算
STLearner13 分钟前
VLDB 2025 | 时间序列(Time Series)论文总结(预测,异常检测,压缩,自动化等)
数据库·人工智能·深度学习·神经网络·机器学习·数据挖掘·时序数据库
GEO-optimize18 分钟前
2025年末GEO服务商推荐甄选:综合实力测评及优选指南
人工智能·搜索引擎·geo
Ven%24 分钟前
【AI大模型算法工程师面试题解析与技术思考】
人工智能·python·算法
我很哇塞耶28 分钟前
BOSS直聘3B超越Qwen3-32B,更多训练数据刷新小模型极限
人工智能·ai·大模型
趁你还年轻_28 分钟前
超越对话:AI 智能体如何自主完成复杂任务?
人工智能
禾高网络39 分钟前
互联网医院系统,互联网医院系统核心功能及技术
java·大数据·人工智能·小程序
AI营销实验室40 分钟前
原圈科技AI CRM系统:数据闭环与可视化革新的行业突破
大数据·人工智能
AndrewHZ42 分钟前
【复杂网络分析】什么是图神经网络?
人工智能·深度学习·神经网络·算法·图神经网络·复杂网络