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

相关推荐
数据与人工智能律师几秒前
数字资产革命中的信任之锚:RWA法律架构的隐形密码
大数据·网络·人工智能·云计算·区块链
CHANG_THE_WORLD12 分钟前
封装一个png的编码解码操作
图像处理·人工智能·计算机视觉
赛丽曼26 分钟前
Assistant API的原理及应用
人工智能·chatgpt
Yo_Becky1 小时前
【PyTorch】PyTorch预训练模型缓存位置迁移,也可拓展应用于其他文件的迁移
人工智能·pytorch·经验分享·笔记·python·程序人生·其他
DeepSeek-大模型系统教程1 小时前
深入金融与多模态场景实战:金融文档分块技术与案例汇总
人工智能·ai·语言模型·程序员·大模型·大模型学习·大模型教程
xinxiangwangzhi_1 小时前
pytorch底层原理学习--PyTorch 架构梳理
人工智能·pytorch·架构
yzx9910131 小时前
关于网络协议
网络·人工智能·python·网络协议
AiTEN_Robot1 小时前
AGV 无人叉车关键技术问题解析:精准定位算法 / 安全避障逻辑 / 系统对接协议全方案
人工智能·机器人·自动化·制造
云天徽上1 小时前
【PaddleOCR】OCR常见关键信息抽取数据集,包含FUNSD、XFUND、WildReceipt等整理,持续更新中......
人工智能·计算机视觉·信息可视化·paddlepaddle·paddleocr·文本识别
zskj_zhyl1 小时前
智绅科技:以科技为翼,构建养老安全守护网
人工智能·科技·安全