CUDA out of memory, or training is very slow

  • YOLO
  • TensorFlow
  • Colab

You may see

torch.OutOfMemoryError: CUDA out of memory.
Resource exhausted: OOM when allocating tensor

Why it happens

  • The batch or picture size is too big.
  • No graphics card is switched on.

How to fix it

  1. Use a smaller batch (8, 4) and imgsz (640, 416).
  2. In Colab choose Runtime > Change runtime type > GPU.
  3. Restart the runtime to free memory.

It worked if: One epoch finishes.

From the official documentation. From PyTorch's documentation; not reproduced (no graphics card here). Sources: Ultralytics: training.

Didn't help? Search another error or ask us.