【问题标题】:why is the accuracy of my pretrained resnet-152 model so low?为什么我预训练的 resnet-152 模型的准确率这么低?
【发布时间】:2020-09-03 14:42:05
【问题描述】:

我对深度学习和神经网络还很陌生。我最近使用 FER-2013 数据集构建了一个面部情绪识别分类器。我正在使用预训练的 resnet-152 模型进行分类,但我的模型的准确度非常低,无论是训练准确度还是验证准确度。我得到了大约 36% 的准确率,这并不好。我想使用迁移学习,准确率应该很高,为什么我得到这么低的准确率。我应该更改超参数吗?这是我的代码。

    model= models.resnet152(pretrained=True)

    for param in model.parameters():
      param.requires_grad= False

    print(model)
    from collections import OrderedDict

    classifier= nn.Sequential(OrderedDict([
                                           ('fc1',nn.Linear(2048, 512)),
                                           ('relu', nn.ReLU()),
                                           ('dropout1', nn. Dropout(p=0.5)),
                                           ('fc2', nn.Linear(512, 7)),
                                           ('output', nn.LogSoftmax(dim=1))
    ]))
    model.fc= classifier
    print(classifier)
    def train_model(model, criterion, optimizer, scheduler, num_epochs=10):
      since= time.time()

      best_model_wts= copy.deepcopy(model.state_dict())
      best_acc= 0.0

      for epoch in range(1, num_epochs + 1):
        print('Epoch {}/{}'.format(epoch, num_epochs))
        print('-' * 10)

        for phase in ['train', 'validation']:
          if phase == 'train':
            scheduler.step()
            model.train()
          else:
            model.eval()

          running_loss= 0.0
          running_corrects=0

          for inputs, labels in dataloaders[phase]:
            inputs, labels= inputs.to(device), labels.to(device)


            optimizer.zero_grad()

            with torch.set_grad_enabled(phase== 'train'):
              outputs= model(inputs)
              loss= criterion(outputs, labels)
              _, preds= torch.max(outputs, 1)

              if phase == 'train':
                loss.backward()
                optimizer.step()

            running_loss += loss.item() * inputs.size(0)
            running_corrects += torch.sum(preds== labels.data)

          epoch_loss= running_loss / dataset_sizes[phase]
          epoch_acc= running_corrects.double() / dataset_sizes[phase]

          print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))

          if phase == 'validation' and epoch_acc > best_acc:
            best_acc= epoch_acc
            best_model_wts= copy.deepcopy(model.state_dict())

      time_elapsed= time.time() - since
      print('Training complete in {:.0f}m {:.0f}s'.format(
          time_elapsed // 60, time_elapsed % 60))
      print('Best valid accuracy: {:4f}'.format(best_acc))

      model.load_state_dict(best_model_wts)
      return model

    use_gpu= torch.cuda.is_available()
    num_epochs= 10
    if use_gpu:
      print('Using GPU: '+ str(use_gpu))
      model= model.cuda()

    criterion= nn.NLLLoss()

    optimizer= optim.SGD(model.fc.parameters(), lr = .0006, momentum=0.9)
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1)

    model_ft = train_model(model, criterion, optimizer, exp_lr_scheduler, num_epochs=10)

有人可以指导我吗?我是它的初学者,我真的可以利用它的一些帮助。

【问题讨论】:

  • 如果您使用预训练模型,请确保以与用于训练模型的原始数据完全相同的方式预处理数据,例如对于 z 缩放,使用相同的均值和标准差来缩放图像。

标签: deep-learning neural-network pytorch conv-neural-network


【解决方案1】:
  1. 预处理数据集。
  2. 获取更多数据集,因为较小的数据集可能会导致准确性低。
  3. 如果数据较少,请尝试数据增强。

【讨论】:

    猜你喜欢
    • 2020-04-20
    • 2020-03-12
    • 1970-01-01
    • 2020-02-11
    • 2020-04-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多