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

相关推荐
AIOps打工人12 分钟前
女朋友想要专属桌宠?我用 AI 提示词,1 小时把照片变成 57 帧动画精灵
人工智能
大囚长23 分钟前
自由能原理视域下的游戏成瘾机制——以“羊了个羊”为例
人工智能·游戏
水如烟43 分钟前
孤能子视角:三十六计之远交近攻——拓扑重构
人工智能
YOLO数据集集合1 小时前
番茄病害智能诊断系统:YOLO+DeepSeek农业AI落地实践
人工智能·yolo·目标检测·计算机视觉
QN1幻化引擎1 小时前
我写了一个「有八层意识结构」的 Python 认知引擎,它没有用任何 LLM
人工智能·算法·架构
anyup1 小时前
这一套绝了!AI 大模型写故事,星云 SDK 驱动 3D 数字人实时演绎
前端·人工智能·aigc
张彦峰ZYF1 小时前
大模型LLM ACA - ACP认证考试真题冲刺演练二答案参考
人工智能·lora·llm·embedding·rag·few-shot
SoaringPigeon1 小时前
NVIDIA博客-预训练去想象,微调去行动:世界-动作模型 (WAM) 的崛起
人工智能·深度学习·机器人·自动驾驶
甲维斯2 小时前
骚操作N:把GPT5.6接入Claude Code大发神威!
人工智能