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

相关推荐
子午2 分钟前
【鸟类识别系统】Python+TensorFlow+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
qq_160144873 分钟前
AI爱好者入门:2025年CAIE报考指南与学习路径解析
人工智能·学习
WebGIS开发5 分钟前
东北黑土地保护|智慧城市地图可视化智能监测、管理系统
人工智能·gis·智慧城市·gis开发·webgis·地理信息科学
某林2125 分钟前
在slam建图中为何坐标base_link,laser,imu_link是始终在一起的,但是odom 会与这位三个坐标在运行中产生偏差
人工智能·算法
Keep__Fighting7 分钟前
【机器学习:逻辑回归】
人工智能·python·算法·机器学习·逻辑回归·scikit-learn·matplotlib
23遇见8 分钟前
AI情绪识别技术:价值与局限并存的智能革新
人工智能
科技与数码9 分钟前
国产MATLAB替代软件的关键能力与生态发展现状
大数据·人工智能·matlab
数据的世界0112 分钟前
重构智慧书-第6条:在趋近圆满中践行成长
人工智能
阿杰学AI14 分钟前
AI核心知识29——大语言模型之Multimodality(简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·aigc·多模态·多模态大模型
极市平台17 分钟前
骁龙大赛技术分享第4期来了
人工智能·经验分享·笔记·后端·个人开发