ultralytics / ultralytics

Ultralytics YOLO11 🚀
https://docs.ultralytics.com
GNU Affero General Public License v3.0
36.33k stars 7k forks source link

How to limit ultralytics yolo11 to use single thread (cpu) #19071

Closed danielroson closed 5 days ago

danielroson commented 5 days ago

Search before asking

Question

How to limit yolo11 ultralytics to use single thread (CPU) during prediction process.

I have a project using image classifications that I need to limit the use of server resources (threads).

I tried: results = model(source, device='cpu', workers=1)

or:

export OPENBLAS_NUM_THREADS=1 export NUMEXPR_NUM_THREADS=1 export VECLIB_MAXIMUM_THREADS=1 export NUM_THREADS=1

or:

import torch torch.set_num_threads(1) torch.set_num_interop_threads(1)

For the test, the application loads a class that does the inference and another that keeps sending the same image and collecting the result:

    def run(self):

        while True:
            json_result = self.app.classify(passage_image_byte)
            if len(json_result) == 0:
                self.status.inc_image_fail_count()
            else:
                self.status.inc_image_class_count()

See below the CPU usage during the test:

Image

Additional

No response

UltralyticsAssistant commented 5 days ago

👋 Hello @danielroson, thank you for your interest in Ultralytics 🚀! We recommend checking out the Docs for more information, where many Python and CLI usage examples are available to assist with various workflows.

If this is a 🐛 Bug Report or unexpected behavior, please share a minimum reproducible example so that we can better understand and address your issue. This includes the exact code snippet used and the steps to replicate the behavior you are observing.

For custom configuration-related ❓ Questions like this, providing more context about your environment (e.g., OS, version of Python, PyTorch, and ultralytics package) and any specific error logs or messages would help us diagnose the issue more effectively. If you haven't already, ensure you're following Tips for Best Training Results for optimal performance.

Upgrade

First, please ensure you're running the latest version of Ultralytics, as updates often include fixes and optimizations. Upgrade with the following command:

pip install -U ultralytics

Environments

YOLO models can be run in a variety of environments. Ensure you're testing in one of our verified setups to see if this resolves the issue:

Additionally, specifying the environment.yml file allows you to recreate our exact dependency setup.

Status

To verify the integrity of your setup, you can check the status of our CI tests, which run daily to validate functionality against various tasks and modes:

Ultralytics CI

These tests ensure compatibility on macOS, Windows, and Ubuntu platforms with all features.

We appreciate your contribution to the community by asking this question! 😊 Please note that this is an automated response, and an Ultralytics engineer will follow up with you shortly for further assistance.

danielroson commented 5 days ago

Sorry, I found the solution

https://github.com/ultralytics/ultralytics/issues/16724

glenn-jocher commented 4 days ago

Great to hear you found a solution! For others seeking to limit YOLOv11 to a single CPU thread, you can set environment variables before importing torch/ultralytics:

import os
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["MKL_NUM_THREADS"] = "1"

import torch
torch.set_num_threads(1)

from ultralytics import YOLO
model = YOLO('yolo11n.pt')
results = model(source, device='cpu')

This combination of environment variables and thread limits helps restrict CPU parallelism. For more details, refer to PyTorch CPU parallelism docs.