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

相关推荐
哈哈,柳暗花明18 分钟前
人工智能专业术语详解(L)
人工智能·专业术语
莱歌数字25 分钟前
散热测试使用恒温热源和功率热源的应用场景分析
人工智能·科技·制造·散热·液冷散热
码农小白AI27 分钟前
AI报告审核通审Agent版搭载IACheck:锅炉压力容器电梯起重设备安装监检核查
人工智能
手写码匠28 分钟前
手写 GraphRAG:从零实现图增强检索增强生成系统
人工智能·深度学习·算法·aigc
沪漂阿龙28 分钟前
Chat Model:LangChain 如何统一调用不同大模型?
人工智能·langchain
庄周迷蝴蝶29 分钟前
Vision Banana
人工智能·计算机视觉
装不满的克莱因瓶33 分钟前
【自动驾驶领域】学习 Cityscapes 数据集——城市街景语义理解的标准基准
人工智能·pytorch·python·深度学习·学习·机器学习·自动驾驶
刚木34 分钟前
用 Agnes AI 免费模型增强 Claude Code:从零上手指南
人工智能
阿部多瑞 ABU40 分钟前
铁三角:泛二次元奶头乐经济的结构分析及其人口后果
大数据·人工智能
FL162386312943 分钟前
户外垃圾类型检测数据集VOC+YOLO格式4278张10类别
人工智能·yolo·机器学习