# cv::error: (-2:未指定错误) 使用 CUDA 后端进行 DNN 前向传播时分配内存失败

- **ID:** `opencv/dnn-forward-cuda-memory`
- **领域:** opencv
- **类别:** resource_error
- **错误码:** `-2`
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

CUDA GPU 在 DNN 前向传播期间内存不足，通常是因为模型过大或批次大小超过 GPU VRAM 容量。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |
| 4.10.0 | active | — | — |

## 解决方案

1. ```
   Reduce batch size in the DNN forward pass: `net.setInput(blob); output = net.forward()` with a smaller blob (e.g., half the batch size).
   ```
2. ```
   Use model optimization techniques like quantization or pruning before inference, or switch to a smaller model variant.
   ```
3. ```
   Free unused GPU memory by calling `torch.cuda.empty_cache()` or `cv2.cuda.resetDevice()` before the forward pass.
   ```

## 无效尝试

- **** — CPU may also run out of memory or be too slow; the issue is the model/batch size, not the backend alone. (60% 失败率)
- **** — Environment variables don't change available VRAM; they only select GPU devices. (85% 失败率)
- **** — CUDA memory is GPU VRAM, not system RAM; adding RAM does not help GPU memory. (95% 失败率)
