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

相关推荐
guoketg1 分钟前
BERT的技术细节和面试问题汇总
人工智能·深度学习·bert
永远在Debug的小殿下1 分钟前
SLAM开发环境(虚拟机的安装)
人工智能
MF_AI7 分钟前
大型烟雾火灾检测识别数据集:25w+图像,2类,yolo标注
图像处理·人工智能·深度学习·yolo·计算机视觉
百家方案17 分钟前
航空港应急安全科教园区 — 应急安全产业园建设项目投标技术方案
人工智能·智慧园区
奔跑草-18 分钟前
【AI日报】每日AI最新消息2026-01-06
人工智能·github
雨大王51220 分钟前
工业AI大模型优化汽车生产排产:技术原理与实践案例
人工智能·汽车
byzh_rc29 分钟前
[机器学习-从入门到入土] 拓展-最小二乘法
人工智能·机器学习·最小二乘法
阿里巴啦31 分钟前
React+go实现AI 图像生成落地实践:文生图、图生图的工程项目
人工智能·react.js·ai作画·七牛云·ai生图·ai图生图
Codebee33 分钟前
AI 时代的人机协同:在智慧与执行之间寻找平衡
人工智能
love530love38 分钟前
EPGF 新手教程 12在 PyCharm(中文版 GUI)中创建 Poetry 项目环境,并把 Poetry 做成“项目自包含”(工具本地化为必做环节)
开发语言·ide·人工智能·windows·python·pycharm·epgf