-2
opencv
resource_error
ai_generated
partial
cv::error: (-2:未指定错误) 使用 CUDA 后端进行 DNN 前向传播时分配内存失败
cv::error: (-2:Unspecified error) Failed to allocate memory for DNN forward pass with CUDA backend
ID: opencv/dnn-forward-cuda-memory
75%修复率
82%置信度
1证据数
2023-07-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 4.6.0 | active | — | — | — |
| 4.7.0 | active | — | — | — |
| 4.8.0 | active | — | — | — |
| 4.9.0 | active | — | — | — |
| 4.10.0 | active | — | — | — |
根因分析
CUDA GPU 在 DNN 前向传播期间内存不足,通常是因为模型过大或批次大小超过 GPU VRAM 容量。
English
CUDA GPU ran out of memory during DNN forward pass, often due to large model or batch size exceeding GPU VRAM capacity.
官方文档
https://docs.opencv.org/4.x/d6/d0f/group__dnn.html#ga29f34df9376379a603acd8df581ac8d7解决方案
-
Reduce batch size in the DNN forward pass: `net.setInput(blob); output = net.forward()` with a smaller blob (e.g., half the batch size).
-
Use model optimization techniques like quantization or pruning before inference, or switch to a smaller model variant.
-
Free unused GPU memory by calling `torch.cuda.empty_cache()` or `cv2.cuda.resetDevice()` before the forward pass.
无效尝试
常见但无效的做法:
-
60% 失败
CPU may also run out of memory or be too slow; the issue is the model/batch size, not the backend alone.
-
85% 失败
Environment variables don't change available VRAM; they only select GPU devices.
-
95% 失败
CUDA memory is GPU VRAM, not system RAM; adding RAM does not help GPU memory.