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

相关推荐
小程故事多_80几秒前
从14.3%到94.3%,破解Agent联网搜索“噪声致幻”的核心密码
人工智能·aigc
BFT白芙堂5 分钟前
游戏化机器人数据采集:以Franka Research 3为核心的RoboCade创新实践
人工智能·深度学习·机器学习·机器人·具身智能·franka
草莓熊Lotso6 分钟前
Qt 控件美化与交互进阶:透明度、光标、字体与 QSS 实战
android·java·开发语言·c++·人工智能·git·qt
待磨的钝刨6 分钟前
目标检测三大技术路线综述:Proposal-based, Anchor-based, Anchor-free
人工智能·目标检测·计算机视觉
wukangjupingbb7 分钟前
在AI时代,个人和企业应从“追求效率”转向“追求独特性”
人工智能
煤炭里de黑猫11 分钟前
使用 PyTorch 实现 MNIST 手写数字分类的 CNN 模型
pytorch·分类·cnn
老蒋每日coding14 分钟前
AI Agent 设计模式系列(十八)—— 安全模式
人工智能·安全·设计模式
StarChainTech15 分钟前
一站式租车平台革新:从信用免押到全流程可视化管理的技术实践
大数据·人工智能·微信小程序·小程序·软件需求
DN202015 分钟前
性价比高的AI销售机器人源头厂家
人工智能·机器人
余俊晖23 分钟前
强化学习GRPO(格式奖励)在多模态文档解析中的运用方法
人工智能·自然语言处理·多模态