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

相关推荐
砚边数影1 分钟前
AI开发依赖引入:DL4J / Java-ML 框架 Maven 坐标配置
java·数据库·人工智能·深度学习·机器学习·ai·maven
砚边数影3 分钟前
AI环境搭建(一):JDK17 + Maven 配置,Java开发环境标准化流程
数据库·人工智能·ai·ai编程
大模型最新论文速读4 分钟前
字节跳动 Seed: 用“分子结构”对思维建模
论文阅读·人工智能·深度学习·机器学习·自然语言处理
要加油哦~6 分钟前
AI | 论文 | Widget2Code: From Visual Widgets to UI Code via Multimodal LLMs
人工智能
向上的车轮7 分钟前
如何用AI重写已有的系统?
人工智能
2501_9421917712 分钟前
基于Mask-RCNN改进模型的肉类新鲜度自动检测与分类系统_1
人工智能·分类·数据挖掘
mumu-hn13 分钟前
浅说LLM-Agent(大模型-智能体)
人工智能·llm·agent
AI时代原住民15 分钟前
红旗法案:通过历史之镜分析AI时代的人机协同范式
人工智能
方见华Richard27 分钟前
自指宇宙学:存在如何通过自我描述而实在化V0.2
人工智能·交互·原型模式·空间计算
liliangcsdn38 分钟前
基于人类反馈的强化学习框架-RLHF&PPO
人工智能·机器学习