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

相关推荐
腾讯安全应急响应中心3 分钟前
AI安全,由攻入防|腾讯广告AI专项众测正式启动
人工智能·安全
mys55189 分钟前
杨建允:AI搜索时代文旅品牌的GEO优化营销策略
人工智能·ai搜索优化·geo优化·文旅geo·文旅品牌geo
hsg7713 分钟前
简述:torchgeo
人工智能·深度学习
ForDreamMusk16 分钟前
PyTorch编程基础
人工智能·pytorch
喵喵侠w17 分钟前
Rokid 智能眼镜开发实战:制作一款花草树木识别智能体
人工智能
小程故事多_8017 分钟前
从推理到智能体,大模型强化学习中信用分配机制的演进与突破
人工智能·prompt·aigc·ai编程
人工智能研究所21 分钟前
Claude + HyperFrames:用 HTML 方式制作视频,AI 时代一切皆可 HTML?
人工智能·html·音视频·ai 视频·hyperframes·claude-
是娇娇公主~29 分钟前
AI Agent详解
人工智能·ai agent
CV-杨帆31 分钟前
RAG 与记忆机制本质辨析及研究路径评估
人工智能
AI医影跨模态组学38 分钟前
如何通过MRI识别的系膜筋膜侵犯类型关联局部晚期直肠癌的免疫微环境及肿瘤增殖活性,并进一步解释其与预后不良的机制联系
人工智能·论文·医学·医学影像