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

相关推荐
徐健峰9 小时前
GPT-image-2 热门玩法实战(三):AI 手办/Action Figure — 把自己变成盒装玩具
人工智能
扑兔AI9 小时前
B2B销售线索挖掘效率提升的技术实践:基于工商公开数据的客源筛选与竞品分析架构
大数据·人工智能·架构
亚鲁鲁9 小时前
00-目录
人工智能
罗西的思考9 小时前
【GUI-Agent】阿里通义MAI-UI 代码阅读(2)--- 实现
人工智能·算法·机器学习
墨染天姬10 小时前
【AI】cursor提示词小技巧
前端·数据库·人工智能
Raink老师10 小时前
【AI面试临阵磨枪-40】文本切块(Chunking)策略:固定长度、语义切块、递归切块、重叠设计
人工智能·ai 面试
techdashen10 小时前
p99 延迟从 9.5 毫秒降到 18 微秒:Cloudflare 机器学习基础设施重构全记录
人工智能·机器学习·重构
AI服务老曹10 小时前
架构师视角:如何构建支持GB28181/RTSP的异构AI视频平台?从Docker部署到源码交付的深度实践
人工智能·docker·音视频
IT_陈寒10 小时前
Vite热更新失效?你可能漏了这个小细节
前端·人工智能·后端
机器之心11 小时前
今天起,Claude正式接入Office全家桶,跨应用还能共享记忆
人工智能·openai