【问题标题】:Pytorch RuntimeError: CUDA out of memory with a huge amount of free memoryPytorch RuntimeError:CUDA 内存不足,可用内存量很大
【发布时间】:2022-07-15 22:33:33
【问题描述】:

在训练模型时,我遇到了以下问题:

RuntimeError: CUDA out of memory. Tried to allocate 304.00 MiB (GPU 0; 8.00 GiB total capacity; 142.76 MiB already allocated; 6.32 GiB free; 158.00 MiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

正如我们所见,尝试分配 304 MiB 内存时发生错误,而 6.32 GiB 是空闲的!问题是什么?如我所见,建议的选项是设置 max_split_size_mb 以避免碎片。它会有所帮助吗?如何正确地做到这一点?

这是我的 PyTorch 版本:

火炬==1.10.2+cu113

torchvision==0.11.3+cu113

火炬===0.10.2+cu113

【问题讨论】:

    标签: python machine-learning pytorch computer-vision


    【解决方案1】:

    您的问题可能是由于您的 GPU 内存碎片造成的。您可能希望清空缓存分配器使用的缓存内存。

     import torch
     torch.cuda.empty_cache()
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
    • 我在训练模型之前调用了这个方法,得到了同样的错误。
    • 您可以尝试减小批量大小吗?并确保在再次运行之前重新启动。
    【解决方案2】:

    我正在尝试这个命令:

    python3 val.py --weights ./weights/yolov5l-xs-1.pt --img 1996 --data ./data/VisDrone.yaml
    

    我有一个 24G Titan 显卡。

    然后我减小了图像大小并为我工作。到:

    python3 val.py --weights ./weights/yolov5l-xs-1.pt --img 1280  --data ./data/VisDrone.yaml
    

    结果:

    Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100%|████████████████████████████████| 18/18 [00:50<00:00,  2.79s/it]
                     all        548      38759      0.653      0.537      0.584      0.375
              pedestrian        548       8844       0.74      0.631      0.708      0.375
                  people        548       5125      0.677      0.506      0.574      0.258
                 bicycle        548       1287      0.541      0.377       0.41      0.213
                     car        548      14064      0.828      0.868      0.904      0.681
                     van        548       1975      0.636      0.566      0.601      0.453
                   truck        548        750      0.595      0.516      0.538      0.388
                tricycle        548       1045      0.601      0.416      0.457      0.288
         awning-tricycle        548        532      0.387      0.242      0.245      0.173
                     bus        548        251      0.782      0.653      0.725      0.565
                   motor        548       4886      0.744      0.598      0.674      0.355
    

    【讨论】:

      【解决方案3】:

      我已尝试减小批量大小,但仍然...

      CUDA Error when training

      【讨论】:

        猜你喜欢
        • 2021-09-17
        • 2022-01-12
        • 2021-10-07
        • 2023-02-09
        • 2019-04-06
        • 2020-12-22
        • 1970-01-01
        • 2023-04-05
        • 2021-12-16
        相关资源
        最近更新 更多