yolov7在检测的时候报RuntimeError: CUDA out of memory. Tried to allocate

    在训练时,这个问题很常见,只需要确保cudnn 和 torch 正常的情况下,修改batch-size即可解决。但在检测中,特别时检测一段时间后出现RuntimeError: CUDA out of memory. Tried to allocate xx MiB (GPU 0; xx GiB total capacity; xx GiB already allocated; xx MiB free; xx GiB 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_CON
    该问题如果直接用他本身的检测类detect.py时无问题,在自己封装过程的检测类,比如结合deepsort时封装的检测器会报这样的问题。本身检测类在最后用了 with torch.no_grad(): ,该代码作用是放在里面的代码块不计算梯度。在自己封装的类里面加入torch.set_grad_enabled(False),如果自己封装类没有引入torch ,需要import torch,如果开启torch.set_grad_enabled(True),关闭计算梯度的功能,检测一段时间后就不回出现显存溢出的情况。`
    import torch 
    torch.set_grad_enabled(False) #不计算梯度 防止显存运行一段时间后溢出`
    ![detect.py](https://img-blog.csdnimg.cn/aa112bab88984ef7b1e9fe06695f6c46.png)
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值