为什么我的预训练的resnet-152模型的准确性这么低?

时间:2020-05-17 21:02:20

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

我对深度学习和神经网络还很陌生。我最近使用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)

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

1 个答案:

答案 0 :(得分:0)

  1. 预处理数据集。
  2. 获取更多数据集,因为准确性较低可能是因为数据集较小。
  3. 如果数据量较少,请尝试数据增强。