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

相关推荐
tanglinS几秒前
工业陶瓷榜单:国内精密工业陶瓷零部件供应商综合选型参考
大数据·运维·人工智能·gpt·材质
杜子不疼.9 分钟前
从“自然语言问数“到“AI 自动调优“:我给金仓写了一个 MCP Server
人工智能
Lee_jerome9 分钟前
python神经网络编程入门(二十七)——RNN IMBD搭建情感分类器与基础训练
pytorch·rnn·深度学习·gru·词嵌入·情感分析·imdb
阳明山水10 分钟前
销量预测的“隐形杀手”:概念漂移与自适应学习实战
人工智能·深度学习·算法·机器学习·架构
YH552698412 分钟前
如何评价GPT-5.6Luna免费不限量,后续基础大模型都会走向免费吗?
人工智能·gpt·chatgpt
u01030552714 分钟前
TMD刀具配送安卓智能体方案
人工智能
枫叶丹416 分钟前
从聊天到委派:AI Agent 如何推进长期任务
人工智能·chatgpt·agent·codex
专业工业模块电源工程师22 分钟前
怎么选好 DC-DC 模块电源功率?厂家推荐还是自主选型
人工智能·电源模块·工业电源
数翊科技23 分钟前
数翊科技出席TDBC2026:基于dataeasy构建Agent基础设施,企业AI可信落地迎来新范式
人工智能·科技
liulilittle26 分钟前
反量化:反量化Q8零(q8_0)
人工智能·算法·机器学习·ai·llm