Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPU Memory Usage during Tensor Inference #8719

Closed
1 task done
K-YLMike opened this issue Jul 26, 2022 · 8 comments
Closed
1 task done

GPU Memory Usage during Tensor Inference #8719

K-YLMike opened this issue Jul 26, 2022 · 8 comments
Labels
question Further information is requested Stale

Comments

@K-YLMike
Copy link

Search before asking

Question

Hi Sir, thanks for providing amazing codes! Now I am almost familiar with how to make own dataset, training with all -- commands, inference, and deploy in TensorRT. The question is no matter I use n/s/m model, or edit width_multiple: 0.50 to 0.25 even less in yolov5_custom.yaml, or change --img from 640 to 320,finally I turn these pt models to tensorRT, the GPU memory usages are always larger than 1700 Mib in nvidia-smi (even file size of pt can be 1.6 mb which is far smaller than 14.4 mb yolov5s.engine). Could you tell me how to further decrease GPU memory usage during inference in tensorRT?

Additional

No response

@K-YLMike K-YLMike added the question Further information is requested label Jul 26, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jul 26, 2022

👋 Hello @M1keStr0ng, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email [email protected].

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 26, 2022

@M1keStr0ng 👋 Hello! Thanks for asking about CUDA memory issues. YOLOv5 🚀 can be trained on CPU, single-GPU, or multi-GPU. When training on GPU it is important to keep your batch-size small enough that you do not use all of your GPU memory, otherwise you will see a CUDA Out Of Memory (OOM) Error and your training will crash. You can observe your CUDA memory utilization using either the nvidia-smi command or by viewing your console output:

Screenshot 2021-05-28 at 12 19 51

CUDA Out of Memory Solutions

If you encounter a CUDA OOM error, the steps you can take to reduce your memory usage are:

  • Reduce --batch-size
  • Reduce --img-size
  • Reduce model size, i.e. from YOLOv5x -> YOLOv5l -> YOLOv5m -> YOLOv5s > YOLOv5n
  • Train with multi-GPU at the same --batch-size
  • Upgrade your hardware to a larger GPU
  • Train on free GPU backends with up to 16GB of CUDA memory: Open In Colab Open In Kaggle

AutoBatch

You can use YOLOv5 AutoBatch (NEW) to find the best batch size for your training by passing --batch-size -1. AutoBatch will solve for a 90% CUDA memory-utilization batch-size given your training settings. AutoBatch is experimental, and only works for Single-GPU training. It may not work on all systems, and is not recommended for production use.

Screenshot 2021-11-06 at 12 31 10

Good luck 🍀 and let us know if you have any other questions!

@K-YLMike
Copy link
Author

@glenn-jocher Thanks for replying. I know this tutorial and it is for training. My issue is about inference GPU memory usage. During inference, I check the batch is 1, model is .pt --> .engine, image is 640, source is Webcam/RTSP/Video, and decrease width_multiple in yolov5.yaml. What I would like to ask is how can I further decrease GPU Memory during inference, because I need to run multiple yolov5 models in one GPU card (cannot upgrade GPU). The thing is I decreased width_multiple and it did decrease the model size, but GPU memory usage is almost kept the same during reference. REALLY needs your generous help, thanks!

@glenn-jocher
Copy link
Member

@M1keStr0ng I don't have any quantifiable information on inference memory, but I'd assume smaller models, image sizes and batch sizes would correlate to reduced requirements.

@sohaiblaraba
Copy link

@glenn-jocher Thanks for replying. I know this tutorial and it is for training. My issue is about inference GPU memory usage. During inference, I check the batch is 1, model is .pt --> .engine, image is 640, source is Webcam/RTSP/Video, and decrease width_multiple in yolov5.yaml. What I would like to ask is how can I further decrease GPU Memory during inference, because I need to run multiple yolov5 models in one GPU card (cannot upgrade GPU). The thing is I decreased width_multiple and it did decrease the model size, but GPU memory usage is almost kept the same during reference. REALLY needs your generous help, thanks!

I'm facing a similar issue. When I run an inference on GPU, the memory usage keeps growing and leads to "RuntimeError: CUDA out of memory. " after only 3 or 4 images being processed.
This is the first time it happens, and only with the latest code update.
... Any solution?

@K-YLMike
Copy link
Author

K-YLMike commented Aug 7, 2022

Since you are talking about inference, I will skip training part. Your situation never happens my here, but I might provide some checks:

  1. Ensure you are inferencing under fully updated codes, including library.
  2. Try s or m model (yolov5s.pt/yolov5m.pt).
  3. Try Webcam/IP Cam/Videos. See if it happens again.
  4. Set inference batch size as 1.
  5. Set image size as default provided by official code.
  6. If you write your own codes for inference, I suggest you follow detect.py.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2022

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

@glenn-jocher
Copy link
Member

@K-YLMike Those are great suggestions. Have you tried using smaller model sizes, batch sizes, or image sizes for inference to see if there's any change in GPU memory usage? If you're using your own custom inference scripts, I'd recommend comparing your implementation to the standard detect.py script to ensure there are no memory leakage issues. If you're still experiencing difficulties, you may want to consider opening an issue on the YOLOv5 GitHub repo with details about your setup and we can investigate it further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

3 participants