使用pytorch搭建ResNet并基于迁移学习训练

 这里的迁移学习方法是载入预训练权重的方法

    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. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-29 08:22:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-29 08:22:03       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-29 08:22:03       82 阅读
  4. Python语言-面向对象

    2023-12-29 08:22:03       91 阅读

热门阅读

  1. 热迁移

    2023-12-29 08:22:03       55 阅读
  2. 开源大语言模型简记

    2023-12-29 08:22:03       52 阅读
  3. 简单工厂设计模式(计算器实例优化)

    2023-12-29 08:22:03       62 阅读
  4. 数据清洗与融合期末考试(常见理论题)

    2023-12-29 08:22:03       58 阅读
  5. 医疗EDI:GE healthcare EDI 需求分析

    2023-12-29 08:22:03       55 阅读
  6. Android集成OpenSSL实现加解密-JNI实现

    2023-12-29 08:22:03       58 阅读