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

相关推荐
小虎AI生活10 分钟前
朋友圈发了没人理?这个AI技能一句话甩你4个版本,做私域的都能用
人工智能·超级个体·青玥ai
龙腾AI白云17 分钟前
多源数据融合:把零散信号拼成一致图景
大数据·人工智能·python·机器学习·知识图谱
aimartec27 分钟前
不动老旧 ERP 核心总账,如何以高性价比方案落地外勤与车间移动协同?
人工智能
艾派森40 分钟前
深度学习实战-基于VGG19的健康和白化珊瑚图像分类识别模型
人工智能·python·深度学习·分类
Csvn1 小时前
让 AI 少返工:任务卡六要素与四维验收怎么写
人工智能
老纪的技术唠嗑局1 小时前
新增项目知识库,网关也能帮 Agent 找回记忆|PowerContext v1.6.0 发布
人工智能
用户842558130871 小时前
插值选错,峰值就没了
人工智能
小宋10211 小时前
embedding 返回全零向量,索引却照收不误:入库前的向量健康门禁、隔离重算与召回恢复判据
linux·服务器·网络·人工智能
京东云开发者1 小时前
京东11.11大促系列|“懂经营”的AI全托管,为什么能从京东长出来?
人工智能
Fruiticecake2 小时前
机器人如何听声辨位:从唤醒词(KWS)与 VAD 到 DOA 声源定位,以及 ALSA XRUN 避坑
人工智能