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

相关推荐
CITY_OF_MO_GY2 分钟前
Spark-TTS:基于大模型的文本语音合成工具
人工智能·深度学习·语音识别
阿丢是丢心心8 分钟前
【从0到1搞懂大模型】神经网络的实现:数据策略、模型调优与评估体系(3)
人工智能·深度学习·神经网络
新智元8 分钟前
10²⁶参数,AGI 还需 70 年!清华人大预测届时 GPU 总价达 4000 万倍苹果市值
人工智能·openai
何大春16 分钟前
【对话推荐系统综述】Broadening the View: Demonstration-augmented Prompt Learning for CR
论文阅读·人工智能·深度学习·语言模型·prompt·论文笔记
WenGyyyL17 分钟前
使用OpenCV和MediaPipe库——增强现实特效(在手腕添加虚拟手表)
人工智能·opencv·计算机视觉·ar·cv·mediapipe
东临碣石8219 分钟前
【英伟达AI论文】多模态大型语言模型的高效长视频理解
人工智能·语言模型·自然语言处理
我去热饭20 分钟前
【完整记录】基于腾讯云HAI+DeepSeek快速开发法律咨询(小律师)辅助平台过程
人工智能
CoovallyAIHub22 分钟前
一码难求的Manus,又对计算机视觉产生冲击?复刻开源版已在路上!
人工智能·深度学习·计算机视觉
是理不是里_23 分钟前
人工智能里的深度学习指的是什么?
人工智能·深度学习