Pytorch evaluation阶段GPU内存持续上涨

这段代码展示了在使用DeBERTa模型进行评估时,由于predicts和labels列表在GPU上不断积累导致GPU内存增加,最终可能引发OOM错误。为了解决这个问题,只需将detach后的logits和label_ids转移到CPU上存储,如代码所示。修改后的代码能够有效防止GPU内存持续上涨。
摘要由CSDN通过智能技术生成

这是一段DeBERTa的代码,会在evaluation阶段造成GPU内存持续上涨,小内存的GPU很容易就OOM

    predicts=[]
    labels=[]
    for batch in tqdm(AsyncDataLoader(eval_dataloader), ncols=80, desc='Evaluating: {}'.format(prefix), disable=no_tqdm):
      batch = batch_to(batch, device)
      with torch.no_grad():
        output = model(**batch)
      logits = output['logits'].detach()
      tmp_eval_loss = output['loss'].detach()
      if 'labels' in output:
        label_ids = output['labels'].detach().to(device)
      else:
        label_ids = batch['labels'].to(device)
      predicts.append(logits)
      labels.append(label_ids)
      eval_loss += tmp_eval_loss.mean().item()
      input_ids = batch['input_ids']
      nb_eval_examples += input_ids.size(0)
      nb_eval_steps += 1

原因就是代码中predicts和labels一直被保留着,而且是在GPU上,为了解决这个问题,我们应该将数据放在CPU上,改成一下即可

predicts=[]
    labels=[]
    for batch in tqdm(AsyncDataLoader(eval_dataloader), ncols=80, desc='Evaluating: {}'.format(prefix), disable=no_tqdm):
      batch = batch_to(batch, device)
      with torch.no_grad():
        output = model(**batch)
      logits = output['logits'].detach().cpu() # 修改
      tmp_eval_loss = output['loss'].detach()
      if 'labels' in output:
        label_ids = output['labels'].detach().cpu() # 修改
      else:
        label_ids = batch['labels'].cpu() # 修改
      predicts.append(logits)
      labels.append(label_ids)
      eval_loss += tmp_eval_loss.mean().item()
      input_ids = batch['input_ids']
      nb_eval_examples += input_ids.size(0)
      nb_eval_steps += 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ResNet (Residual Neural Network) is a popular deep learning architecture commonly used for image classification tasks. It was introduced by Kaiming He et al. in 2015. ResNet uses residual connections to allow the network to learn residual mappings, making it easier to train very deep networks. To implement ResNet in PyTorch, you can use the torchvision library, which provides pre-trained ResNet models as well as the ability to create custom ResNet architectures. Here is an example of how to use the torchvision library to load a pre-trained ResNet model and perform image classification: ```python import torch import torchvision.models as models import torchvision.transforms as transforms from PIL import Image # Load the pre-trained ResNet model resnet = models.resnet50(pretrained=True) # Preprocess the input image transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # Load and preprocess the image image = Image.open("image.jpg") input_tensor = transform(image) input_batch = input_tensor.unsqueeze(0) # Move the input tensor to the GPU if available device = torch.device("cuda" if torch.cuda.is_available() else "cpu") input_batch = input_batch.to(device) # Set the model to evaluation mode resnet.eval() # Make predictions with torch.no_grad(): input_batch = input_batch.to(device) output = resnet(input_batch) # Load the class labels with open("imagenet_classes.txt") as f: class_labels = [line.strip() for line in f.readlines()] # Get the predicted class label _, predicted_idx = torch.max(output, 1) predicted_label = class_labels[predicted_idx.item()] # Print the predicted label print("Predicted label: ", predicted_label) ``` In the above code, the input image is preprocessed using the same transformations used during training of the ResNet model. The pre-trained ResNet model is loaded, and the input image is passed through the model to get predictions. The predicted class label is then obtained and printed. Note: Make sure to replace "image.jpg" with the path to your input image and "imagenet_classes.txt" with the path to the class labels file corresponding to the pre-trained ResNet model you are using.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值